1. 8.3.x core/modules/content_moderation/content_moderation.module
  2. 8.2.x core/modules/content_moderation/content_moderation.module
  3. 8.4.x core/modules/content_moderation/content_moderation.module

Contains content_moderation.module.

File

core/modules/content_moderation/content_moderation.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains content_moderation.module.
  5. */
  6. use Drupal\content_moderation\EntityOperations;
  7. use Drupal\content_moderation\EntityTypeInfo;
  8. use Drupal\content_moderation\ContentPreprocess;
  9. use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublishNode;
  10. use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublishNode;
  11. use Drupal\content_moderation\Plugin\Menu\EditTab;
  12. use Drupal\Core\Access\AccessResult;
  13. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  14. use Drupal\Core\Entity\EntityInterface;
  15. use Drupal\Core\Entity\EntityTypeInterface;
  16. use Drupal\Core\Form\FormStateInterface;
  17. use Drupal\Core\Routing\RouteMatchInterface;
  18. use Drupal\Core\Session\AccountInterface;
  19. use Drupal\node\NodeInterface;
  20. use Drupal\node\Plugin\Action\PublishNode;
  21. use Drupal\node\Plugin\Action\UnpublishNode;
  22. /**
  23. * Implements hook_help().
  24. */
  25. function content_moderation_help($route_name, RouteMatchInterface $route_match) {
  26. switch ($route_name) {
  27. // Main module help for the content_moderation module.
  28. case 'help.page.content_moderation':
  29. $output = '';
  30. $output .= '<h3>' . t('About') . '</h3>';
  31. $output .= '<p>' . t('The Content Moderation module provides basic moderation for content. This lets site admins define states for content, and then define transitions between those states. For more information, see the <a href=":content_moderation">online documentation for the Content Moderation module</a>.', [':content_moderation' => 'https://www.drupal.org/documentation/modules/content_moderation']) . '</p>';
  32. $output .= '<h3>' . t('Uses') . '</h3>';
  33. $output .= '<dl>';
  34. $output .= '<dt>' . t('Moderation states') . '</dt>';
  35. $output .= '<dd>' . t('Moderation states provide the <em>Draft</em> and <em>Archived</em> states as additions to the basic <em>Published</em> option. You can click the blue <em>Add Moderation state</em> button and create new states.') . '</dd>';
  36. $output .= '<dt>' . t('Moderation state transitions') . '</dt>';
  37. $output .= '<dd>' . t('Using the "Moderation state transitions" screen, you can create the actual workflow. You decide the direction in which content moves from state to state, and which user roles are allowed to make that move.') . '</dd>';
  38. $output .= '<dt>' . t('Configure Content Moderation permissions') . '</dt>';
  39. $output .= '<dd>' . t('Each state is exposed as a permission. If a user has the permission for a transition, then they can move that node from the start state to the end state') . '</p>';
  40. $output .= '</dl>';
  41. return $output;
  42. }
  43. }
  44. /**
  45. * Implements hook_entity_base_field_info().
  46. */
  47. function content_moderation_entity_base_field_info(EntityTypeInterface $entity_type) {
  48. return \Drupal::service('class_resolver')
  49. ->getInstanceFromDefinition(EntityTypeInfo::class)
  50. ->entityBaseFieldInfo($entity_type);
  51. }
  52. /**
  53. * Implements hook_entity_type_alter().
  54. */
  55. function content_moderation_entity_type_alter(array &$entity_types) {
  56. \Drupal::service('class_resolver')
  57. ->getInstanceFromDefinition(EntityTypeInfo::class)
  58. ->entityTypeAlter($entity_types);
  59. }
  60. /**
  61. * Implements hook_entity_operation().
  62. */
  63. function content_moderation_entity_operation(EntityInterface $entity) {
  64. return \Drupal::service('class_resolver')
  65. ->getInstanceFromDefinition(EntityTypeInfo::class)
  66. ->entityOperation($entity);
  67. }
  68. /**
  69. * Sets required flag based on enabled state.
  70. */
  71. function content_moderation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  72. \Drupal::service('class_resolver')
  73. ->getInstanceFromDefinition(EntityTypeInfo::class)
  74. ->entityBundleFieldInfoAlter($fields, $entity_type, $bundle);
  75. }
  76. /**
  77. * Implements hook_entity_presave().
  78. */
  79. function content_moderation_entity_presave(EntityInterface $entity) {
  80. return \Drupal::service('class_resolver')
  81. ->getInstanceFromDefinition(EntityOperations::class)
  82. ->entityPresave($entity);
  83. }
  84. /**
  85. * Implements hook_entity_insert().
  86. */
  87. function content_moderation_entity_insert(EntityInterface $entity) {
  88. return \Drupal::service('class_resolver')
  89. ->getInstanceFromDefinition(EntityOperations::class)
  90. ->entityInsert($entity);
  91. }
  92. /**
  93. * Implements hook_entity_update().
  94. */
  95. function content_moderation_entity_update(EntityInterface $entity) {
  96. return \Drupal::service('class_resolver')
  97. ->getInstanceFromDefinition(EntityOperations::class)
  98. ->entityUpdate($entity);
  99. }
  100. /**
  101. * Implements hook_local_tasks_alter().
  102. */
  103. function content_moderation_local_tasks_alter(&$local_tasks) {
  104. $content_entity_type_ids = array_keys(array_filter(\Drupal::entityTypeManager()->getDefinitions(), function (EntityTypeInterface $entity_type) {
  105. return $entity_type->isRevisionable();
  106. }));
  107. foreach ($content_entity_type_ids as $content_entity_type_id) {
  108. if (isset($local_tasks["entity.$content_entity_type_id.edit_form"])) {
  109. $local_tasks["entity.$content_entity_type_id.edit_form"]['class'] = EditTab::class;
  110. $local_tasks["entity.$content_entity_type_id.edit_form"]['entity_type_id'] = $content_entity_type_id;
  111. }
  112. }
  113. }
  114. /**
  115. * Implements hook_form_alter().
  116. */
  117. function content_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  118. \Drupal::service('class_resolver')
  119. ->getInstanceFromDefinition(EntityTypeInfo::class)
  120. ->formAlter($form, $form_state, $form_id);
  121. }
  122. /**
  123. * Implements hook_preprocess_HOOK().
  124. *
  125. * Many default node templates rely on $page to determine whether to output the
  126. * node title as part of the node content.
  127. */
  128. function content_moderation_preprocess_node(&$variables) {
  129. \Drupal::service('class_resolver')
  130. ->getInstanceFromDefinition(ContentPreprocess::class)
  131. ->preprocessNode($variables);
  132. }
  133. /**
  134. * Implements hook_entity_extra_field_info().
  135. */
  136. function content_moderation_entity_extra_field_info() {
  137. return \Drupal::service('class_resolver')
  138. ->getInstanceFromDefinition(EntityTypeInfo::class)
  139. ->entityExtraFieldInfo();
  140. }
  141. /**
  142. * Implements hook_entity_view().
  143. */
  144. function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  145. \Drupal::service('class_resolver')
  146. ->getInstanceFromDefinition(EntityOperations::class)
  147. ->entityView($build, $entity, $display, $view_mode);
  148. }
  149. /**
  150. * Implements hook_node_access().
  151. *
  152. * Nodes in particular should be viewable if unpublished and the user has
  153. * the appropriate permission. This permission is therefore effectively
  154. * mandatory for any user that wants to moderate things.
  155. */
  156. function content_moderation_node_access(NodeInterface $node, $operation, AccountInterface $account) {
  157. /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  158. $moderation_info = Drupal::service('content_moderation.moderation_information');
  159. $access_result = NULL;
  160. if ($operation === 'view') {
  161. $access_result = (!$node->isPublished())
  162. ? AccessResult::allowedIfHasPermission($account, 'view any unpublished content')
  163. : AccessResult::neutral();
  164. $access_result->addCacheableDependency($node);
  165. }
  166. elseif ($operation === 'update' && $moderation_info->isModeratedEntity($node) && $node->moderation_state && $node->moderation_state->target_id) {
  167. /** @var \Drupal\content_moderation\StateTransitionValidation $transition_validation */
  168. $transition_validation = \Drupal::service('content_moderation.state_transition_validation');
  169. $valid_transition_targets = $transition_validation->getValidTransitionTargets($node, $account);
  170. $access_result = $valid_transition_targets ? AccessResult::neutral() : AccessResult::forbidden();
  171. $access_result->addCacheableDependency($node);
  172. $access_result->addCacheableDependency($account);
  173. foreach ($valid_transition_targets as $valid_transition_target) {
  174. $access_result->addCacheableDependency($valid_transition_target);
  175. }
  176. }
  177. return $access_result;
  178. }
  179. /**
  180. * Implements hook_theme().
  181. */
  182. function content_moderation_theme() {
  183. return ['entity_moderation_form' => ['render element' => 'form']];
  184. }
  185. /**
  186. * Implements hook_action_info_alter().
  187. */
  188. function content_moderation_action_info_alter(&$definitions) {
  189. // The publish/unpublish actions are not valid on moderated entities. So swap
  190. // their implementations out for alternates that will become a no-op on a
  191. // moderated node. If another module has already swapped out those classes,
  192. // though, we'll be polite and do nothing.
  193. if (isset($definitions['node_publish_action']['class']) && $definitions['node_publish_action']['class'] == PublishNode::class) {
  194. $definitions['node_publish_action']['class'] = ModerationOptOutPublishNode::class;
  195. }
  196. if (isset($definitions['node_unpublish_action']['class']) && $definitions['node_unpublish_action']['class'] == UnpublishNode::class) {
  197. $definitions['node_unpublish_action']['class'] = ModerationOptOutUnpublishNode::class;
  198. }
  199. }

Functions