Newer
Older
catch
committed
<?php
/**
* @file
* Contains \Drupal\field_ui\Plugin\Derivative\FieldUiLocalTask.
*/
namespace Drupal\field_ui\Plugin\Derivative;
use Drupal\Core\Entity\EntityManagerInterface;
Angie Byron
committed
use Drupal\Component\Plugin\Derivative\DerivativeBase;
catch
committed
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides local task definitions for all entity bundles.
*/
Angie Byron
committed
class FieldUiLocalTask extends DerivativeBase implements ContainerDerivativeInterface {
catch
committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The entity manager
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Creates an FieldUiLocalTask object.
*
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
*/
public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $translation_manager) {
$this->routeProvider = $route_provider;
$this->entityManager = $entity_manager;
$this->translationManager = $translation_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('router.route_provider'),
$container->get('entity.manager'),
$container->get('string_translation')
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getDerivativeDefinitions($base_plugin_definition) {
catch
committed
$this->derivatives = array();
Alex Pott
committed
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type->isFieldable() && $entity_type->hasLinkTemplate('admin-form')) {
$this->derivatives["overview_$entity_type_id"] = array(
'route_name' => "field_ui.overview_$entity_type_id",
catch
committed
'weight' => 1,
'title' => $this->t('Manage fields'),
Alex Pott
committed
'base_route' => "field_ui.overview_$entity_type_id",
catch
committed
);
// 'Manage form display' tab.
Alex Pott
committed
$this->derivatives["form_display_overview_$entity_type_id"] = array(
'route_name' => "field_ui.form_display_overview_$entity_type_id",
catch
committed
'weight' => 2,
'title' => $this->t('Manage form display'),
Alex Pott
committed
'base_route' => "field_ui.overview_$entity_type_id",
catch
committed
);
// 'Manage display' tab.
Alex Pott
committed
$this->derivatives["display_overview_$entity_type_id"] = array(
'route_name' => "field_ui.display_overview_$entity_type_id",
catch
committed
'weight' => 3,
'title' => $this->t('Manage display'),
Alex Pott
committed
'base_route' => "field_ui.overview_$entity_type_id",
catch
committed
);
// Field instance edit tab.
Alex Pott
committed
$this->derivatives["instance_edit_$entity_type_id"] = array(
'route_name' => "field_ui.instance_edit_$entity_type_id",
catch
committed
'title' => $this->t('Edit'),
Alex Pott
committed
'base_route' => "field_ui.instance_edit_$entity_type_id",
catch
committed
);
// Field settings tab.
Alex Pott
committed
$this->derivatives["field_edit_$entity_type_id"] = array(
'route_name' => "field_ui.field_edit_$entity_type_id",
catch
committed
'title' => $this->t('Field settings'),
Alex Pott
committed
'base_route' => "field_ui.instance_edit_$entity_type_id",
catch
committed
);
// View and form modes secondary tabs.
// The same base $path for the menu item (with a placeholder) can be
// used for all bundles of a given entity type; but depending on
// administrator settings, each bundle has a different set of view
// modes available for customisation. So we define menu items for all
// view modes, and use a route requirement to determine which ones are
// actually visible for a given bundle.
Alex Pott
committed
$this->derivatives['field_form_display_default_' . $entity_type_id] = array(
catch
committed
'title' => 'Default',
Alex Pott
committed
'route_name' => "field_ui.form_display_overview_$entity_type_id",
'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
Alex Pott
committed
'weight' => -1,
catch
committed
);
Alex Pott
committed
$this->derivatives['field_display_default_' . $entity_type_id] = array(
catch
committed
'title' => 'Default',
Alex Pott
committed
'route_name' => "field_ui.display_overview_$entity_type_id",
'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
Alex Pott
committed
'weight' => -1,
catch
committed
);
// One local task for each form mode.
$weight = 0;
foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
Alex Pott
committed
$this->derivatives['field_form_display_' . $form_mode . '_' . $entity_type_id] = array(
catch
committed
'title' => $form_mode_info['label'],
Alex Pott
committed
'route_name' => "field_ui.form_display_overview_form_mode_$entity_type_id",
catch
committed
'route_parameters' => array(
'form_mode_name' => $form_mode,
),
Alex Pott
committed
'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
catch
committed
'weight' => $weight++,
);
}
// One local task for each view mode.
$weight = 0;
foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
Alex Pott
committed
$this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = array(
catch
committed
'title' => $form_mode_info['label'],
Alex Pott
committed
'route_name' => "field_ui.display_overview_view_mode_$entity_type_id",
catch
committed
'route_parameters' => array(
'view_mode_name' => $view_mode,
),
Alex Pott
committed
'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
catch
committed
'weight' => $weight++,
);
}
}
}
foreach ($this->derivatives as &$entry) {
$entry += $base_plugin_definition;
}
return $this->derivatives;
}
/**
Angie Byron
committed
* Alters the base_route definition for field_ui local tasks.
catch
committed
*
* @param array $local_tasks
* An array of local tasks plugin definitions, keyed by plugin ID.
*/
public function alterLocalTasks(&$local_tasks) {
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
Alex Pott
committed
if ($entity_info->isFieldable() && $entity_info->hasLinkTemplate('admin-form')) {
$admin_form = $entity_info->getLinkTemplate('admin-form');
$local_tasks["field_ui.fields:overview_$entity_type"]['base_route'] = $admin_form;
$local_tasks["field_ui.fields:form_display_overview_$entity_type"]['base_route'] = $admin_form;
$local_tasks["field_ui.fields:display_overview_$entity_type"]['base_route'] = $admin_form;
$local_tasks["field_ui.fields:field_form_display_default_$entity_type"]['base_route'] = $admin_form;
$local_tasks["field_ui.fields:field_display_default_$entity_type"]['base_route'] = $admin_form;
Angie Byron
committed
foreach ($this->entityManager->getFormModes($entity_type) as $form_mode => $form_mode_info) {
Alex Pott
committed
$local_tasks['field_ui.fields:field_form_display_' . $form_mode . '_' . $entity_type]['base_route'] = $admin_form;
Angie Byron
committed
}
foreach ($this->entityManager->getViewModes($entity_type) as $view_mode => $form_mode_info) {
Alex Pott
committed
$local_tasks['field_ui.fields:field_display_' . $view_mode . '_' . $entity_type]['base_route'] = $admin_form;
catch
committed
}
}
}
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
}
}