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

Issue #3304641 by larowlan: Allow microsite bundle classes to have a say in menu item generation

parent 9d31bbf3
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_hierarchy_microsite\EntityHooks;
use Drupal\node\Entity\Node as DrupalNode;
use PNX\NestedSet\Node;

/**
 * Defines a class for a microsite entity.
@@ -57,7 +59,7 @@ class Microsite extends ContentEntityBase implements MicrositeInterface {
  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['name'] = BaseFieldDefinition::create('string')
@@ -99,7 +101,7 @@ class Microsite extends ContentEntityBase implements MicrositeInterface {
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'checkbox',
        'type' => 'boolean_checkbox',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE)
@@ -144,6 +146,13 @@ class Microsite extends ContentEntityBase implements MicrositeInterface {
    return (bool) $this->get('generate_menu')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function modifyMenuPluginDefinition(Node $treeNode, DrupalNode $node, array $definition, Node $homeNode): array {
    return $definition;
  }

  /**
   * {@inheritdoc}
   */
+20 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\entity_hierarchy_microsite\Entity;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\Entity\Node as DrupalNode;
use PNX\NestedSet\Node;

/**
 * Defines an interface for microsites.
@@ -33,4 +35,22 @@ interface MicrositeInterface extends ContentEntityInterface {
   */
  public function shouldGenerateMenu();

  /**
   * Allows a microsite sub-class to modify plugin definitions.
   *
   * @param \PNX\NestedSet\Node $treeNode
   *   Tree node for this menu plugin definition.
   * @param \Drupal\node\Entity\Node $node
   *   Drupal node for the menu plugin definition.
   * @param array $definition
   *   Menu plugin definition. This should be modified and returned.
   * @param \PNX\NestedSet\Node $homeNode
   *   Tree node for the microsite home.
   *
   * @return array
   *   Modified menu plugin definition. To prevent the menu link from being
   *   created, return an empty array.
   */
  public function modifyMenuPluginDefinition(Node $treeNode, DrupalNode $node, array $definition, Node $homeNode): array;

}
+7 −5
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface
          $revisionKey = sprintf('%s:%s', $treeNode->getId(), $treeNode->getRevisionId());
          $itemUuid = $item->uuid();
          $parentUuids[$revisionKey] = $itemUuid;
          $definitions[$itemUuid] = [
          if ($definition = $microsite->modifyMenuPluginDefinition($treeNode, $item, [
            'class' => MicrositeMenuItem::class,
            'menu_name' => 'entity-hierarchy-microsite',
            'route_name' => $url->getRouteName(),
@@ -169,13 +169,15 @@ class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface
            'provider' => 'entity_hierarchy_microsite',
            'discovered' => 1,
            'parent' => 'entity_hierarchy_microsite:' . $home->uuid(),
          ];
          ], $homeNode)) {
            $definitions[$itemUuid] = $definition;
            $parent = $tree->findParent($treeNode->getNodeKey());
            if ($parent && ($parentRevisionKey = sprintf('%s:%s', $parent->getId(), $parent->getRevisionId())) && array_key_exists($parentRevisionKey, $parentUuids)) {
              $definitions[$itemUuid]['parent'] = 'entity_hierarchy_microsite:' . $parentUuids[$parentRevisionKey];
            }
          }
        }
      }
      /** @var \Drupal\entity_hierarchy_microsite\Entity\MicrositeMenuItemOverrideInterface $override */
      if ($definitions) {
        foreach ($this->entityTypeManager->getStorage('eh_microsite_menu_override')
+10 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Url;
use Drupal\entity_hierarchy_microsite\Entity\MicrositeMenuItemOverrideInterface;
use Drupal\entity_hierarchy_microsite\Plugin\Menu\MicrositeMenuItem;
use Drupal\entity_hierarchy_microsite_test\Entity\CustomMicrosite;

/**
 * Implements hook_entity_hierarchy_microsite_links_alter().
@@ -25,3 +26,12 @@ function entity_hierarchy_microsite_test_entity_hierarchy_microsite_menu_item_ur
  $attributes['data-some-data'] = 'some-data';
  $url->setOption('attributes', $attributes);
}

/**
 * Implements hook_entity_bundle_info_alter().
 */
function entity_hierarchy_microsite_test_entity_bundle_info_alter(&$bundles) {
  if (isset($bundles['entity_hierarchy_microsite']['entity_hierarchy_microsite'])) {
    $bundles['entity_hierarchy_microsite']['entity_hierarchy_microsite']['class'] = CustomMicrosite::class;
  }
}
+26 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\entity_hierarchy_microsite_test\Entity;

use Drupal\entity_hierarchy_microsite\Entity\Microsite;
use Drupal\node\Entity\Node as DrupalNode;
use PNX\NestedSet\Node;

/**
 * Defines a class for a custom microsite entity.
 */
final class CustomMicrosite extends Microsite {

  /**
   * {@inheritdoc}
   */
  public function modifyMenuPluginDefinition(Node $treeNode, DrupalNode $node, array $definition, Node $homeNode): array {
    if ($treeNode->getDepth() > \Drupal::state()->get('entity_hierarchy_microsite_max_depth', 100)) {
      return [];
    }
    return parent::modifyMenuPluginDefinition($treeNode, $node, $definition, $homeNode);
  }

}
Loading