Skip to content
Snippets Groups Projects
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
Branches
Tags
No related merge requests found
......@@ -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}
*/
......
......@@ -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;
}
......@@ -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,10 +169,12 @@ class MicrositeMenuLinkDiscovery implements MicrositeMenuLinkDiscoveryInterface
'provider' => 'entity_hierarchy_microsite',
'discovered' => 1,
'parent' => 'entity_hierarchy_microsite:' . $home->uuid(),
];
$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];
], $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];
}
}
}
}
......
......@@ -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;
}
}
<?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);
}
}
......@@ -16,6 +16,7 @@ class MicrositeMenuItemsTest extends EntityHierarchyMicrositeKernelTestBase {
* Tests the microsite menu link integration.
*/
public function testMicrositeMenuLinkDerivation() {
\Drupal::state()->set('entity_hierarchy_microsite_max_depth', 2);
$media = $this->createImageMedia();
$children = $this->createChildEntities($this->parent->id(), 5);
list ($first, $second) = array_values($children);
......@@ -26,6 +27,8 @@ class MicrositeMenuItemsTest extends EntityHierarchyMicrositeKernelTestBase {
'home' => $this->parent,
'logo' => $media,
]);
$last_second_child = end($second_children);
$too_deep = $this->createChildEntities($last_second_child->id(), 4, '2.4.');
$microsite->save();
// There should be no menus generated.
/** @var \Drupal\Core\Menu\MenuLinkTreeInterface $tree */
......@@ -61,6 +64,9 @@ class MicrositeMenuItemsTest extends EntityHierarchyMicrositeKernelTestBase {
$this->assertCount(4, $items[$plugin_id]->subtree[$child_plugin_id]->subtree);
foreach ($second_children as $child_entity) {
$this->assertArrayHasKey('entity_hierarchy_microsite:' . $child_entity->uuid(), $items[$plugin_id]->subtree[$child_plugin_id]->subtree);
if ($child_entity->uuid() === $last_second_child->uuid()) {
$this->assertEmpty($items[$plugin_id]->subtree[$child_plugin_id]->subtree['entity_hierarchy_microsite:' . $child_entity->uuid()]->subtree);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment