Commit 703e9e47 authored by alexpott's avatar alexpott

Issue #2021063 by Berdir, kiamlaluno, YesCT: Use hook_entity_operation_alter()...

Issue #2021063 by Berdir, kiamlaluno, YesCT: Use hook_entity_operation_alter() for manage fields and manage display links.
parent 6aa975ca
......@@ -52,7 +52,7 @@ public function getOperations(EntityInterface $entity) {
'title' => t('Disable'),
'href' => $uri['path'] . '/disable',
'options' => $uri['options'],
'weight' => 20,
'weight' => 40,
);
}
}
......
......@@ -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:
......
......@@ -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;
}
......
......@@ -32,6 +32,7 @@
* "list" = "Drupal\custom_block\CustomBlockTypeListController"
* },
* config_prefix = "custom_block.type",
* bundle_of = "custom_block",
* entity_keys = {
* "id" = "id",
* "label" = "label",
......
......@@ -63,7 +63,7 @@ function testList() {
'title' => t('Disable'),
'href' => $uri['path'] . '/disable',
'options' => $uri['options'],
'weight' => 20,
'weight' => 40,
),
'delete' => array (
'title' => t('Delete'),
......
......@@ -14,35 +14,6 @@
*/
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().
*/
......
......@@ -32,6 +32,7 @@
* },
* uri_callback = "contact_category_uri",
* config_prefix = "contact.category",
* bundle_of = "contact_message",
* entity_keys = {
* "id" = "id",
* "label" = "label",
......
......@@ -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.
*
......
......@@ -31,6 +31,7 @@
* "list" = "Drupal\node\NodeTypeListController",
* },
* config_prefix = "node.type",
* bundle_of = "node",
* entity_keys = {
* "id" = "type",
* "label" = "name",
......
......@@ -88,33 +88,10 @@ public function buildRow(EntityInterface $entity) {
*/
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;
}
......
......@@ -31,6 +31,7 @@
* }
* },
* config_prefix = "taxonomy.vocabulary",
* bundle_of = "taxonomy_term",
* entity_keys = {
* "id" = "vid",
* "label" = "name",
......
......@@ -45,7 +45,7 @@ public function getOperations(EntityInterface $entity) {
'title' => t('add terms'),
'href' => $uri['path'] . '/add',
'options' => $uri['options'],
'weight' => 30,
'weight' => 10,
);
unset($operations['delete']);
......
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