You are here

class FormModeManagerContextualLinks in Form mode manager 8.2

Provides contextual links definitions for all entity bundles.

Hierarchy

Expanded class hierarchy of FormModeManagerContextualLinks

1 string reference to 'FormModeManagerContextualLinks'
form_mode_manager.links.contextual.yml in ./form_mode_manager.links.contextual.yml
form_mode_manager.links.contextual.yml

File

src/Plugin/Derivative/FormModeManagerContextualLinks.php, line 16

Namespace

Drupal\form_mode_manager\Plugin\Derivative
View source
class FormModeManagerContextualLinks extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity display repository.
   *
   * @var \Drupal\form_mode_manager\FormModeManagerInterface
   */
  protected $formModeManager;

  /**
   * The Form Mode Manager service.
   *
   * @var string[]
   */
  protected $cacheTags;

  /**
   * The Form Mode Manager service.
   *
   * @var array
   */
  protected $formModesDefinitionsList;

  /**
   * The Routes Manager Plugin.
   *
   * @var \Drupal\form_mode_manager\EntityRoutingMapManager
   */
  protected $entityRoutingMap;

  /**
   * Constructs a new FormModeManagerContextualLinks object.
   *
   * @param \Drupal\form_mode_manager\FormModeManagerInterface $form_mode_manager
   *   The form mode manager.
   * @param \Drupal\form_mode_manager\EntityRoutingMapManager $plugin_routes_manager
   *   Plugin EntityRoutingMap to retrieve entity form operation routes.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('form_mode.manager'), $container
      ->get('plugin.manager.entity_routing_map'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];
    foreach ($this->formModesDefinitionsList as $entity_type_id => $form_modes) {

      /** @var \Drupal\form_mode_manager\EntityRoutingMapBase $entity_operation_mapping */
      $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;
  }

  /**
   * Set the default Contextual link on each entities.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   * @param string $edit_route_name
   *   The edit route name of given entity.
   */
  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,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
FormModeManagerContextualLinks::$cacheTags protected property The Form Mode Manager service.
FormModeManagerContextualLinks::$entityRoutingMap protected property The Routes Manager Plugin.
FormModeManagerContextualLinks::$formModeManager protected property The entity display repository.
FormModeManagerContextualLinks::$formModesDefinitionsList protected property The Form Mode Manager service.
FormModeManagerContextualLinks::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
FormModeManagerContextualLinks::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
FormModeManagerContextualLinks::setDefaultTasks private function Set the default Contextual link on each entities.
FormModeManagerContextualLinks::__construct public function Constructs a new FormModeManagerContextualLinks object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.