Commit fe33c4b9 authored by alexpott's avatar alexpott

Issue #2781207 by timmillwood, dawehner: Use class resolver for abstracted...

Issue #2781207 by timmillwood, dawehner: Use class resolver for abstracted Content Moderation classes
parent 92c311c2
......@@ -36,81 +36,67 @@ 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'),
\Drupal::service('current_user')
);
}
/**
* Implements hook_entity_base_field_info().
*/
function content_moderation_entity_base_field_info(EntityTypeInterface $entity_type) {
return _content_moderation_create_entity_type_info()->entityBaseFieldInfo($entity_type);
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityBaseFieldInfo($entity_type);
}
/**
* Implements hook_entity_type_alter().
*/
function content_moderation_entity_type_alter(array &$entity_types) {
_content_moderation_create_entity_type_info()->entityTypeAlter($entity_types);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityTypeAlter($entity_types);
}
/**
* Implements hook_entity_operation().
*/
function content_moderation_entity_operation(EntityInterface $entity) {
return _content_moderation_create_entity_type_info()->entityOperation($entity);
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityOperation($entity);
}
/**
* Sets required flag based on enabled state.
*/
function content_moderation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $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('content_moderation.revision_tracker')
);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityBundleFieldInfoAlter($fields, $entity_type, $bundle);
}
/**
* Implements hook_entity_presave().
*/
function content_moderation_entity_presave(EntityInterface $entity) {
return _content_moderation_create_entity_operations()->entityPresave($entity);
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityOperations::class)
->entityPresave($entity);
}
/**
* Implements hook_entity_insert().
*/
function content_moderation_entity_insert(EntityInterface $entity) {
return _content_moderation_create_entity_operations()->entityInsert($entity);
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityOperations::class)
->entityInsert($entity);
}
/**
* Implements hook_entity_update().
*/
function content_moderation_entity_update(EntityInterface $entity) {
return _content_moderation_create_entity_operations()->entityUpdate($entity);
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityOperations::class)
->entityUpdate($entity);
}
/**
......@@ -133,7 +119,9 @@ function content_moderation_local_tasks_alter(&$local_tasks) {
* Implements hook_form_alter().
*/
function content_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_content_moderation_create_entity_type_info()->formAlter($form, $form_state, $form_id);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->formAlter($form, $form_state, $form_id);
}
/**
......@@ -143,22 +131,27 @@ function content_moderation_form_alter(&$form, FormStateInterface $form_state, $
* node title as part of the node content.
*/
function content_moderation_preprocess_node(&$variables) {
$content_process = new ContentPreprocess(\Drupal::routeMatch());
$content_process->preprocessNode($variables);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(ContentPreprocess::class)
->preprocessNode($variables);
}
/**
* Implements hook_entity_extra_field_info().
*/
function content_moderation_entity_extra_field_info() {
return _content_moderation_create_entity_type_info()->entityExtraFieldInfo();
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityExtraFieldInfo();
}
/**
* Implements hook_entity_view().
*/
function content_moderation_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
_content_moderation_create_entity_operations()->entityView($build, $entity, $display, $view_mode);
\Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityOperations::class)
->entityView($build, $entity, $display, $view_mode);
}
/**
......
......@@ -2,13 +2,15 @@
namespace Drupal\content_moderation;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Determines whether a route is the "Latest version" tab of a node.
*/
class ContentPreprocess {
class ContentPreprocess implements ContainerInjectionInterface {
/**
* The route match service.
......@@ -27,6 +29,15 @@ public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_route_match')
);
}
/**
* Wrapper for hook_preprocess_HOOK().
*
......
......@@ -3,6 +3,7 @@
namespace Drupal\content_moderation;
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
......@@ -10,11 +11,12 @@
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\content_moderation\Form\EntityModerationForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a class for reacting to entity events.
*/
class EntityOperations {
class EntityOperations implements ContainerInjectionInterface {
/**
* The Moderation Information service.
......@@ -63,6 +65,18 @@ public function __construct(ModerationInformationInterface $moderation_info, Ent
$this->tracker = $tracker;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('content_moderation.moderation_information'),
$container->get('entity_type.manager'),
$container->get('form_builder'),
$container->get('content_moderation.revision_tracker')
);
}
/**
* Determines the default moderation state on load for an entity.
*
......
......@@ -6,6 +6,7 @@
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
......@@ -22,6 +23,7 @@
use Drupal\content_moderation\Form\BundleModerationConfigurationForm;
use Drupal\content_moderation\Routing\EntityModerationRouteProvider;
use Drupal\content_moderation\Routing\EntityTypeModerationRouteProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Manipulates entity type information.
......@@ -29,7 +31,7 @@
* This class contains primarily bridged hooks for compile-time or
* cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
*/
class EntityTypeInfo {
class EntityTypeInfo implements ContainerInjectionInterface {
use StringTranslationTrait;
......@@ -83,6 +85,19 @@ public function __construct(TranslationInterface $translation, ModerationInforma
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('string_translation'),
$container->get('content_moderation.moderation_information'),
$container->get('entity_type.manager'),
$container->get('current_user')
);
}
/**
* Adds Moderation configuration to appropriate entity types.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment