diff --git a/core/modules/content_moderation/content_moderation.module b/core/modules/content_moderation/content_moderation.module index 9ab6044..938dc3c 100644 --- a/core/modules/content_moderation/content_moderation.module +++ b/core/modules/content_moderation/content_moderation.module @@ -9,6 +9,12 @@ * revision) - i.e. unpublish */ +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\content_moderation\Plugin\Menu\EditTab; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; @@ -19,9 +25,6 @@ use Drupal\node\NodeInterface; use Drupal\node\Plugin\Action\PublishNode; use Drupal\node\Plugin\Action\UnpublishNode; -use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublishNode; -use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublishNode; -use Drupal\content_moderation\Plugin\Menu\EditTab; /** * Implements hook_help(). @@ -38,49 +41,82 @@ function content_moderation_help($route_name, RouteMatchInterface $route_match) } /** + * Creates an EntityTypeInfo object to respond to entity hooks. + * + * @return \Drupal\content_moderation\EntityTypeInfo + */ +function _content_moderation_create_entity_type_info() { + return new EntityTypeInfo( + \Drupal::service('string_translation'), + \Drupal::service('content_moderation.moderation_information'), + \Drupal::service('entity_type.manager') + ); +} + +/** * Implements hook_entity_base_field_info(). */ function content_moderation_entity_base_field_info(EntityTypeInterface $entity_type) { - return \Drupal::service('content_moderation.entity_type')->entityBaseFieldInfo($entity_type); + return _content_moderation_create_entity_type_info()->entityBaseFieldInfo($entity_type); } /** * Implements hook_module_implements_alter(). */ function content_moderation_module_implements_alter(&$implementations, $hook) { - /** @var \Drupal\content_moderation\InlineEditingDisabler $inline_editing_disabler */ - $inline_editing_disabler = \Drupal::service('content_moderation.inline_editing_disabler'); - $inline_editing_disabler->moduleImplementsAlter($implementations, $hook); + if ($hook === 'entity_view_alter') { + // Find the quickedit implementation and move content after it. + unset($implementations['content_moderation']); + $implementations['content_moderation'] = FALSE; + } } /** * Implements hook_entity_view_alter(). */ function content_moderation_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { - /** @var \Drupal\content_moderation\InlineEditingDisabler $inline_editing_disabler */ - $inline_editing_disabler = \Drupal::service('content_moderation.inline_editing_disabler'); - $inline_editing_disabler->entityViewAlter($build, $entity, $display); + $moderation_information = \Drupal::service('content_moderation.moderation_information'); + if ($moderation_information->isModeratableEntity($entity) && !$moderation_information->isLatestRevision($entity)) { + // Hide quickedit, because its super confusing for the user to not edit the + // live revision. + unset($build['#attributes']['data-quickedit-entity-id']); + } } /** * Implements hook_entity_type_alter(). */ function content_moderation_entity_type_alter(array &$entity_types) { - \Drupal::service('content_moderation.entity_type')->entityTypeAlter($entity_types); + _content_moderation_create_entity_type_info()->entityTypeAlter($entity_types); } /** * Implements hook_entity_operation(). */ function content_moderation_entity_operation(EntityInterface $entity) { - return \Drupal::service('content_moderation.entity_type')->entityOperation($entity); + _content_moderation_create_entity_type_info()->entityOperation($entity); } /** * Sets required flag based on enabled state. */ function content_moderation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { - return \Drupal::service('content_moderation.entity_type')->entityBundleFieldInfoAlter($fields, $entity_type, $bundle); + _content_moderation_create_entity_type_info()->entityBundleFieldInfoAlter($fields, $entity_type, $bundle); +} + +/** + * Creates an EntityOperations object to respond to entity operation hooks. + * + * @return \Drupal\content_moderation\EntityOperations + */ +function _content_moderation_create_entity_operations() { + return new EntityOperations( + \Drupal::service('content_moderation.moderation_information'), + \Drupal::service('entity_type.manager'), + \Drupal::service('form_builder'), + \Drupal::service('event_dispatcher'), + \Drupal::service('content_moderation.revision_tracker') + ); } /** @@ -90,8 +126,8 @@ function content_moderation_entity_storage_load(array $entities, $entity_type_id // Work around the fact that this hook might be called when the container is // not fully initialized after the module has been enabled. // @todo Remove this check after https://www.drupal.org/node/2753733 is fixed. - if (\Drupal::hasService('content_moderation.entity_operations')) { - \Drupal::service('content_moderation.entity_operations')->entityStorageLoad($entities, $entity_type_id); + if (\Drupal::hasService('content_moderation.moderation_information')) { + _content_moderation_create_entity_operations()->entityStorageLoad($entities, $entity_type_id); } } @@ -99,21 +135,21 @@ function content_moderation_entity_storage_load(array $entities, $entity_type_id * Implements hook_entity_presave(). */ function content_moderation_entity_presave(EntityInterface $entity) { - return \Drupal::service('content_moderation.entity_operations')->entityPresave($entity); + return _content_moderation_create_entity_operations()->entityPresave($entity); } /** * Implements hook_entity_insert(). */ function content_moderation_entity_insert(EntityInterface $entity) { - return \Drupal::service('content_moderation.entity_operations')->entityInsert($entity); + return _content_moderation_create_entity_operations()->entityInsert($entity); } /** * Implements hook_entity_update(). */ function content_moderation_entity_update(EntityInterface $entity) { - return \Drupal::service('content_moderation.entity_operations')->entityUpdate($entity); + return _content_moderation_create_entity_operations()->entityUpdate($entity); } /** @@ -136,7 +172,7 @@ function content_moderation_local_tasks_alter(&$local_tasks) { * Implements hook_form_alter(). */ function content_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) { - return \Drupal::service('content_moderation.entity_type')->bundleFormAlter($form, $form_state, $form_id); + _content_moderation_create_entity_type_info()->bundleFormAlter($form, $form_state, $form_id); } /** @@ -146,21 +182,22 @@ function content_moderation_form_alter(&$form, FormStateInterface $form_state, $ * node title as part of the node content. */ function content_moderation_preprocess_node(&$variables) { - \Drupal::service('content_moderation.content_preprocess')->preprocessNode($variables); + $content_process = new ContentPreprocess(\Drupal::routeMatch()); + $content_process->preprocessNode($variables); } /** * Implements hook_entity_extra_field_info(). */ function content_moderation_entity_extra_field_info() { - return \Drupal::service('content_moderation.entity_type')->entityExtraFieldInfo(); + return _content_moderation_create_entity_type_info()->entityExtraFieldInfo(); } /** * Implements hook_entity_view(). */ function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { - \Drupal::service('content_moderation.entity_operations')->entityView($build, $entity, $display, $view_mode); + _content_moderation_create_entity_operations()->entityView($build, $entity, $display, $view_mode); } /** @@ -171,7 +208,7 @@ function content_moderation_entity_view(array &$build, EntityInterface $entity, * mandatory for any user that wants to moderate things. */ function content_moderation_node_access(NodeInterface $entity, $operation, AccountInterface $account) { - /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */ + /** @var \Drupal\content_moderation\ModerationInformationInterface $modinfo */ $moderation_info = Drupal::service('content_moderation.moderation_information'); if ($operation == 'view') { diff --git a/core/modules/content_moderation/content_moderation.services.yml b/core/modules/content_moderation/content_moderation.services.yml index 5a4eb14..c0c9413 100644 --- a/core/modules/content_moderation/content_moderation.services.yml +++ b/core/modules/content_moderation/content_moderation.services.yml @@ -11,21 +11,6 @@ services: content_moderation.moderation_information: class: Drupal\content_moderation\ModerationInformation arguments: ['@entity_type.manager', '@current_user'] - content_moderation.entity_type: - class: Drupal\content_moderation\EntityTypeInfo - arguments: ['@string_translation', '@content_moderation.moderation_information', '@entity_type.manager'] - content_moderation.entity_operations: - class: Drupal\content_moderation\EntityOperations - arguments: ['@content_moderation.moderation_information', '@entity_type.manager', '@form_builder', '@event_dispatcher', '@content_moderation.revision_tracker'] - content_moderation.inline_editing_disabler: - class: \Drupal\content_moderation\InlineEditingDisabler - arguments: ['@content_moderation.moderation_information'] - content_moderation.content_preprocess: - class: Drupal\content_moderation\ContentPreprocess - arguments: ['@current_route_match'] - content_moderation.views_data: - class: \Drupal\content_moderation\ViewsData - arguments: ['@entity_type.manager', '@content_moderation.moderation_information'] access_check.latest_revision: class: Drupal\content_moderation\Access\LatestRevisionCheck arguments: ['@content_moderation.moderation_information'] diff --git a/core/modules/content_moderation/content_moderation.views.inc b/core/modules/content_moderation/content_moderation.views.inc index 0f59da3..64f767a 100644 --- a/core/modules/content_moderation/content_moderation.views.inc +++ b/core/modules/content_moderation/content_moderation.views.inc @@ -7,9 +7,15 @@ * @ingroup views_module_handlers */ +use Drupal\content_moderation\ViewsData; + /** * Implements hook_views_data(). */ function content_moderation_views_data() { - return \Drupal::service('content_moderation.views_data')->getViewsData(); + $views_data = new ViewsData( + \Drupal::service('entity_type.manager'), + \Drupal::service('content_moderation.moderation_information') + ); + return $views_data->getViewsData(); } diff --git a/core/modules/content_moderation/src/ContentPreprocess.php b/core/modules/content_moderation/src/ContentPreprocess.php index df63a62..b3b7337 100644 --- a/core/modules/content_moderation/src/ContentPreprocess.php +++ b/core/modules/content_moderation/src/ContentPreprocess.php @@ -2,7 +2,7 @@ namespace Drupal\content_moderation; -use Drupal\Core\Routing\CurrentRouteMatch; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\node\Entity\Node; /** @@ -11,17 +11,19 @@ class ContentPreprocess { /** - * @var \Drupal\Core\Routing\CurrentRouteMatch $routeMatch + * The route match service. + * + * @var \Drupal\Core\Routing\RouteMatchInterface $routeMatch */ protected $routeMatch; /** * Constructor. * - * @param \Drupal\Core\Routing\CurrentRouteMatch $route_match + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * Current route match service. */ - public function __construct(CurrentRouteMatch $route_match) { + public function __construct(RouteMatchInterface $route_match) { $this->routeMatch = $route_match; } diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index 6e3760a..e0e1519 100644 --- a/core/modules/content_moderation/src/EntityOperations.php +++ b/core/modules/content_moderation/src/EntityOperations.php @@ -70,8 +70,8 @@ class EntityOperations { public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EventDispatcherInterface $event_dispatcher, RevisionTrackerInterface $tracker) { $this->moderationInfo = $moderation_info; $this->entityTypeManager = $entity_type_manager; - $this->eventDispatcher = $event_dispatcher; $this->formBuilder = $form_builder; + $this->eventDispatcher = $event_dispatcher; $this->tracker = $tracker; } diff --git a/core/modules/content_moderation/src/InlineEditingDisabler.php b/core/modules/content_moderation/src/InlineEditingDisabler.php deleted file mode 100644 index 69239ea..0000000 --- a/core/modules/content_moderation/src/InlineEditingDisabler.php +++ /dev/null @@ -1,52 +0,0 @@ -moderationInfo = $moderation_info; - } - - /** - * Implements hook_entity_view_alter(). - */ - public function entityViewAlter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { - if ($this->moderationInfo->isModeratableEntity($entity) && !$this->moderationInfo->isLatestRevision($entity)) { - // Hide quickedit, because its super confusing for the user to not edit the - // live revision. - unset($build['#attributes']['data-quickedit-entity-id']); - } - } - - /** - * Implements hook_module_implements_alter(). - */ - public function moduleImplementsAlter(&$implementations, $hook) { - if ($hook == 'entity_view_alter') { - // Find the quickedit implementation and move content after it. - unset($implementations['content_moderation']); - $implementations['content_moderation'] = FALSE; - } - } - -} diff --git a/core/modules/content_moderation/src/StateTransitionValidation.php b/core/modules/content_moderation/src/StateTransitionValidation.php index f01f0e6..af5f752 100644 --- a/core/modules/content_moderation/src/StateTransitionValidation.php +++ b/core/modules/content_moderation/src/StateTransitionValidation.php @@ -11,7 +11,7 @@ /** * Validates whether a certain state transition is allowed. */ -class StateTransitionValidation { +class StateTransitionValidation implements StateTransitionValidationInterface { /** * @var \Drupal\Core\Entity\EntityTypeManagerInterface @@ -83,16 +83,7 @@ protected function getPossibleTransitions() { } /** - * Gets a list of states a user may transition an entity to. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity to be transitioned. - * @param \Drupal\Core\Session\AccountInterface $user - * The account that wants to perform a transition. - * - * @return \Drupal\content_moderation\Entity\ModerationState[] - * Returns an array of States to which the specified user may transition the - * entity. + * {@inheritdoc} */ public function getValidTransitionTargets(ContentEntityInterface $entity, AccountInterface $user) { $bundle = $this->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle()); @@ -116,14 +107,7 @@ public function getValidTransitionTargets(ContentEntityInterface $entity, Accoun } /** - * Gets a list of transitions that are legal for this user on this entity. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity to be transitioned. - * @param \Drupal\Core\Session\AccountInterface $user - * The account that wants to perform a transition. - * - * @return ModerationStateTransition[] + * {@inheritdoc} */ public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) { $bundle = $this->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle()); @@ -167,20 +151,7 @@ protected function getTransitionsFrom($state_name) { } /** - * Determines if a user is allowed to transition from one state to another. - * - * This method will also return FALSE if there is no transition between the - * specified states at all. - * - * @param string $from - * The origin state machine name. - * @param string $to - * The destination state machine name. - * @param \Drupal\Core\Session\AccountInterface $user - * The user to validate. - * - * @return bool - * TRUE if the given user may transition between those two states. + * {@inheritdoc} */ public function userMayTransition($from, $to, AccountInterface $user) { if ($transition = $this->getTransitionFromStates($from, $to)) { @@ -215,15 +186,7 @@ protected function getTransitionFromStates($from, $to) { } /** - * Determines a transition allowed. - * - * @param string $from - * The from state. - * @param string $to - * The to state. - * - * @return bool - * Is the transition allowed. + * {@inheritdoc} */ public function isTransitionAllowed($from, $to) { $allowed_transitions = $this->calculatePossibleTransitions(); diff --git a/core/modules/content_moderation/src/StateTransitionValidationInterface.php b/core/modules/content_moderation/src/StateTransitionValidationInterface.php new file mode 100644 index 0000000..016eaf8 --- /dev/null +++ b/core/modules/content_moderation/src/StateTransitionValidationInterface.php @@ -0,0 +1,70 @@ +