View source
<?php
namespace Drupal\entity_hierarchy_microsite;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeMenuItemOverrideInterface;
use Drupal\entity_hierarchy_microsite\Form\MicrositeMenuItemForm;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityHooks implements ContainerInjectionInterface {
protected $menuLinkManager;
protected $parentCandidate;
protected $childOfMicrositeLookup;
protected $menuLinkDiscovery;
public function __construct(MenuLinkManagerInterface $menuLinkManager, ParentCandidateInterface $parentCandidate, ChildOfMicrositeLookupInterface $childOfMicrositeLookup, MicrositeMenuLinkDiscoveryInterface $menuLinkDiscovery) {
$this->menuLinkManager = $menuLinkManager;
$this->parentCandidate = $parentCandidate;
$this->childOfMicrositeLookup = $childOfMicrositeLookup;
$this->menuLinkDiscovery = $menuLinkDiscovery;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.menu.link'), $container
->get('entity_hierarchy.information.parent_candidate'), $container
->get('entity_hierarchy_microsite.microsite_lookup'), $container
->get('entity_hierarchy_microsite.menu_link_discovery'));
}
public function onNodeInsert(NodeInterface $node) {
foreach ($this->parentCandidate
->getCandidateFields($node) as $field) {
foreach ($this
->getMicrositesForNodeAndField($node, $field) as $microsite) {
$this
->updateMenuForMicrosite($microsite);
}
}
}
protected function getMicrositesForNodeAndField(NodeInterface $node, string $field) : array {
if (!$node
->hasField($field)) {
return [];
}
if ($node
->get($field)
->isEmpty()) {
return [];
}
return $this->childOfMicrositeLookup
->findMicrositesForNodeAndField($node, $field);
}
public function onNodeUpdate(NodeInterface $node) {
$original = $node->original;
foreach ($this->parentCandidate
->getCandidateFields($node) as $field) {
if ($node
->hasField($field) && (!$node
->get($field)
->isEmpty() || !$original
->get($field)
->isEmpty()) || ($node->{$field}->target_id !== $original->{$field}->target_id || $node->{$field}->weight !== $original->{$field}->weight)) {
if ($microsites = $this->childOfMicrositeLookup
->findMicrositesForNodeAndField($node, $field)) {
foreach ($microsites as $microsite) {
$this
->updateMenuForMicrosite($microsite);
}
continue;
}
if ($microsites = $this->childOfMicrositeLookup
->findMicrositesForNodeAndField($original, $field)) {
$plugin_id = 'entity_hierarchy_microsite:' . $original
->uuid();
if ($this->menuLinkManager
->hasDefinition($plugin_id)) {
$this->menuLinkManager
->removeDefinition($plugin_id);
}
}
}
}
}
public function onNodeDelete(NodeInterface $node) {
foreach ($this->parentCandidate
->getCandidateFields($node) as $field) {
if ($this
->getMicrositesForNodeAndField($node, $field)) {
$plugin_id = 'entity_hierarchy_microsite:' . $node
->uuid();
if ($this->menuLinkManager
->hasDefinition($plugin_id)) {
$this->menuLinkManager
->removeDefinition($plugin_id);
continue;
}
}
}
}
public function onMicrositePostSave(MicrositeInterface $microsite, $isUpdate) {
$this
->updateMenuForMicrosite($microsite);
}
protected function updateMenuForMicrosite(MicrositeInterface $microsite) {
foreach ($this->menuLinkDiscovery
->getMenuLinkDefintions($microsite) as $uuid => $definition) {
$plugin_id = 'entity_hierarchy_microsite:' . $uuid;
if ($this->menuLinkManager
->hasDefinition($plugin_id)) {
$this->menuLinkManager
->updateDefinition($plugin_id, $definition, FALSE);
continue;
}
$this->menuLinkManager
->addDefinition($plugin_id, $definition);
}
}
public function onMenuOverridePostSave(MicrositeMenuItemOverrideInterface $item, $update = FALSE) {
if ($item
->isSyncing()) {
return;
}
$plugin_id = 'entity_hierarchy_microsite:' . $item
->getTarget();
if ($this->menuLinkManager
->hasDefinition($plugin_id) && ($original = $this->menuLinkManager
->getDefinition($plugin_id))) {
$definition = [
'title' => $item
->label(),
'weight' => $item
->getWeight(),
'form_class' => MicrositeMenuItemForm::class,
'enabled' => $item
->isEnabled(),
'expanded' => $item
->isExpanded(),
'parent' => $item
->getParent(),
] + $original;
if (!$update) {
$definition['metadata'] = [
'original' => array_intersect_key($original, [
'title' => TRUE,
'weight' => TRUE,
'enabled' => TRUE,
'expanded' => TRUE,
'parent' => TRUE,
]),
] + $original['metadata'];
}
$this->menuLinkManager
->updateDefinition($plugin_id, $definition);
}
}
public function onMenuOverridePostDelete(array $items) {
foreach ($items as $item) {
$plugin_id = 'entity_hierarchy_microsite:' . $item
->getTarget();
if ($this->menuLinkManager
->hasDefinition($plugin_id) && ($definition = $this->menuLinkManager
->getDefinition($plugin_id)) && isset($definition['metadata']['original'])) {
$definition = $definition['metadata']['original'] + $definition;
$definition['form_class'] = MenuLinkDefaultForm::class;
unset($definition['metadata']['original']);
$this->menuLinkManager
->updateDefinition($plugin_id, $definition, FALSE);
continue;
}
}
}
}