You are here

class DefaultEntityLocalTaskProvider in Entity API 8

Provides a set of tasks to view, edit and duplicate an entity.

Hierarchy

Expanded class hierarchy of DefaultEntityLocalTaskProvider

File

src/Menu/DefaultEntityLocalTaskProvider.php, line 14

Namespace

Drupal\entity\Menu
View source
class DefaultEntityLocalTaskProvider implements EntityLocalTaskProviderInterface, EntityHandlerInterface {
  use StringTranslationTrait;

  /**
   * Constructs a DefaultEntityLocalTaskProvider object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(EntityTypeInterface $entity_type, TranslationInterface $string_translation) {
    $this
      ->setStringTranslation($string_translation);
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildLocalTasks(EntityTypeInterface $entity_type) {

    // Note: delete-form was intentionally omitted, to match core. See #1834002.
    $link_templates = [];
    foreach ([
      'canonical',
      'edit-form',
      'duplicate-form',
      'version-history',
    ] as $rel) {
      if ($entity_type
        ->hasLinkTemplate($rel)) {
        $link_templates[] = str_replace('-', '_', $rel);
      }
    }
    $tasks = [];
    if (count($link_templates) > 1) {
      $entity_type_id = $entity_type
        ->id();
      $base = reset($link_templates);
      $titles = [
        'canonical' => $this
          ->t('View'),
        'edit_form' => $this
          ->t('Edit'),
        'duplicate_form' => $this
          ->t('Duplicate'),
        'version_history' => $this
          ->t('Revisions'),
      ];
      $weight = 0;
      foreach ($link_templates as $rel) {
        $route_name = "entity.{$entity_type_id}.{$rel}";
        $tasks[$route_name] = [
          'title' => $titles[$rel],
          'route_name' => $route_name,
          'base_route' => "entity.{$entity_type_id}.{$base}",
          'weight' => $weight,
        ];
        $weight += 10;
      }
    }
    return $tasks;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultEntityLocalTaskProvider::buildLocalTasks public function Builds local tasks for the given entity type. Overrides EntityLocalTaskProviderInterface::buildLocalTasks
DefaultEntityLocalTaskProvider::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
DefaultEntityLocalTaskProvider::__construct public function Constructs a DefaultEntityLocalTaskProvider 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.