content_moderation.module 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Contains content_moderation.module.
 */

use Drupal\content_moderation\EntityOperations;
use Drupal\content_moderation\EntityTypeInfo;
use Drupal\content_moderation\ContentPreprocess;
use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublishNode;
use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublishNode;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
16
use Drupal\Core\Entity\EntityPublishedInterface;
17
use Drupal\Core\Entity\EntityTypeInterface;
18 19
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
20 21 22
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
23
use Drupal\workflows\WorkflowInterface;
24 25
use Drupal\node\Plugin\Action\PublishNode;
use Drupal\node\Plugin\Action\UnpublishNode;
26
use Drupal\workflows\Entity\Workflow;
27 28 29 30 31 32 33 34 35 36

/**
 * Implements hook_help().
 */
function content_moderation_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the content_moderation module.
    case 'help.page.content_moderation':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
37
      $output .= '<p>' . t('The Content Moderation module provides moderation for content by applying workflows to content. 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>';
38 39
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
40 41
      $output .= '<dt>' . t('Configuring workflows') . '</dt>';
      $output .= '<dd>' . t('Enable the Workflow UI module to create, edit and delete content moderation workflows.') . '</p>';
42
      $output .= '<dt>' . t('Configure Content Moderation permissions') . '</dt>';
43
      $output .= '<dd>' . t('Each transition 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>';
44
      $output .= '</dl>';
45 46 47 48 49 50 51 52
      return $output;
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function content_moderation_entity_base_field_info(EntityTypeInterface $entity_type) {
53 54 55
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityBaseFieldInfo($entity_type);
56 57 58 59 60 61
}

/**
 * Implements hook_entity_type_alter().
 */
function content_moderation_entity_type_alter(array &$entity_types) {
62 63 64
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityTypeAlter($entity_types);
65 66 67 68 69 70
}

/**
 * Implements hook_entity_presave().
 */
function content_moderation_entity_presave(EntityInterface $entity) {
71 72 73
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityPresave($entity);
74 75 76 77 78 79
}

/**
 * Implements hook_entity_insert().
 */
function content_moderation_entity_insert(EntityInterface $entity) {
80 81 82
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityInsert($entity);
83 84 85 86 87 88
}

/**
 * Implements hook_entity_update().
 */
function content_moderation_entity_update(EntityInterface $entity) {
89 90 91
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityUpdate($entity);
92 93 94 95 96 97
}

/**
 * Implements hook_form_alter().
 */
function content_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
98 99 100
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->formAlter($form, $form_state, $form_id);
101 102 103 104 105 106 107 108 109
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Many default node templates rely on $page to determine whether to output the
 * node title as part of the node content.
 */
function content_moderation_preprocess_node(&$variables) {
110 111 112
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(ContentPreprocess::class)
    ->preprocessNode($variables);
113 114 115 116 117 118
}

/**
 * Implements hook_entity_extra_field_info().
 */
function content_moderation_entity_extra_field_info() {
119 120 121
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityTypeInfo::class)
    ->entityExtraFieldInfo();
122 123 124 125 126 127
}

/**
 * Implements hook_entity_view().
 */
function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
128 129 130
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityView($build, $entity, $display, $view_mode);
131 132 133
}

/**
134
 * Implements hook_entity_access().
135
 *
136 137 138
 * Entities should be viewable if unpublished and the user has the appropriate
 * permission. This permission is therefore effectively mandatory for any user
 * that wants to moderate things.
139
 */
140
function content_moderation_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
141 142 143 144 145
  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = Drupal::service('content_moderation.moderation_information');

  $access_result = NULL;
  if ($operation === 'view') {
146
    $access_result = (($entity instanceof EntityPublishedInterface) && !$entity->isPublished())
147 148 149
      ? AccessResult::allowedIfHasPermission($account, 'view any unpublished content')
      : AccessResult::neutral();

150
    $access_result->addCacheableDependency($entity);
151
  }
