View source
<?php
namespace Drupal\entity_hierarchy_microsite\Plugin\Menu;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MicrositeMenuItem extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $overrideAllowed = [
'parent' => 1,
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
'title' => 1,
];
protected $overrideEntity;
protected $entityTypeManager;
private $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
}
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('module_handler'));
}
public function isDeletable() {
return TRUE;
}
public function deleteLink() {
if (!$this
->getOverrideEntity()
->isNew()) {
$this
->getOverrideEntity()
->delete();
}
}
public function getDeleteRoute() {
if (!$this
->getOverrideEntity()
->isNew()) {
return $this
->getOverrideEntity()
->toUrl('delete-form');
}
}
public function getEditRoute() {
if (!$this
->getOverrideEntity()
->isNew()) {
return $this
->getOverrideEntity()
->toUrl('edit-form');
}
return new Url('entity.eh_microsite_menu_override.add', [
'target' => $this
->getDerivativeId(),
]);
}
public function isResettable() {
return FALSE;
}
protected function getOverrideEntity() {
$storage = $this->entityTypeManager
->getStorage('eh_microsite_menu_override');
if (empty($this->overrideEntity)) {
if ($items = $storage
->loadByProperties([
'target' => $this
->getDerivativeId(),
])) {
$this->overrideEntity = reset($items);
return $this->overrideEntity;
}
$this->overrideEntity = $storage
->create([
'target' => $this
->getDerivativeId(),
'enabled' => $this
->isEnabled(),
'weight' => $this
->getWeight(),
'title' => $this
->getTitle(),
'parent' => $this
->getParent(),
]);
}
return $this->overrideEntity;
}
public function updateLink(array $new_definition_values, $persist) {
$overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
$original = $this
->getPluginDefinition();
$metadata = $original['metadata'] + [
'original' => array_intersect_key($original, [
'title' => TRUE,
'weight' => TRUE,
'enabled' => TRUE,
'expanded' => TRUE,
'parent' => TRUE,
]),
];
$this->pluginDefinition = [
'metadata' => $metadata,
] + $overrides + $original;
if ($persist) {
$overrideEntity = $this
->getOverrideEntity();
foreach ($overrides as $key => $value) {
$overrideEntity->{$key}->value = $value;
}
$overrideEntity
->setSyncing(TRUE);
$overrideEntity
->save();
$overrideEntity
->setSyncing(FALSE);
}
return $this->pluginDefinition;
}
public function isTranslatable() {
return FALSE;
}
public function getTitle() {
return $this->pluginDefinition['title'];
}
public function getDescription() {
return $this->pluginDefinition['description'];
}
public function getUrlObject($title_attribute = TRUE) {
$url = parent::getUrlObject($title_attribute);
$override_entity = $this
->getOverrideEntity();
$this->moduleHandler
->alter('entity_hierarchy_microsite_menu_item_url', $url, $override_entity, $this);
return $url;
}
}