FormModeManagerContextualLinks.php in Form mode manager 8.2
File
src/Plugin/Derivative/FormModeManagerContextualLinks.php
View source
<?php
namespace Drupal\form_mode_manager\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\form_mode_manager\EntityRoutingMapManager;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormModeManagerContextualLinks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $formModeManager;
protected $cacheTags;
protected $formModesDefinitionsList;
protected $entityRoutingMap;
public function __construct(FormModeManagerInterface $form_mode_manager, EntityRoutingMapManager $plugin_routes_manager) {
$this->formModeManager = $form_mode_manager;
$this->cacheTags = $form_mode_manager
->getListCacheTags();
$this->formModesDefinitionsList = $form_mode_manager
->getAllFormModesDefinitions();
$this->entityRoutingMap = $plugin_routes_manager;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('form_mode.manager'), $container
->get('plugin.manager.entity_routing_map'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
foreach ($this->formModesDefinitionsList as $entity_type_id => $form_modes) {
$entity_operation_mapping = $this->entityRoutingMap
->createInstance($entity_type_id, [
'entityTypeId' => $entity_type_id,
]);
$edit_route_name = $entity_operation_mapping
->getOperation("edit_form");
$this
->setDefaultTasks($entity_type_id, $edit_route_name);
foreach ($form_modes as $form_mode_name => $form_mode) {
if ($this->formModeManager
->hasActiveFormMode($entity_type_id, $form_mode_name)) {
$form_mode_route_name = "{$edit_route_name}.{$form_mode_name}";
$this->derivatives[$form_mode_route_name]['route_name'] = $form_mode_route_name;
$this->derivatives[$form_mode_route_name]['group'] = $entity_type_id;
$this->derivatives[$form_mode_route_name]['title'] = $this
->t('Edit as @form_mode', [
'@form_mode' => $form_mode['label'],
]);
$this->derivatives[$form_mode_route_name]['cache_tags'] = Cache::mergeTags([
'form_mode_manager_contextual_links',
], $this->cacheTags);
}
}
}
return $this->derivatives;
}
private function setDefaultTasks($entity_type_id, $edit_route_name) {
$this->derivatives[$edit_route_name] = [
'route_name' => $edit_route_name,
'title' => $this
->t('Edit as Default'),
'group' => $entity_type_id,
'cache_tags' => $this->cacheTags,
];
}
}