Skip to content
Snippets Groups Projects
Verified Commit 1557deab authored by Lee Rowlands's avatar Lee Rowlands Committed by Lee Rowlands
Browse files

Issue #3103321 by larowlan, acbramley, Murz: Childrens of saved entity in...

Issue #3103321 by larowlan, acbramley, Murz: Childrens of saved entity in microsite menu structure moves to upper level after saving parent entity, containing childrens
parent a2af2dae
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\entity_hierarchy_microsite\EntityHooks;
......@@ -15,7 +16,10 @@ use Drupal\node\NodeInterface;
/**
* Implements hook_ENTITY_TYPE_update().
*/
function entity_hierarchy_microsite_node_update(NodeInterface $node) {
function entity_hierarchy_microsite_entity_update(EntityInterface $node) {
if ($node->getEntityTypeId() !== 'node') {
return;
}
\Drupal::service('class_resolver')->getInstanceFromDefinition(EntityHooks::class)->onNodeUpdate($node);
}
......@@ -29,7 +33,10 @@ function entity_hierarchy_microsite_node_insert(NodeInterface $node) {
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function entity_hierarchy_microsite_node_delete(NodeInterface $node) {
function entity_hierarchy_microsite_entity_delete(EntityInterface $node) {
if ($node->getEntityTypeId() !== 'node') {
return;
}
\Drupal::service('class_resolver')->getInstanceFromDefinition(EntityHooks::class)->onNodeDelete($node);
}
......@@ -61,3 +68,16 @@ function entity_hierarchy_microsite_form_menu_edit_form_alter(&$form, FormStateI
}
}
}
/**
* Implements hook_module_implements_alter().
*/
function entity_hierarchy_microsite_module_implements_alter(&$implementations, $hook) {
if ($hook == 'entity_update' || $hook === 'entity_delete') {
// Our implementation of these hooks needs to go after entity_hierarchy
// so that the parent update can run.
$definition = $implementations['entity_hierarchy_microsite'];
unset($implementations['entity_hierarchy_microsite']);
$implementations['entity_hierarchy_microsite'] = $definition;
}
}
......@@ -19,8 +19,8 @@ class MicrositeMenuItemsTest extends EntityHierarchyMicrositeKernelTestBase {
$media = $this->createImageMedia();
$children = $this->createChildEntities($this->parent->id(), 5);
list ($first, $second) = array_values($children);
$first_children = $this->createChildEntities($first->id(), 5);
$second_children = $this->createChildEntities($second->id(), 4);
$first_children = $this->createChildEntities($first->id(), 5, '1.');
$second_children = $this->createChildEntities($second->id(), 4, '2.');
$microsite = Microsite::create([
'name' => 'Subsite',
'home' => $this->parent,
......@@ -83,6 +83,14 @@ class MicrositeMenuItemsTest extends EntityHierarchyMicrositeKernelTestBase {
$this->assertArrayHasKey('entity_hierarchy_microsite:' . $child_entity->uuid(), $items[$plugin_id]->subtree[$child_plugin_id]->subtree);
}
// Update child and make sure no items have been re-parented.
$items = $tree->load('entity-hierarchy-microsite', $params);
$this->assertCount(5, $items[$plugin_id]->subtree);
$first->set('title', 'Updated first title')->setNewRevision();
$first->save();
$items = $tree->load('entity-hierarchy-microsite', $params);
$this->assertCount(5, $items[$plugin_id]->subtree);
$lastChildOfSecond = end($second_children);
$override1 = MicrositeMenuItemOverride::create([
'target' => $lastChildOfSecond->uuid(),
......
......@@ -96,14 +96,16 @@ trait EntityHierarchyTestTrait {
* Parent ID.
* @param int $count
* (optional) Number to create. Defaults to 5.
* @param string $prefix
* (Optional) Title prefix.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* Child entities
*/
protected function createChildEntities($parentId, $count = 5) {
protected function createChildEntities($parentId, $count = 5, string $prefix = '') {
$entities = [];
foreach (range(1, $count) as $i) {
$label = sprintf('Child %d', $i);
$label = sprintf('Child %s%d', $prefix, $i);
$entities[$label] = $this->doCreateChildTestEntity($parentId, $label, -1 * $i);
}
return $entities;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment