You are here

class MenuPositionLink in Menu Position 8

Same name in this branch
  1. 8 src/Plugin/Derivative/MenuPositionLink.php \Drupal\menu_position\Plugin\Derivative\MenuPositionLink
  2. 8 src/Plugin/Menu/MenuPositionLink.php \Drupal\menu_position\Plugin\Menu\MenuPositionLink

Provides menu links for Menu Position Rules.

Hierarchy

Expanded class hierarchy of MenuPositionLink

See also

\Drupal\menu_position\Plugin\Menu\MenuPositionLink

1 string reference to 'MenuPositionLink'
menu_position.links.menu.yml in ./menu_position.links.menu.yml
menu_position.links.menu.yml

File

src/Plugin/Derivative/MenuPositionLink.php, line 17

Namespace

Drupal\menu_position\Plugin\Derivative
View source
class MenuPositionLink extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The menu_position_rule storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config_factory;

  /**
   * Menu position link constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $menu_position_rule_storage
   *   The menu_position_rule storage.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $menu_position_rule_storage, ConfigFactoryInterface $config_factory) {
    $this->storage = $menu_position_rule_storage
      ->getStorage('menu_position_rule');
    $this->config_factory = $config_factory;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // Reset the discovered definitions.
    $this->derivatives = [];
    foreach ($this->storage
      ->loadMultiple() as $menu_position_rule) {

      /* @var \Drupal\menu_position\Entity\MenuPositionRule $menu_position_rule */

      /* @var \Drupal\menu_position\Plugin\Menu\MenuPositionLink $menu_link */
      if ($menu_link = $menu_position_rule
        ->getMenuLinkPlugin()) {

        // Link already exists, use that.
        $definition = $menu_link
          ->getPluginDefinition();
      }
      else {

        // Provide defaults, they will be updated by the rule.
        $definition = [
          'id' => $base_plugin_definition['id'] . ':' . $menu_position_rule
            ->id(),
          'title' => $this
            ->t('@label (menu position rule)', [
            '@label' => $menu_position_rule
              ->getLabel(),
          ]),
          'menu_name' => $menu_position_rule
            ->getMenuName(),
          'parent' => $menu_position_rule
            ->getParent(),
          'weight' => 0,
          'metadata' => [
            'entity_id' => $menu_position_rule
              ->id(),
          ],
          // Links are enabled (i.e. visible) depending on the modules'
          // settings.
          'enabled' => $this->config_factory
            ->get('menu_position.settings')
            ->get('link_display') === 'child',
        ];
      }
      $this->derivatives[$menu_position_rule
        ->id()] = $definition + $base_plugin_definition;
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

}

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
MenuPositionLink::$config_factory protected property The config factory service.
MenuPositionLink::$storage protected property The menu_position_rule storage.
MenuPositionLink::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
MenuPositionLink::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
MenuPositionLink::__construct public function Menu position link constructor.
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.