MenuPositionLink.php in Menu Position 8
File
src/Plugin/Derivative/MenuPositionLink.php
View source
<?php
namespace Drupal\menu_position\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuPositionLink extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $storage;
protected $config_factory;
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;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
foreach ($this->storage
->loadMultiple() as $menu_position_rule) {
if ($menu_link = $menu_position_rule
->getMenuLinkPlugin()) {
$definition = $menu_link
->getPluginDefinition();
}
else {
$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(),
],
'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);
}
}