View source
<?php
namespace Drupal\form_mode_manager\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class FormModeManagerLocalTasks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
const BLOCK_CONTENT_CANONICAL = 'entity.block_content.canonical';
protected $formModeManager;
protected $cacheTags;
protected $formModesDefinitionsList;
public function __construct(FormModeManagerInterface $form_mode_manager) {
$this->formModeManager = $form_mode_manager;
$this->cacheTags = $form_mode_manager
->getListCacheTags();
$this->formModesDefinitionsList = $form_mode_manager
->getAllFormModesDefinitions();
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('form_mode.manager'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
foreach ($this->formModesDefinitionsList as $entity_type_id => $form_modes) {
$this
->setDefaultTasks($entity_type_id);
foreach ($form_modes as $form_mode_name => $form_mode) {
if ($this->formModeManager
->hasActiveFormMode($entity_type_id, $form_mode_name)) {
$this
->setFormModesTasks($form_mode, $entity_type_id, $this->formModeManager
->tasksIsPrimary($entity_type_id));
}
}
}
foreach ($this->derivatives as &$entry) {
$entry += $base_plugin_definition;
}
return $this->derivatives;
}
private function blockContentEnhancer($element_name, $entity_type_id, $is_default_task = TRUE) {
if ('block_content' === $entity_type_id) {
if ($is_default_task) {
$this->derivatives[$element_name]['route_name'] = self::BLOCK_CONTENT_CANONICAL;
}
$this->derivatives[$element_name]['parent_id'] = self::BLOCK_CONTENT_CANONICAL;
}
}
private function setDefaultTasks($entity_type_id) {
$this->derivatives["form_mode_manager.{$entity_type_id}.default.task_tab"] = [
'route_name' => "entity.{$entity_type_id}.edit_form",
'title' => $this
->t('Edit as Default'),
'parent_id' => "entity.{$entity_type_id}.edit_form",
'cache_tags' => $this->cacheTags,
];
$element_name = "form_mode_manager.{$entity_type_id}.default.task_tab";
$this
->blockContentEnhancer($element_name, $entity_type_id);
}
private function setFormModesTasks(array $form_mode, $entity_type_id, $is_primary_tasks) {
$this
->setFormModesTasksBase($form_mode, $entity_type_id);
$this
->setUserRegisterTasks($form_mode, $entity_type_id);
$element_name = "form_mode_manager.{$form_mode['id']}.task_tab";
$this
->blockContentEnhancer($element_name, $entity_type_id, FALSE);
if ($is_primary_tasks) {
$this->derivatives[$element_name]['base_route'] = "entity.{$entity_type_id}.canonical";
unset($this->derivatives[$element_name]['parent_id']);
}
}
private function setUserRegisterTasks(array $form_mode, $entity_type_id) {
if ('user' === $entity_type_id) {
$this->derivatives["form_mode_manager.{$form_mode['id']}.register_task_tab"] = [
'route_name' => "user.register.{$this->formModeManager->getFormModeMachineName($form_mode['id'])}",
'title' => $this
->t('Create new account as @form_mode', [
'@form_mode' => $form_mode['label'],
]),
'base_route' => "user.page",
];
}
}
private function setFormModesTasksBase(array $form_mode, $entity_type_id) {
$this->derivatives["form_mode_manager.{$form_mode['id']}.task_tab"] = [
'route_name' => "entity.{$entity_type_id}.edit_form.{$this->formModeManager->getFormModeMachineName($form_mode['id'])}",
'title' => $this
->t('Edit as @form_mode', [
'@form_mode' => $form_mode['label'],
]),
'parent_id' => "entity.{$entity_type_id}.edit_form",
'cache_tags' => $this->cacheTags,
];
}
}