Skip to content

Commit

Permalink
Task [drupal-ukraine#134]: 1. create buttons 'Save as Draft' and 'Sen…
Browse files Browse the repository at this point in the history
…t to Review', added form submission handler for them. update node presentation configuration - remove button 'Preview'.
  • Loading branch information
Serhii Shumskiy committed Dec 13, 2019
1 parent 50dcf36 commit e44b9f5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 28 deletions.
33 changes: 13 additions & 20 deletions config/sync/core.entity_form_display.node.presentation.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ dependencies:
- field.field.node.presentation.field_yoast_seo
- image.style.thumbnail
- node.type.presentation
- workflows.workflow.presentation
module:
- content_moderation
- field_group
- link
- paragraphs
Expand Down Expand Up @@ -72,7 +70,7 @@ third_party_settings:
children:
- field_page_content
parent_name: ''
weight: 14
weight: 13
format_type: html_element
format_settings:
id: ''
Expand All @@ -94,7 +92,7 @@ mode: default
content:
created:
type: datetime_timestamp
weight: 5
weight: 4
region: content
settings: { }
third_party_settings: { }
Expand Down Expand Up @@ -123,7 +121,7 @@ content:
type: link_default
region: content
field_image:
weight: 13
weight: 12
settings:
progress_indicator: throbber
preview_image_style: thumbnail
Expand Down Expand Up @@ -212,48 +210,42 @@ content:
region: content
langcode:
type: language_select
weight: 3
weight: 2
region: content
settings:
include_locked: true
third_party_settings: { }
moderation_state:
type: moderation_state_default
weight: 2
settings: { }
region: content
third_party_settings: { }
path:
type: path
weight: 8
weight: 7
region: content
settings: { }
third_party_settings: { }
promote:
type: boolean_checkbox
settings:
display_label: true
weight: 6
weight: 5
region: content
third_party_settings: { }
publish_on:
type: datetime_timestamp_no_default
weight: 11
weight: 10
region: content
settings: { }
third_party_settings: { }
status:
type: boolean_checkbox
settings:
display_label: true
weight: 9
weight: 8
region: content
third_party_settings: { }
sticky:
type: boolean_checkbox
settings:
display_label: true
weight: 7
weight: 6
region: content
third_party_settings: { }
title:
Expand All @@ -265,13 +257,13 @@ content:
placeholder: ''
third_party_settings: { }
translation:
weight: 10
weight: 9
region: content
settings: { }
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 4
weight: 3
settings:
match_operator: CONTAINS
size: 60
Expand All @@ -280,10 +272,11 @@ content:
third_party_settings: { }
unpublish_on:
type: datetime_timestamp_no_default
weight: 12
weight: 11
region: content
settings: { }
third_party_settings: { }
hidden:
field_meta_tags: true
field_yoast_seo: true
moderation_state: true
2 changes: 1 addition & 1 deletion config/sync/node.type.presentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ type: presentation
description: ''
help: ''
new_revision: true
preview_mode: 1
preview_mode: 0
display_submitted: false
2 changes: 2 additions & 0 deletions docroot/modules/custom/dckyiv_core/dckyiv_core.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ type: module
description: Main functionality.
core: 8.x
package: dckyiv
dependencies:
- drupal:content_moderation
103 changes: 96 additions & 7 deletions docroot/modules/custom/dckyiv_core/dckyiv_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/

use Drupal\dckyiv_core\PreprocessTransparentMenu;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\dckyiv_core\Event\NodePresentationSaveEvent;
use Drupal\content_moderation\Entity\ContentModerationState;

/**
* Implements hook_preprocess_HOOK().
Expand All @@ -30,15 +34,13 @@ function dckyiv_core_entity_type_alter(array &$entity_types) {

/**
* Implements hook_form_alter().
*
* Create alter form by form_id.
*/
function dckyiv_core_form_alter(&$form, &$form_state, $form_id) {
// Override revision form in presentation form.
if ($form_id === 'node_presentation_form') {
$form['revision_information']['#title'] = t('Message to org team:');
$form['revision_log']['widget'][0]['value']['#title'] = FALSE;
$form['revision_log']['widget'][0]['value']['#description'] = t('If you have any comments to org team, write them here.');

$form['actions']['preview']['#access'] = FALSE;
$function = __FUNCTION__ . "__{$form_id}";
if (function_exists($function)) {
$function($form, $form_state, $form_id);
}
}

Expand All @@ -48,3 +50,90 @@ function dckyiv_core_form_alter(&$form, &$form_state, $form_id) {
function dckyiv_core_toolbar_alter(&$items) {
$items['administration']['#attached']['library'][] = 'dckyiv_core/admin_menu_icons';
}

/**
* Implements hook_form_alter().
*/
function dckyiv_core_form_alter__node_presentation_form(&$form, &$form_state, $form_id) {
$form['revision_information']['#title'] = t('Message to org team:');
$form['revision_log']['widget'][0]['value']['#title'] = FALSE;
$form['revision_log']['widget'][0]['value']['#description'] = t('If you have any comments to org team, write them here.');

$form['actions']['sent_to_review'] = $form['actions']['submit'];
$form['actions']['submit']['#submit'][] = 'node_presentation_save_as_draft';
$form['actions']['submit']['#value'] = t('Save as Draft');
$form['actions']['sent_to_review']['#submit'][] = 'node_presentation_sent_to_review';
$form['actions']['sent_to_review']['#value'] = t('Sent to Review');
}

/**
* Form submission handler for content moderation state create.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function node_presentation_sent_to_review(&$form, FormStateInterface $form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
create_content_moderation_state($node, 'sent_to_review');
}

/**
* Form submission handler for content moderation state create.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
function node_presentation_save_as_draft(&$form, FormStateInterface $form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
create_content_moderation_state($node, 'draft');
}

/**
* Constructs a new 'ContentModerationState' entity object and saving it.
*
* @param \Drupal\node\NodeInterface $node
* The current node presentation object.
* @param string $moderation_state_id
* The moderation state ID.
*/
function create_content_moderation_state(NodeInterface $node, string $moderation_state_id) {
/** @var \Drupal\content_moderation\StateTransitionValidationInterface $validator */
$validator = \Drupal::service('content_moderation.state_transition_validation');
$current_user = \Drupal::currentUser();
$transitions = $validator->getValidTransitions($node, $current_user);

$list = [];
foreach ($transitions as $transition) {
$list[] = $transition->to()->id();
}

if (!in_array($moderation_state_id, $list)) {
return;
}

/** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
$content_moderation_info = \Drupal::service('content_moderation.moderation_information');
$workflow = $content_moderation_info->getWorkflowForEntity($node);

$entity = ContentModerationState::create([
'uid' => $current_user->id(),
'workflow' => $workflow,
'moderation_state' => $moderation_state_id,
'content_entity_type_id' => $node->getEntityTypeId(),
'content_entity_id' => $node->id(),
'content_entity_revision_id' => $node->getRevisionId(),
]);

try {
$entity->save();
}
catch (Exception $e) {
Drupal::logger('node_presentation_save')->error($e->getMessage());
}
}

0 comments on commit e44b9f5

Please sign in to comment.