Verified Commit c149280c authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3322336 by larowlan, mstrelan, nterbogt, pghaemim: Don't update...

Issue #3322336 by larowlan, mstrelan, nterbogt, pghaemim: Don't update microsite menus if the parent doesn't change
parent 3fc94460
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ function hook_entity_hierarchy_microsite_menu_item_url_alter(\Drupal\Core\Url $u
 * @param array $links
 *   The link definitions to be altered.
 */
function hook_entity_hierarchy_microsite_links_alter($links) {
function hook_entity_hierarchy_microsite_links_alter(array $links) {
  // Disable all test node links in the microsite menu.
  foreach ($links as $key => $link) {
    if (empty($link['menu_name']) ||
+6 −0
Original line number Diff line number Diff line
@@ -24,3 +24,9 @@ services:
      - '@entity_hierarchy.information.parent_candidate'
    tags:
      - { name: cache.context }
  entity_hierarchy_microsite.menu_rebuild_processor:
    class: Drupal\entity_hierarchy_microsite\MenuRebuildProcessor
    arguments:
      - '@plugin.manager.menu.link'
    tags:
      - { name: needs_destruction }
+32 −7
Original line number Diff line number Diff line
@@ -59,6 +59,11 @@ class EntityHooks implements ContainerInjectionInterface {
   */
  protected $menuLinkDiscovery;

  /**
   * @var \Drupal\entity_hierarchy_microsite\MenuRebuildProcessor
   */
  private MenuRebuildProcessor $menuRebuildProcessor;

  /**
   * Constructs a new EntityHooks.
   *
@@ -72,13 +77,23 @@ class EntityHooks implements ContainerInjectionInterface {
   *   Discovery.
   * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
   *   Menu link tree.
   * @param \Drupal\entity_hierarchy_microsite\MenuRebuildProcessor $menuRebuildProcessor
   *   Menu rebuild processor.
   */
  public function __construct(MenuLinkManagerInterface $menuLinkManager, ParentCandidateInterface $parentCandidate, ChildOfMicrositeLookupInterface $childOfMicrositeLookup, MicrositeMenuLinkDiscoveryInterface $menuLinkDiscovery, MenuLinkTreeInterface $menuLinkTree) {
  public function __construct(
    MenuLinkManagerInterface $menuLinkManager,
    ParentCandidateInterface $parentCandidate,
    ChildOfMicrositeLookupInterface $childOfMicrositeLookup,
    MicrositeMenuLinkDiscoveryInterface $menuLinkDiscovery,
    MenuLinkTreeInterface $menuLinkTree,
    MenuRebuildProcessor $menuRebuildProcessor
  ) {
    $this->menuLinkTree = $menuLinkTree;
    $this->menuLinkManager = $menuLinkManager;
    $this->parentCandidate = $parentCandidate;
    $this->childOfMicrositeLookup = $childOfMicrositeLookup;
    $this->menuLinkDiscovery = $menuLinkDiscovery;
    $this->menuRebuildProcessor = $menuRebuildProcessor;
  }

  /**
@@ -90,7 +105,8 @@ class EntityHooks implements ContainerInjectionInterface {
      $container->get('entity_hierarchy.information.parent_candidate'),
      $container->get('entity_hierarchy_microsite.microsite_lookup'),
      $container->get('entity_hierarchy_microsite.menu_link_discovery'),
      $container->get('menu.link_tree')
      $container->get('menu.link_tree'),
      $container->get('entity_hierarchy_microsite.menu_rebuild_processor')
    );
  }

@@ -138,9 +154,16 @@ class EntityHooks implements ContainerInjectionInterface {
  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 ($node->hasField($field) &&
        (
          // Either the new version or the old version has no parent.
          $node->get($field)->isEmpty() !== $original->get($field)->isEmpty() ||
          // Or the parent changed.
          (int) $node->{$field}->target_id !== (int) $original->{$field}->target_id ||
          // Or the weight changed.
          (int) $node->{$field}->weight !== (int) $original->{$field}->weight
        )
      ) {
        if ($microsites = $this->childOfMicrositeLookup->findMicrositesForNodeAndField($node, $field)) {
          foreach ($microsites as $microsite) {
            $this->updateMenuForMicrosite($microsite);
@@ -186,8 +209,10 @@ class EntityHooks implements ContainerInjectionInterface {
   *   TRUE if is an update.
   */
  public function onMicrositePostSave(MicrositeInterface $microsite, $isUpdate) {
    if ($microsite->shouldGenerateMenu() || ($microsite->original instanceof MicrositeInterface && $microsite->original->shouldGenerateMenu())) {
      $this->updateMenuForMicrosite($microsite);
    }
  }

  /**
   * Updates menu for the microsite.
@@ -196,7 +221,7 @@ class EntityHooks implements ContainerInjectionInterface {
   *   Microsite.
   */
  protected function updateMenuForMicrosite(MicrositeInterface $microsite) {
    $this->menuLinkManager->rebuild();
    $this->menuRebuildProcessor->markRebuildNeeded();
  }

  /**
+59 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\entity_hierarchy_microsite;

use Drupal\Core\DestructableInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;

/**
 * Defines a class for rebuild microsite menus.
 */
final class MenuRebuildProcessor implements DestructableInterface {

  /**
   * TRUE if needs rebuild.
   *
   * @var bool
   */
  protected $needsRebuild = FALSE;

  /**
   * Menu link manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * Constructs a new MenuRebuildProcessor.
   *
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   *   Menu link manager.
   */
  public function __construct(MenuLinkManagerInterface $menuLinkManager) {
    $this->menuLinkManager = $menuLinkManager;
  }

  /**
   * {@inheritdoc}
   */
  public function destruct(): void {
    if ($this->needsRebuild) {
      $this->menuLinkManager->rebuild();
      $this->needsRebuild = FALSE;
    }
  }

  /**
   * Marks rebuild as needed.
   *
   * @return $this
   */
  public function markRebuildNeeded(): self {
    $this->needsRebuild = TRUE;
    return $this;
  }

}
+5 −2
Original line number Diff line number Diff line
@@ -131,7 +131,10 @@ class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface
          'description' => '',
          'weight' => $homeNode->getLeft(),
          'id' => 'entity_hierarchy_microsite:' . $home->uuid(),
          'metadata' => ['entity_id' => $home->id(), 'entity_hierarchy_depth' => $homeNode->getDepth()],
          'metadata' => [
            'entity_id' => $home->id(),
            'entity_hierarchy_depth' => $homeNode->getDepth(),
          ],
          'form_class' => MenuLinkDefaultForm::class,
          'enabled' => 1,
          'expanded' => 1,
@@ -161,7 +164,7 @@ class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface
            'id' => 'entity_hierarchy_microsite:' . $itemUuid,
            'metadata' => [
              'entity_id' => $item->id(),
              'entity_hierarchy_depth' => $treeNode->getDepth()
              'entity_hierarchy_depth' => $treeNode->getDepth(),
            ],
            'form_class' => MenuLinkDefaultForm::class,
            'enabled' => 1,
Loading