-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f7c17e
Added PreviewLinkAutopopulate plugin to auto-populate preview links l…
stephen-cox d76eaeb
Fixed typo
stephen-cox 8bc7070
Merge branch '2.x' into feature/2.x/localgov-600-autopopulate-preview…
stephen-cox 45063c9
Added new method to fetch flatterned entity hierachy to SubsitesHiera…
stephen-cox 6edea81
Added test for new getFlattenedSubsiteHierarchy method
stephen-cox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.