Loading modules/entity_hierarchy_microsite/src/Entity/Microsite.php +11 −2 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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') Loading Loading @@ -99,7 +101,7 @@ class Microsite extends ContentEntityBase implements MicrositeInterface { 'weight' => -5, ]) ->setDisplayOptions('form', [ 'type' => 'checkbox', 'type' => 'boolean_checkbox', 'weight' => -5, ]) ->setDisplayConfigurable('view', TRUE) Loading Loading @@ -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} */ Loading modules/entity_hierarchy_microsite/src/Entity/MicrositeInterface.php +20 −0 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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; } modules/entity_hierarchy_microsite/src/MicrositeMenuLinkDiscovery.php +7 −5 Original line number Diff line number Diff line Loading @@ -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(), Loading @@ -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') Loading modules/entity_hierarchy_microsite/tests/modules/entity_hierarchy_microsite_test/entity_hierarchy_microsite_test.module +10 −0 Original line number Diff line number Diff line Loading @@ -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(). Loading @@ -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; } } modules/entity_hierarchy_microsite/tests/modules/entity_hierarchy_microsite_test/src/Entity/CustomMicrosite.php 0 → 100644 +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
modules/entity_hierarchy_microsite/src/Entity/Microsite.php +11 −2 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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') Loading Loading @@ -99,7 +101,7 @@ class Microsite extends ContentEntityBase implements MicrositeInterface { 'weight' => -5, ]) ->setDisplayOptions('form', [ 'type' => 'checkbox', 'type' => 'boolean_checkbox', 'weight' => -5, ]) ->setDisplayConfigurable('view', TRUE) Loading Loading @@ -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} */ Loading
modules/entity_hierarchy_microsite/src/Entity/MicrositeInterface.php +20 −0 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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; }
modules/entity_hierarchy_microsite/src/MicrositeMenuLinkDiscovery.php +7 −5 Original line number Diff line number Diff line Loading @@ -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(), Loading @@ -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') Loading
modules/entity_hierarchy_microsite/tests/modules/entity_hierarchy_microsite_test/entity_hierarchy_microsite_test.module +10 −0 Original line number Diff line number Diff line Loading @@ -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(). Loading @@ -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; } }
modules/entity_hierarchy_microsite/tests/modules/entity_hierarchy_microsite_test/src/Entity/CustomMicrosite.php 0 → 100644 +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); } }