152
  elseif ($operation === 'update' && $moderation_info->isModeratedEntity($entity) && $entity->moderation_state) {
153 154 155
    /** @var \Drupal\content_moderation\StateTransitionValidation $transition_validation */
    $transition_validation = \Drupal::service('content_moderation.state_transition_validation');

156
    $valid_transition_targets = $transition_validation->getValidTransitions($entity, $account);
157 158
    $access_result = $valid_transition_targets ? AccessResult::neutral() : AccessResult::forbidden();

159
    $access_result->addCacheableDependency($entity);
160
    $access_result->addCacheableDependency($account);
161
    $workflow = $moderation_info->getWorkflowForEntity($entity);
162
    $access_result->addCacheableDependency($workflow);
163 164 165 166 167 168 169 170
    foreach ($valid_transition_targets as $valid_transition_target) {
      $access_result->addCacheableDependency($valid_transition_target);
    }
  }

  return $access_result;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * Implements hook_entity_field_access().
 */
function content_moderation_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($items && $operation === 'edit') {
    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = Drupal::service('content_moderation.moderation_information');

    $entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getTargetEntityTypeId());

    $entity = $items->getEntity();

    // Deny edit access to the published field if the entity is being moderated.
    if ($entity_type->hasKey('published') && $moderation_info->isModeratedEntity($entity) && $entity->moderation_state && $field_definition->getName() == $entity_type->getKey('published')) {
      return AccessResult::forbidden();
    }
  }

  return AccessResult::neutral();
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/**
 * Implements hook_theme().
 */
function content_moderation_theme() {
  return ['entity_moderation_form' => ['render element' => 'form']];
}

/**
 * Implements hook_action_info_alter().
 */
function content_moderation_action_info_alter(&$definitions) {

  // The publish/unpublish actions are not valid on moderated entities. So swap
  // their implementations out for alternates that will become a no-op on a
  // moderated node. If another module has already swapped out those classes,
  // though, we'll be polite and do nothing.
  if (isset($definitions['node_publish_action']['class']) && $definitions['node_publish_action']['class'] == PublishNode::class) {
    $definitions['node_publish_action']['class'] = ModerationOptOutPublishNode::class;
  }
  if (isset($definitions['node_unpublish_action']['class']) && $definitions['node_unpublish_action']['class'] == UnpublishNode::class) {
    $definitions['node_unpublish_action']['class'] = ModerationOptOutUnpublishNode::class;
  }
}
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

/**
 * Implements hook_entity_bundle_info_alter().
 */
function content_moderation_entity_bundle_info_alter(&$bundles) {
  /** @var \Drupal\workflows\WorkflowInterface $workflow */
  foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
    /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
    $plugin = $workflow->getTypePlugin();
    foreach ($plugin->getEntityTypes() as $entity_type_id) {
      foreach ($plugin->getBundlesForEntityType($entity_type_id) as $bundle_id) {
        if (isset($bundles[$entity_type_id][$bundle_id])) {
          $bundles[$entity_type_id][$bundle_id]['workflow'] = $workflow->id();
        }
      }
    }
  }
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247
/**
 * Implements hook_entity_bundle_delete().
 */
function content_moderation_entity_bundle_delete($entity_type_id, $bundle_id) {
  // Remove non-configuration based bundles from content moderation based
  // workflows when they are removed.
  foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
    if ($workflow->getTypePlugin()->appliesToEntityTypeAndBundle($entity_type_id, $bundle_id)) {
      $workflow->getTypePlugin()->removeEntityTypeAndBundle($entity_type_id, $bundle_id);
      $workflow->save();
    }
  }
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function content_moderation_workflow_insert(WorkflowInterface $entity) {
  // Clear bundle cache so workflow gets added or removed from the bundle
  // information.
  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
  // Clear field cache so extra field is added or removed.
  \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function content_moderation_workflow_update(WorkflowInterface $entity) {
  content_moderation_workflow_insert($entity);
}
265 266 267 268 269 270 271 272 273 274

/**
 * Implements hook_rest_resource_alter().
 */
function content_moderation_rest_resource_alter(&$definitions) {
  // ContentModerationState is an internal entity type. Therefore it should not
  // be exposed via REST.
  // @see \Drupal\content_moderation\ContentModerationStateAccessControlHandler
  unset($definitions['entity:content_moderation_state']);
}