diff --git a/core/lib/Drupal/Core/Entity/EntityAccessController.php b/core/lib/Drupal/Core/Entity/EntityAccessController.php index f18cd2c..d17a999 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessController.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php @@ -17,7 +17,7 @@ /** * Defines a default implementation for entity access controllers. */ -class EntityAccessController implements EntityAccessControllerInterface { +class EntityAccessController extends EntityControllerBase implements EntityAccessControllerInterface { /** * Stores calculcated access check results. @@ -41,13 +41,6 @@ class EntityAccessController implements EntityAccessControllerInterface { protected $entityInfo; /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * Constructs an access controller instance. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info @@ -79,8 +72,8 @@ public function access(EntityInterface $entity, $operation, $langcode = Language // - No modules say to deny access. // - At least one module says to grant access. $access = array_merge( - $this->moduleHandler->invokeAll('entity_access', array($entity, $operation, $account, $langcode)), - $this->moduleHandler->invokeAll($entity->entityType() . '_access', array($entity, $operation, $account, $langcode)) + $this->moduleHandler()->invokeAll('entity_access', array($entity, $operation, $account, $langcode)), + $this->moduleHandler()->invokeAll($entity->entityType() . '_access', array($entity, $operation, $account, $langcode)) ); if (($return = $this->processAccessHookResults($access)) === NULL) { @@ -226,8 +219,8 @@ public function createAccess($entity_bundle = NULL, AccountInterface $account = // - No modules say to deny access. // - At least one module says to grant access. $access = array_merge( - $this->moduleHandler->invokeAll('entity_create_access', array($account, $context['langcode'])), - $this->moduleHandler->invokeAll($this->entityType . '_create_access', array($account, $context['langcode'])) + $this->moduleHandler()->invokeAll('entity_create_access', array($account, $context['langcode'])), + $this->moduleHandler()->invokeAll($this->entityType . '_create_access', array($account, $context['langcode'])) ); if (($return = $this->processAccessHookResults($access)) === NULL) { @@ -284,14 +277,6 @@ protected function prepareUser(AccountInterface $account = NULL) { /** * {@inheritdoc} */ - public function setModuleHandler(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; - return $this; - } - - /** - * {@inheritdoc} - */ public function fieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account = NULL, FieldItemListInterface $items = NULL) { $account = $this->prepareUser($account); @@ -301,9 +286,9 @@ public function fieldAccess($operation, FieldDefinitionInterface $field_definiti // Invoke hook and collect grants/denies for field access from other // modules. Our default access flag is masked under the ':default' key. $grants = array(':default' => $default); - $hook_implementations = $this->moduleHandler->getImplementations('entity_field_access'); + $hook_implementations = $this->moduleHandler()->getImplementations('entity_field_access'); foreach ($hook_implementations as $module) { - $grants = array_merge($grants, array($module => $this->moduleHandler->invoke($module, 'entity_field_access', array($operation, $field_definition, $account, $items)))); + $grants = array_merge($grants, array($module => $this->moduleHandler()->invoke($module, 'entity_field_access', array($operation, $field_definition, $account, $items)))); } // Also allow modules to alter the returned grants/denies. @@ -313,7 +298,7 @@ public function fieldAccess($operation, FieldDefinitionInterface $field_definiti 'items' => $items, 'account' => $account, ); - $this->moduleHandler->alter('entity_field_access', $grants, $context); + $this->moduleHandler()->alter('entity_field_access', $grants, $context); // One grant being FALSE is enough to deny access immediately. if (in_array(FALSE, $grants, TRUE)) { diff --git a/core/lib/Drupal/Core/Entity/EntityControllerBase.php b/core/lib/Drupal/Core/Entity/EntityControllerBase.php new file mode 100644 index 0000000..f468303 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityControllerBase.php @@ -0,0 +1,86 @@ +translationManager) { + $this->translationManager = \Drupal::translation(); + } + return $this->translationManager; + } + + /** + * Sets the translation manager for this controller. + * + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * The translation manager. + * + * @return $this + */ + public function setTranslationManager(TranslationInterface $translation_manager) { + $this->translationManager = $translation_manager; + return $this; + } + + /** + * Returns the module handler. + * + * @return \Drupal\Core\Extension\ModuleHandlerInterface + * The module handler. + */ + protected function moduleHandler() { + if (!$this->moduleHandler) { + $this->moduleHandler = \Drupal::moduleHandler(); + } + return $this->moduleHandler; + } + + /** + * Sets the module handler for this controller. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * + * @return $this + */ + public function setModuleHandler(ModuleHandlerInterface $module_handler) { + $this->moduleHandler = $module_handler; + return $this; + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityListController.php b/core/lib/Drupal/Core/Entity/EntityListController.php index 303e378..b169e2f 100644 --- a/core/lib/Drupal/Core/Entity/EntityListController.php +++ b/core/lib/Drupal/Core/Entity/EntityListController.php @@ -15,7 +15,7 @@ /** * Provides a generic implementation of an entity list controller. */ -class EntityListController implements EntityListControllerInterface, EntityControllerInterface { +class EntityListController extends EntityControllerBase implements EntityListControllerInterface, EntityControllerInterface { /** * The entity storage controller class. @@ -25,13 +25,6 @@ class EntityListController implements EntityListControllerInterface, EntityContr protected $storage; /** - * The module handler to invoke hooks on. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * The entity type name. * * @var string @@ -46,13 +39,6 @@ class EntityListController implements EntityListControllerInterface, EntityContr protected $entityInfo; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) { @@ -173,7 +159,7 @@ public function buildRow(EntityInterface $entity) { public function buildOperations(EntityInterface $entity) { // Retrieve and sort operations. $operations = $this->getOperations($entity); - $this->moduleHandler->alter('entity_operation', $operations, $entity); + $this->moduleHandler()->alter('entity_operation', $operations, $entity); uasort($operations, 'drupal_sort_weight'); $build = array( '#type' => 'operations', @@ -211,33 +197,7 @@ public function render() { * See the t() documentation for details. */ protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - - /** - * Sets the translation manager for this list. - * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. - * - * @return $this - */ - public function setTranslationManager(TranslationInterface $translation_manager) { - $this->translationManager = $translation_manager; - return $this; - } - - /** - * Sets the module handler for this list. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. - * - * @return $this - */ - public function setModuleHandler(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; - return $this; + return $this->translationManager()->translate($string, $args, $options); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php index b74cfba..7db28fc 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerBase.php @@ -12,7 +12,7 @@ /** * A base entity storage controller class. */ -abstract class EntityStorageControllerBase implements EntityStorageControllerInterface, EntityControllerInterface { +abstract class EntityStorageControllerBase extends EntityControllerBase implements EntityStorageControllerInterface, EntityControllerInterface { /** * Static cache of entities. @@ -45,13 +45,6 @@ protected $entityInfo; /** - * The module handler to invoke hooks on. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * Name of the entity's ID field in the entity database table. * * @var string @@ -157,9 +150,9 @@ protected function cacheSet($entities) { */ protected function invokeHook($hook, EntityInterface $entity) { // Invoke the hook. - $this->moduleHandler->invokeAll($this->entityType . '_' . $hook, array($entity)); + $this->moduleHandler()->invokeAll($this->entityType . '_' . $hook, array($entity)); // Invoke the respective entity-level hook. - $this->moduleHandler->invokeAll('entity_' . $hook, array($entity, $this->entityType)); + $this->moduleHandler()->invokeAll('entity_' . $hook, array($entity, $this->entityType)); } /** @@ -172,12 +165,12 @@ protected function postLoad(array &$queried_entities) { $entity_class = $this->entityInfo->getClass(); $entity_class::postLoad($this, $queried_entities); // Call hook_entity_load(). - foreach ($this->moduleHandler->getImplementations('entity_load') as $module) { + foreach ($this->moduleHandler()->getImplementations('entity_load') as $module) { $function = $module . '_entity_load'; $function($queried_entities, $this->entityType); } // Call hook_TYPE_load(). - foreach ($this->moduleHandler->getImplementations($this->entityType . '_load') as $module) { + foreach ($this->moduleHandler()->getImplementations($this->entityType . '_load') as $module) { $function = $module . '_' . $this->entityType . '_load'; $function($queried_entities); } @@ -209,17 +202,4 @@ public function loadByProperties(array $values = array()) { return $result ? $this->loadMultiple($result) : array(); } - /** - * Sets the module handler for this storage. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. - * - * @return $this - */ - public function setModuleHandler(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; - return $this; - } - } diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index b661fb4..81e6c38 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -16,7 +16,7 @@ /** * Base class for entity view controllers. */ -class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderInterface { +class EntityViewBuilder extends EntityControllerBase implements EntityControllerInterface, EntityViewBuilderInterface { /** * The type of entities for which this controller is instantiated. @@ -40,13 +40,6 @@ class EntityViewBuilder implements EntityControllerInterface, EntityViewBuilderI protected $entityManager; /** - * The module handler to invoke hooks on. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * The cache bin used to store the render cache. * * @todo Defaults to 'cache' for now, until http://drupal.org/node/1194136 is @@ -293,17 +286,4 @@ protected function isViewModeCacheable($view_mode) { return !empty($view_modes_info[$view_mode]['cache']); } - /** - * Sets the module handler for this view builder. - * - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. - * - * @return $this - */ - public function setModuleHandler(ModuleHandlerInterface $module_handler) { - $this->moduleHandler = $module_handler; - return $this; - } - } diff --git a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php index 105fcb1..73cabaa 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -58,7 +58,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $build[$entity_id] = array(); } - $this->moduleHandler->alter(array('block_view', "block_view_$base_id"), $build[$entity_id], $plugin); + $this->moduleHandler()->alter(array('block_view', "block_view_$base_id"), $build[$entity_id], $plugin); // @todo Remove after fixing http://drupal.org/node/1989568. $build[$entity_id]['#block'] = $entity;