ViewsMenuLink.php in Drupal 10
File
core/modules/views/src/Plugin/Menu/ViewsMenuLink.php
View source
<?php
namespace Drupal\views\Plugin\Menu;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\ViewExecutableFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewsMenuLink extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $overrideAllowed = [
'menu_name' => 1,
'parent' => 1,
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
'title' => 1,
'description' => 1,
];
protected $entityTypeManager;
protected $viewExecutableFactory;
protected $view;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ViewExecutableFactory $view_executable_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->viewExecutableFactory = $view_executable_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('views.executable'));
}
public function loadView() {
if (empty($this->view)) {
$metadata = $this
->getMetaData();
$view_id = $metadata['view_id'];
$display_id = $metadata['display_id'];
$view_entity = $this->entityTypeManager
->getStorage('view')
->load($view_id);
$view = $this->viewExecutableFactory
->get($view_entity);
$view
->setDisplay($display_id);
$view
->initDisplay();
$this->view = $view;
}
return $this->view;
}
public function getTitle() {
return $this
->loadView()->display_handler
->getOption('menu')['title'];
}
public function getDescription() {
return $this
->loadView()->display_handler
->getOption('menu')['description'];
}
public function isExpanded() {
return (bool) $this
->loadView()->display_handler
->getOption('menu')['expanded'];
}
public function updateLink(array $new_definition_values, $persist) {
$overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
$this->pluginDefinition = $overrides + $this->pluginDefinition;
if ($persist) {
$view = $this
->loadView();
$display =& $view->storage
->getDisplay($view->current_display);
$changed = FALSE;
foreach ($overrides as $key => $new_definition_value) {
if (empty($display['display_options']['menu'][$key]) || $display['display_options']['menu'][$key] != $new_definition_value) {
$display['display_options']['menu'][$key] = $new_definition_value;
$changed = TRUE;
}
}
if ($changed) {
$view->storage
->save();
}
}
return $this->pluginDefinition;
}
public function isDeletable() {
return TRUE;
}
public function deleteLink() {
}
}