From 703e9e476cd365ea19b69113f271f65eb2fe9b5a Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 25 Aug 2013 11:41:10 +0100 Subject: [PATCH] Issue #2021063 by Berdir, kiamlaluno, YesCT: Use hook_entity_operation_alter() for manage fields and manage display links. --- .../Entity/ConfigEntityListController.php | 2 +- .../Core/Entity/Annotation/EntityType.php | 11 ++++++ .../CustomBlockTypeListController.php | 24 ++---------- .../custom_block/Entity/CustomBlockType.php | 1 + .../config/Tests/ConfigEntityListTest.php | 2 +- .../Drupal/contact/CategoryListController.php | 29 -------------- .../lib/Drupal/contact/Entity/Category.php | 1 + core/modules/field_ui/field_ui.module | 38 +++++++++++++++++++ .../node/lib/Drupal/node/Entity/NodeType.php | 1 + .../Drupal/node/NodeTypeListController.php | 31 ++------------- .../lib/Drupal/taxonomy/Entity/Vocabulary.php | 1 + .../taxonomy/VocabularyListController.php | 2 +- 12 files changed, 64 insertions(+), 79 deletions(-) diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityListController.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityListController.php index 41c3ba642e9..0eb187a102a 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityListController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityListController.php @@ -52,7 +52,7 @@ class ConfigEntityListController extends EntityListController { 'title' => t('Disable'), 'href' => $uri['path'] . '/disable', 'options' => $uri['options'], - 'weight' => 20, + 'weight' => 40, ); } } diff --git a/core/lib/Drupal/Core/Entity/Annotation/EntityType.php b/core/lib/Drupal/Core/Entity/Annotation/EntityType.php index b8e406131cb..0bf08f0a7d9 100644 --- a/core/lib/Drupal/Core/Entity/Annotation/EntityType.php +++ b/core/lib/Drupal/Core/Entity/Annotation/EntityType.php @@ -154,6 +154,17 @@ class EntityType extends Plugin { */ public $translation = array(); + /** + * The name of the entity type for which bundles are provided. + * + * It can be used by other modules to act accordingly; for example, + * the Field UI module uses it to add operation links to manage fields and + * displays. + * + * @var string + */ + public $bundle_of; + /** * An array describing how the Field API can extract certain information from * objects of this entity type: diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php index efa54368f4a..efa43805f04 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeListController.php @@ -20,26 +20,10 @@ class CustomBlockTypeListController extends ConfigEntityListController { */ public function getOperations(EntityInterface $entity) { $operations = parent::getOperations($entity); - if (module_exists('field_ui')) { - $uri = $entity->uri(); - $operations['manage-fields'] = array( - 'title' => t('Manage fields'), - 'href' => $uri['path'] . '/fields', - 'options' => $uri['options'], - 'weight' => 0, - ); - $operations['manage-form-display'] = array( - 'title' => t('Manage form display'), - 'href' => $uri['path'] . '/form-display', - 'options' => $uri['options'], - 'weight' => 5, - ); - $operations['manage-display'] = array( - 'title' => t('Manage display'), - 'href' => $uri['path'] . '/display', - 'options' => $uri['options'], - 'weight' => 10, - ); + // Place the edit operation after the operations added by field_ui.module + // which have the weights 15, 20, 25. + if (isset($operations['edit'])) { + $operations['edit']['weight'] = 30; } return $operations; } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php index ec51ca46fd2..b74c1d70d84 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php @@ -32,6 +32,7 @@ use Drupal\custom_block\CustomBlockTypeInterface; * "list" = "Drupal\custom_block\CustomBlockTypeListController" * }, * config_prefix = "custom_block.type", + * bundle_of = "custom_block", * entity_keys = { * "id" = "id", * "label" = "label", diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php index ed1f69fa64f..acf2dd0fbea 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php @@ -63,7 +63,7 @@ class ConfigEntityListTest extends WebTestBase { 'title' => t('Disable'), 'href' => $uri['path'] . '/disable', 'options' => $uri['options'], - 'weight' => 20, + 'weight' => 40, ), 'delete' => array ( 'title' => t('Delete'), diff --git a/core/modules/contact/lib/Drupal/contact/CategoryListController.php b/core/modules/contact/lib/Drupal/contact/CategoryListController.php index 26433c44a34..1e626304a57 100644 --- a/core/modules/contact/lib/Drupal/contact/CategoryListController.php +++ b/core/modules/contact/lib/Drupal/contact/CategoryListController.php @@ -14,35 +14,6 @@ use Drupal\Core\Entity\EntityInterface; */ class CategoryListController extends ConfigEntityListController { - /** - * Overrides Drupal\Core\Entity\EntityListController::getOperations(). - */ - public function getOperations(EntityInterface $entity) { - $operations = parent::getOperations($entity); - if ($this->moduleHandler->moduleExists('field_ui')) { - $uri = $entity->uri(); - $operations['manage-fields'] = array( - 'title' => t('Manage fields'), - 'href' => $uri['path'] . '/fields', - 'options' => $uri['options'], - 'weight' => 11, - ); - $operations['manage-form-display'] = array( - 'title' => t('Manage form display'), - 'href' => $uri['path'] . '/form-display', - 'options' => $uri['options'], - 'weight' => 12, - ); - $operations['manage-display'] = array( - 'title' => t('Manage display'), - 'href' => $uri['path'] . '/display', - 'options' => $uri['options'], - 'weight' => 13, - ); - } - return $operations; - } - /** * Overrides Drupal\Core\Entity\EntityListController::buildHeader(). */ diff --git a/core/modules/contact/lib/Drupal/contact/Entity/Category.php b/core/modules/contact/lib/Drupal/contact/Entity/Category.php index 194f12c3e34..7f633fc8d6a 100644 --- a/core/modules/contact/lib/Drupal/contact/Entity/Category.php +++ b/core/modules/contact/lib/Drupal/contact/Entity/Category.php @@ -32,6 +32,7 @@ use Drupal\contact\CategoryInterface; * }, * uri_callback = "contact_category_uri", * config_prefix = "contact.category", + * bundle_of = "contact_message", * entity_keys = { * "id" = "id", * "label" = "label", diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index 8a8674c0a9a..7dff0b6d642 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -5,6 +5,7 @@ * Allows administrators to attach custom fields to fieldable types. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\entity\EntityViewModeInterface; /** @@ -293,6 +294,43 @@ function field_ui_form_node_type_form_alter(&$form, $form_state) { } } +/** + * Implements hook_entity_operation_alter(). + */ +function field_ui_entity_operation_alter(array &$operations, EntityInterface $entity) { + $info = $entity->entityInfo(); + // Add manage fields and display links if this entity type is the bundle + // of another. + if (!empty($info['bundle_of'])) { + $bundle_of = $info['bundle_of']; + $uri = $entity->uri(); + if (user_access('administer '. $bundle_of . ' fields')) { + $operations['manage-fields'] = array( + 'title' => t('Manage fields'), + 'href' => $uri['path'] . '/fields', + 'options' => $uri['options'], + 'weight' => 15, + ); + } + if (user_access('administer '. $bundle_of . ' form display')) { + $operations['manage-form-display'] = array( + 'title' => t('Manage form display'), + 'href' => $uri['path'] . '/form-display', + 'options' => $uri['options'], + 'weight' => 20, + ); + } + if (user_access('administer '. $bundle_of . ' display')) { + $operations['manage-display'] = array( + 'title' => t('Manage display'), + 'href' => $uri['path'] . '/display', + 'options' => $uri['options'], + 'weight' => 25, + ); + } + } +} + /** * Form submission handler for the 'Save and manage fields' button. * diff --git a/core/modules/node/lib/Drupal/node/Entity/NodeType.php b/core/modules/node/lib/Drupal/node/Entity/NodeType.php index d89d4b046b2..cc6021cef7c 100644 --- a/core/modules/node/lib/Drupal/node/Entity/NodeType.php +++ b/core/modules/node/lib/Drupal/node/Entity/NodeType.php @@ -31,6 +31,7 @@ use Drupal\Core\Annotation\Translation; * "list" = "Drupal\node\NodeTypeListController", * }, * config_prefix = "node.type", + * bundle_of = "node", * entity_keys = { * "id" = "type", * "label" = "name", diff --git a/core/modules/node/lib/Drupal/node/NodeTypeListController.php b/core/modules/node/lib/Drupal/node/NodeTypeListController.php index 530ccf79838..11e14141448 100644 --- a/core/modules/node/lib/Drupal/node/NodeTypeListController.php +++ b/core/modules/node/lib/Drupal/node/NodeTypeListController.php @@ -88,33 +88,10 @@ class NodeTypeListController extends ConfigEntityListController implements Entit */ public function getOperations(EntityInterface $entity) { $operations = parent::getOperations($entity); - $uri = $entity->uri(); - if ($this->moduleHandler->moduleExists('field_ui') && user_access('administer node fields')) { - $operations['manage-fields'] = array( - 'title' => t('Manage fields'), - 'href' => $uri['path'] . '/fields', - 'options' => $uri['options'], - 'weight' => 0, - ); - } - if ($this->moduleHandler->moduleExists('field_ui') && user_access('administer node form display')) { - $operations['manage-form-display'] = array( - 'title' => t('Manage form display'), - 'href' => $uri['path'] . '/form-display', - 'options' => $uri['options'], - 'weight' => 5, - ); - } - if ($this->moduleHandler->moduleExists('field_ui') && user_access('administer node display')) { - $operations['manage-display'] = array( - 'title' => t('Manage display'), - 'href' => $uri['path'] . '/display', - 'options' => $uri['options'], - 'weight' => 10, - ); - } - if ($entity->isLocked()) { - unset($operations['delete']); + // Place the edit operation after the operations added by field_ui.module + // which have the weights 15, 20, 25. + if (isset($operations['edit'])) { + $operations['edit']['weight'] = 30; } return $operations; } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php index cd4e0967f9a..00570362255 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php @@ -31,6 +31,7 @@ use Drupal\taxonomy\VocabularyInterface; * } * }, * config_prefix = "taxonomy.vocabulary", + * bundle_of = "taxonomy_term", * entity_keys = { * "id" = "vid", * "label" = "name", diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php index 22a26cf2306..a5480c5f3bc 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyListController.php @@ -45,7 +45,7 @@ class VocabularyListController extends ConfigEntityListController implements For 'title' => t('add terms'), 'href' => $uri['path'] . '/add', 'options' => $uri['options'], - 'weight' => 30, + 'weight' => 10, ); unset($operations['delete']);