Commit 2678eabf authored by alexpott's avatar alexpott

Issue #2943627 by tim.plunkett, Falco010, alexpott, samuel.mortenson: Layout...

Issue #2943627 by tim.plunkett, Falco010, alexpott, samuel.mortenson: Layout Builder does not consult layout plugin for dependencies
parent 3af430ad
<?php
/**
* @file
* Post update functions for Layout Builder.
*/
/**
* Rebuild plugin dependencies for all entity view displays.
*/
function layout_builder_post_update_rebuild_plugin_dependencies(&$sandbox = NULL) {
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
if (!isset($sandbox['ids'])) {
$sandbox['ids'] = $storage->getQuery()->accessCheck(FALSE)->execute();
$sandbox['count'] = count($sandbox['ids']);
}
for ($i = 0; $i < 10 && count($sandbox['ids']); $i++) {
$id = array_shift($sandbox['ids']);
if ($display = $storage->load($id)) {
$display->save();
}
}
$sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
}
......@@ -198,6 +198,7 @@ public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this->getSections() as $delta => $section) {
$this->calculatePluginDependencies($section->getLayout());
foreach ($section->getComponents() as $uuid => $component) {
$this->calculatePluginDependencies($component->getPlugin());
}
......@@ -212,17 +213,28 @@ public function calculateDependencies() {
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
// Loop through all components and determine if the removed dependencies are
// used by their plugins.
// Loop through all sections and determine if the removed dependencies are
// used by their layout plugins.
foreach ($this->getSections() as $delta => $section) {
foreach ($section->getComponents() as $uuid => $component) {
$plugin_dependencies = $this->getPluginDependencies($component->getPlugin());
$component_removed_dependencies = $this->getPluginRemovedDependencies($plugin_dependencies, $dependencies);
if ($component_removed_dependencies) {
// @todo Allow the plugins to react to their dependency removal in
// https://www.drupal.org/project/drupal/issues/2579743.
$section->removeComponent($uuid);
$changed = TRUE;
$layout_dependencies = $this->getPluginDependencies($section->getLayout());
$layout_removed_dependencies = $this->getPluginRemovedDependencies($layout_dependencies, $dependencies);
if ($layout_removed_dependencies) {
// @todo Allow the plugins to react to their dependency removal in
// https://www.drupal.org/project/drupal/issues/2579743.
$this->removeSection($delta);
$changed = TRUE;
}
// If the section is not removed, loop through all components.
else {
foreach ($section->getComponents() as $uuid => $component) {
$plugin_dependencies = $this->getPluginDependencies($component->getPlugin());
$component_removed_dependencies = $this->getPluginRemovedDependencies($plugin_dependencies, $dependencies);
if ($component_removed_dependencies) {
// @todo Allow the plugins to react to their dependency removal in
// https://www.drupal.org/project/drupal/issues/2579743.
$section->removeComponent($uuid);
$changed = TRUE;
}
}
}
}
......
<?php
/**
* @file
* Test fixture.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
// Update core.extension.
$extensions = $connection->select('config')
->fields('config', ['data'])
->condition('collection', '')
->condition('name', 'core.extension')
->execute()
->fetchField();
$extensions = unserialize($extensions);
$extensions['module']['layout_builder'] = 0;
$extensions['module']['layout_discovery'] = 0;
$extensions['module']['layout_test'] = 0;
$connection->update('config')
->fields([
'data' => serialize($extensions),
'collection' => '',
'name' => 'core.extension',
])
->condition('collection', '')
->condition('name', 'core.extension')
->execute();
// Add a layout plugin with a dependency to an existing entity view display.
$display = $connection->select('config')
->fields('config', ['data'])
->condition('collection', '')
->condition('name', 'core.entity_view_display.node.article.teaser')
->execute()
->fetchField();
$display = unserialize($display);
$display['third_party_settings']['layout_builder']['sections'][] = [
'layout_id' => 'layout_test_dependencies_plugin',
'layout_settings' => [],
'components' => [],
];
$connection->update('config')
->fields([
'data' => serialize($display),
'collection' => '',
'name' => 'core.entity_view_display.node.article.teaser',
])
->condition('collection', '')
->condition('name', 'core.entity_view_display.node.article.teaser')
->execute();
......@@ -16,6 +16,7 @@ class LayoutBuilderTest extends BrowserTestBase {
*/
public static $modules = [
'layout_builder',
'layout_test',
'block',
'node',
];
......@@ -205,9 +206,26 @@ public function testPluginDependencies() {
$page->fillField('label', 'My Menu');
$page->fillField('id', 'mymenu');
$page->pressButton('Save');
$this->drupalGet('admin/structure/menu/add');
$page->fillField('label', 'My Menu');
$page->fillField('id', 'myothermenu');
$page->pressButton('Save');
// Add a menu block.
$this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display-layout/default');
$assert_session->linkExists('Add Section');
$this->clickLink('Add Section');
$assert_session->linkExists('Layout plugin (with dependencies)');
$this->clickLink('Layout plugin (with dependencies)');
$assert_session->elementExists('css', '.layout--layout-test-dependencies-plugin');
$assert_session->elementExists('css', '.field--name-body');
$assert_session->linkExists('Save Layout');
$this->clickLink('Save Layout');
$this->drupalPostForm('admin/structure/menu/manage/myothermenu/delete', [], 'Delete');
$this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display-layout/default');
$assert_session->elementNotExists('css', '.layout--layout-test-dependencies-plugin');
$assert_session->elementExists('css', '.field--name-body');
// Add a menu block.
$assert_session->linkExists('Add Block');
$this->clickLink('Add Block');
$assert_session->linkExists('My Menu');
......
<?php
namespace Drupal\Tests\layout_builder\Functional\Update;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests the upgrade path for Layout Builder section dependencies.
*
* @group layout_builder
*/
class SectionDependenciesUpdatePathTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/section-dependencies.php',
];
}
/**
* Tests the upgrade path for Layout Builder section dependencies.
*/
public function testRunUpdates() {
$data = EntityViewDisplay::load('node.article.teaser')->toArray();
$this->assertNotContains('system.menu.myothermenu', $data['dependencies']['config']);
$this->assertNotContains('layout_builder', $data['dependencies']['module']);
$this->assertNotContains('layout_test', $data['dependencies']['module']);
$this->runUpdates();
$data = EntityViewDisplay::load('node.article.teaser')->toArray();
$this->assertContains('system.menu.myothermenu', $data['dependencies']['config']);
$this->assertContains('layout_builder', $data['dependencies']['module']);
$this->assertContains('layout_test', $data['dependencies']['module']);
}
}
<?php
namespace Drupal\layout_test\Plugin\Layout;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Layout\LayoutDefault;
/**
* Provides a plugin that contains config dependencies.
*
* @Layout(
* id = "layout_test_dependencies_plugin",
* label = @Translation("Layout plugin (with dependencies)"),
* category = @Translation("Layout test"),
* description = @Translation("Test layout"),
* regions = {
* "main" = {
* "label" = @Translation("Main Region")
* }
* }
* )
*/
class LayoutTestDependenciesPlugin extends LayoutDefault implements DependentPluginInterface {
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = [];
$dependencies['config'][] = 'system.menu.myothermenu';
return $dependencies;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment