Skip to content

Commit 86246d8

Browse files
authored
Added PreviewLinkAutopopulate plugin to auto-populate preview links (#146)
* Added PreviewLinkAutopopulate plugin to auto-populate preview links localgovdrupal/localgov#600 * Fixed typo * Added new method to fetch flatterned entity hierachy to SubsitesHierarchyTrait * Added test for new getFlattenedSubsiteHierarchy method
1 parent 4392ae7 commit 86246d8

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

src/Plugin/Block/SubsitesHierarchyTrait.php

+37
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,41 @@ protected function getRootId(EntityInterface $entity): ?int {
124124
return NULL;
125125
}
126126

127+
/**
128+
* Get flattened list of nodes in subsite hierarchy.
129+
*
130+
* This does not do any access checks so unpublished nodes may be returned. If
131+
* this becomes a requirement then it should be extended to include this.
132+
*
133+
* @param \Drupal\node\NodeInterface $node
134+
* Any entity in the hierarchy.
135+
*
136+
* @return array
137+
* Flattened list of nodes in subsite hierarchy.
138+
*/
139+
public function getFlattenedSubsiteHierarchy(NodeInterface $node): array {
140+
$nodes = [];
141+
142+
$storage = $this->getNestedSetStorage('localgov_subsites');
143+
$node_key = $this->getNestedSetNodeKeyFactory()->fromEntity($node);
144+
if ($ancestors = $storage->findAncestors($node_key)) {
145+
$tree = $storage->findDescendants($ancestors[0]->getNodeKey());
146+
array_unshift($tree, $ancestors[0]);
147+
$mapper = \Drupal::service('entity_hierarchy.entity_tree_node_mapper');
148+
$ancestor_entities = $mapper->loadEntitiesForTreeNodesWithoutAccessChecks('node', $tree);
149+
foreach ($ancestor_entities as $ancestor_entity) {
150+
if (!$ancestor_entities->contains($ancestor_entity)) {
151+
// Doesn't exist or is access hidden.
152+
continue;
153+
}
154+
$entity = $ancestor_entities->offsetGet($ancestor_entity);
155+
if ($entity instanceof NodeInterface) {
156+
$nodes[] = $entity;
157+
}
158+
}
159+
}
160+
161+
return $nodes;
162+
}
163+
127164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Drupal\localgov_subsites\Plugin\PreviewLinkAutopopulate;
4+
5+
use Drupal\localgov_subsites\Plugin\Block\SubsitesHierarchyTrait;
6+
use Drupal\preview_link\PreviewLinkAutopopulatePluginBase;
7+
8+
/**
9+
* Auto-populate subsite preview links.
10+
*
11+
* @PreviewLinkAutopopulate(
12+
* id = "localgov_subsites",
13+
* label = @Translation("Add all the pages for this subsite"),
14+
* description = @Translation("Add subsite overview and page nodes to preview link."),
15+
* supported_entities = {
16+
* "node" = {
17+
* "localgov_subsites_overview",
18+
* "localgov_subsites_page",
19+
* }
20+
* },
21+
* )
22+
*/
23+
class Subsites extends PreviewLinkAutopopulatePluginBase {
24+
25+
use SubsitesHierarchyTrait;
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function getPreviewEntities(): array {
31+
return $this->getFlattenedSubsiteHierarchy($this->entity);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\Tests\localgov_subsites\Kernel;
6+
7+
use Drupal\field\Entity\FieldConfig;
8+
use Drupal\field\Entity\FieldStorageConfig;
9+
use Drupal\KernelTests\KernelTestBase;
10+
use Drupal\localgov_subsites\Plugin\Block\SubsitesHierarchyTrait;
11+
use Drupal\node\NodeInterface;
12+
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
13+
use Drupal\Tests\node\Traits\NodeCreationTrait;
14+
15+
/**
16+
* Test SubsitesHierarchyTrait methods.
17+
*
18+
* @group localgov_subsites
19+
*/
20+
class SubsitesHierarchyTraitTest extends KernelTestBase {
21+
22+
use ContentTypeCreationTrait;
23+
use NodeCreationTrait;
24+
use SubsitesHierarchyTrait;
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected static $modules = [
30+
'dbal',
31+
'field',
32+
'filter',
33+
'entity_hierarchy',
34+
'language',
35+
'link',
36+
'node',
37+
'path',
38+
'system',
39+
'text',
40+
'token',
41+
'user',
42+
];
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
protected function setUp(): void {
48+
parent::setUp();
49+
50+
$this->installEntitySchema('user');
51+
$this->installEntitySchema('node');
52+
$this->installSchema('node', ['node_access']);
53+
$this->installConfig([
54+
'filter',
55+
'system',
56+
'user',
57+
'node',
58+
]);
59+
60+
// Create subsite content types and entity hierarchy field.
61+
$this->createContentType([
62+
'type' => 'localgov_subsites_overview',
63+
'name' => 'Subsites Overview',
64+
]);
65+
$this->createContentType([
66+
'type' => 'localgov_subsites_page',
67+
'name' => 'Subsites Page',
68+
]);
69+
$storage = FieldStorageConfig::create([
70+
'entity_type' => 'node',
71+
'field_name' => 'localgov_subsites_parent',
72+
'id' => 'node.localgov_subsites_parent',
73+
'type' => 'entity_reference_hierarchy',
74+
'settings' => [
75+
'target_type' => 'node',
76+
],
77+
]);
78+
$storage->save();
79+
$config = FieldConfig::create([
80+
'field_name' => 'localgov_subsites_parent',
81+
'entity_type' => 'node',
82+
'bundle' => 'localgov_subsites_page',
83+
'id' => 'node.localgov_subsites_page.localgov_subsites_parent',
84+
'label' => 'Parent',
85+
]);
86+
$config->save();
87+
}
88+
89+
/**
90+
* Test callback.
91+
*/
92+
public function testGetFlattenedSubsiteHierarchy(): void {
93+
94+
// Create subsite hierarchy.
95+
$subsite_overview = $this->createNode([
96+
'title' => $this->randomString(),
97+
'type' => 'localgov_subsites_overview',
98+
'status' => NodeInterface::PUBLISHED,
99+
]);
100+
$subsite_overview->save();
101+
$subsite_page1 = $this->createNode([
102+
'title' => $this->randomString(),
103+
'type' => 'localgov_subsites_page',
104+
'localgov_subsites_parent' => [
105+
'target_id' => $subsite_overview->id(),
106+
'weight' => 0,
107+
],
108+
'status' => NodeInterface::NOT_PUBLISHED,
109+
]);
110+
$subsite_page1->save();
111+
$subsite_page2 = $this->createNode([
112+
'title' => $this->randomString(),
113+
'type' => 'localgov_subsites_page',
114+
'localgov_subsites_parent' => [
115+
'target_id' => $subsite_overview->id(),
116+
'weight' => 0,
117+
],
118+
'status' => NodeInterface::PUBLISHED,
119+
]);
120+
$subsite_page2->save();
121+
$subsite_page3 = $this->createNode([
122+
'title' => $this->randomString(),
123+
'type' => 'localgov_subsites_page',
124+
'localgov_subsites_parent' => [
125+
'target_id' => $subsite_page2->id(),
126+
'weight' => 0,
127+
],
128+
'status' => NodeInterface::PUBLISHED,
129+
]);
130+
$subsite_page3->save();
131+
132+
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_overview));
133+
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page1));
134+
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page2));
135+
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page3));
136+
}
137+
138+
}

0 commit comments

Comments
 (0)