Skip to content

Added PreviewLinkAutopopulate plugin to auto-populate preview links #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Plugin/Block/SubsitesHierarchyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,41 @@ protected function getRootId(EntityInterface $entity): ?int {
return NULL;
}

/**
* Get flattened list of nodes in subsite hierarchy.
*
* This does not do any access checks so unpublished nodes may be returned. If
* this becomes a requirement then it should be extended to include this.
*
* @param \Drupal\node\NodeInterface $node
* Any entity in the hierarchy.
*
* @return array
* Flattened list of nodes in subsite hierarchy.
*/
public function getFlattenedSubsiteHierarchy(NodeInterface $node): array {
$nodes = [];

$storage = $this->getNestedSetStorage('localgov_subsites');
$node_key = $this->getNestedSetNodeKeyFactory()->fromEntity($node);
if ($ancestors = $storage->findAncestors($node_key)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be $storage->findRoot($node_key) which does much the same.

$tree = $storage->findDescendants($ancestors[0]->getNodeKey());
array_unshift($tree, $ancestors[0]);
$mapper = \Drupal::service('entity_hierarchy.entity_tree_node_mapper');
$ancestor_entities = $mapper->loadEntitiesForTreeNodesWithoutAccessChecks('node', $tree);
foreach ($ancestor_entities as $ancestor_entity) {
if (!$ancestor_entities->contains($ancestor_entity)) {
// Doesn't exist or is access hidden.
continue;
}
$entity = $ancestor_entities->offsetGet($ancestor_entity);
if ($entity instanceof NodeInterface) {
$nodes[] = $entity;
}
}
}

return $nodes;
}

}
34 changes: 34 additions & 0 deletions src/Plugin/PreviewLinkAutopopulate/Subsites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\localgov_subsites\Plugin\PreviewLinkAutopopulate;

use Drupal\localgov_subsites\Plugin\Block\SubsitesHierarchyTrait;
use Drupal\preview_link\PreviewLinkAutopopulatePluginBase;

/**
* Auto-populate subsite preview links.
*
* @PreviewLinkAutopopulate(
* id = "localgov_subsites",
* label = @Translation("Add all the pages for this subsite"),
* description = @Translation("Add subsite overview and page nodes to preview link."),
* supported_entities = {
* "node" = {
* "localgov_subsites_overview",
* "localgov_subsites_page",
* }
* },
* )
*/
class Subsites extends PreviewLinkAutopopulatePluginBase {

use SubsitesHierarchyTrait;

/**
* {@inheritdoc}
*/
public function getPreviewEntities(): array {
return $this->getFlattenedSubsiteHierarchy($this->entity);
}

}
138 changes: 138 additions & 0 deletions tests/src/Kernel/SubsitesHierarchyTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\localgov_subsites\Kernel;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\localgov_subsites\Plugin\Block\SubsitesHierarchyTrait;
use Drupal\node\NodeInterface;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;

/**
* Test SubsitesHierarchyTrait methods.
*
* @group localgov_subsites
*/
class SubsitesHierarchyTraitTest extends KernelTestBase {

use ContentTypeCreationTrait;
use NodeCreationTrait;
use SubsitesHierarchyTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'dbal',
'field',
'filter',
'entity_hierarchy',
'language',
'link',
'node',
'path',
'system',
'text',
'token',
'user',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
$this->installConfig([
'filter',
'system',
'user',
'node',
]);

// Create subsite content types and entity hierarchy field.
$this->createContentType([
'type' => 'localgov_subsites_overview',
'name' => 'Subsites Overview',
]);
$this->createContentType([
'type' => 'localgov_subsites_page',
'name' => 'Subsites Page',
]);
$storage = FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'localgov_subsites_parent',
'id' => 'node.localgov_subsites_parent',
'type' => 'entity_reference_hierarchy',
'settings' => [
'target_type' => 'node',
],
]);
$storage->save();
$config = FieldConfig::create([
'field_name' => 'localgov_subsites_parent',
'entity_type' => 'node',
'bundle' => 'localgov_subsites_page',
'id' => 'node.localgov_subsites_page.localgov_subsites_parent',
'label' => 'Parent',
]);
$config->save();
}

/**
* Test callback.
*/
public function testGetFlattenedSubsiteHierarchy(): void {

// Create subsite hierarchy.
$subsite_overview = $this->createNode([
'title' => $this->randomString(),
'type' => 'localgov_subsites_overview',
'status' => NodeInterface::PUBLISHED,
]);
$subsite_overview->save();
$subsite_page1 = $this->createNode([
'title' => $this->randomString(),
'type' => 'localgov_subsites_page',
'localgov_subsites_parent' => [
'target_id' => $subsite_overview->id(),
'weight' => 0,
],
'status' => NodeInterface::NOT_PUBLISHED,
]);
$subsite_page1->save();
$subsite_page2 = $this->createNode([
'title' => $this->randomString(),
'type' => 'localgov_subsites_page',
'localgov_subsites_parent' => [
'target_id' => $subsite_overview->id(),
'weight' => 0,
],
'status' => NodeInterface::PUBLISHED,
]);
$subsite_page2->save();
$subsite_page3 = $this->createNode([
'title' => $this->randomString(),
'type' => 'localgov_subsites_page',
'localgov_subsites_parent' => [
'target_id' => $subsite_page2->id(),
'weight' => 0,
],
'status' => NodeInterface::PUBLISHED,
]);
$subsite_page3->save();

$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_overview));
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page1));
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page2));
$this->assertCount(4, $this->getFlattenedSubsiteHierarchy($subsite_page3));
}

}