diff --git a/core/includes/entity.api.php b/core/includes/entity.api.php index a509926..c12e4db 100644 --- a/core/includes/entity.api.php +++ b/core/includes/entity.api.php @@ -11,22 +11,43 @@ */ /** - * Alter the entity type definition. + * Add to entity type definitions. + * + * Modules may implement this hook to add information to defined entity types. + * + * @param array $entity_info + * An associative array of all entity type definitions, keyed by the entity + * type name. Passed by reference. + * + * @see \Drupal\Core\Entity\Entity + * @see \Drupal\Core\Entity\EntityManager + * @see entity_get_info() + */ +function hook_entity_info(&$entity_info) { + // Add the 'Print' view mode for nodes. + $entity_info['node']['view_modes']['print'] = array( + 'label' => t('Print'), + 'custom_settings' => FALSE, + ); +} + +/** + * Alter the entity type definitions. * * Modules may implement this hook to alter the information that defines an - * entity. All properties that are available in + * entity type. All properties that are available in * \Drupal\Core\Entity\EntityManager can be altered here. * + * Do not use this hook to add information to entity types. Use + * hook_entity_info() for that instead. + * * @param array $entity_info * An associative array of all entity type definitions, keyed by the entity - * type name. + * type name. Passed by reference. * * @see \Drupal\Core\Entity\Entity * @see \Drupal\Core\Entity\EntityManager * @see entity_get_info() - * - * @todo Allow a module to add its bundles and view modes before other modules - * alter the definition. */ function hook_entity_info_alter(&$entity_info) { // Set the controller class for nodes to an alternate implementation of the diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index a75dc44..e34adb6 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; +use Drupal\Core\Plugin\Discovery\InfoHookDecorator; use Drupal\Core\Cache\CacheBackendInterface; /** @@ -222,7 +223,7 @@ class EntityManager extends PluginManagerBase { */ public function __construct() { // Allow the plugin definition to be altered by hook_entity_info_alter(). - $this->discovery = new AlterDecorator(new AnnotatedClassDiscovery('Core', 'Entity'), 'entity_info'); + $this->discovery = new AlterDecorator(new InfoHookDecorator(new AnnotatedClassDiscovery('Core', 'Entity'), 'entity_info'), 'entity_info'); $this->factory = new DefaultFactory($this); // Entity type plugins includes translated strings, so each language is diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php index d4ce40b..8bcee3f 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php @@ -35,7 +35,7 @@ class AlterDecorator implements DiscoveryInterface { * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated * The object implementing DiscoveryInterface that is being decorated. * @param string $hook - * The name of the alter hook that will be implemented by this discovery instance. + * The name of the alter hook that will be used by this discovery instance. */ public function __construct(DiscoveryInterface $decorated, $hook) { $this->decorated = $decorated; diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php b/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php similarity index 71% copy from core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php copy to core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php index d4ce40b..7ba8913 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AlterDecorator.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/InfoHookDecorator.php @@ -2,23 +2,17 @@ /** * @file - * Definition of Drupal\Core\Plugin\Discovery\AlterDiscoveryDecorator. -*/ + * Contains Drupal\Core\Plugin\Discovery\InfoHookDecorator. + */ namespace Drupal\Core\Plugin\Discovery; use Drupal\Component\Plugin\Discovery\DiscoveryInterface; /** - * Enables altering of discovered plugin definitions. + * Allows info hook implementations to enhance discovered plugin definitions. */ -class AlterDecorator implements DiscoveryInterface { - /** - * The name of the alter hook that will be implemented by this discovery instance. - * - * @var string - */ - protected $hook; +class InfoHookDecorator implements DiscoveryInterface { /** * The Discovery object being decorated. @@ -28,14 +22,19 @@ class AlterDecorator implements DiscoveryInterface { protected $decorated; /** - * Constructs a Drupal\Core\Plugin\Discovery\AlterDecorator object. + * The name of the info hook that will be implemented by this discovery instance. * - * It uses the DiscoveryInterface object it should decorate. + * @var string + */ + protected $hook; + + /** + * Constructs a InfoHookDecorator object. * * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated * The object implementing DiscoveryInterface that is being decorated. * @param string $hook - * The name of the alter hook that will be implemented by this discovery instance. + * The name of the info hook to be invoked by this discovery instance. */ public function __construct(DiscoveryInterface $decorated, $hook) { $this->decorated = $decorated; @@ -50,13 +49,15 @@ public function getDefinition($plugin_id) { return isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL; } - /** * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). */ public function getDefinitions() { $definitions = $this->decorated->getDefinitions(); - drupal_alter($this->hook, $definitions); + foreach (module_implements($this->hook) as $module) { + $function = $module . '_' . $this->hook; + $function($definitions); + } return $definitions; } @@ -66,4 +67,5 @@ public function getDefinitions() { public function __call($method, $args) { return call_user_func_array(array($this->decorated, $method), $args); } + } diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 4c514af..062097a 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -246,9 +246,9 @@ function book_admin_paths() { } /** - * Implements hook_entity_info_alter(). + * Implements hook_entity_info(). */ -function book_entity_info_alter(&$info) { +function book_entity_info(&$info) { // Add the 'Print' view mode for nodes. $info['node']['view_modes']['print'] = array( 'label' => t('Print'), diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 87c1798..24f48ad 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -98,9 +98,9 @@ function comment_help($path, $arg) { } /** - * Implements hook_entity_info_alter(). + * Implements hook_entity_info(). */ -function comment_entity_info_alter(&$info) { +function comment_entity_info(&$info) { foreach (node_type_get_names() as $type => $name) { $info['comment']['bundles']['comment_node_' . $type] = array( 'label' => t('@node_type comment', array('@node_type' => $name)), diff --git a/core/modules/node/node.module b/core/modules/node/node.module index a6a64ae..e6251ab 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -192,9 +192,9 @@ function node_cron() { } /** - * Implements hook_entity_info_alter(). + * Implements hook_entity_info(). */ -function node_entity_info_alter(&$info) { +function node_entity_info(&$info) { // Add a translation handler for fields if the language module is enabled. if (module_exists('language')) { $info['node']['translation']['node'] = TRUE; diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 8da637f..d8ea5b7 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -106,9 +106,9 @@ function taxonomy_permission() { } /** - * Implements hook_entity_info_alter(). + * Implements hook_entity_info(). */ -function taxonomy_entity_info_alter(&$info) { +function taxonomy_entity_info(&$info) { foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) { $info['taxonomy_term']['bundles'][$machine_name] = array( 'label' => $vocabulary->name,