first commit

This commit is contained in:
2020-06-08 23:57:36 +02:00
commit 6277454f7a
16057 changed files with 1715382 additions and 0 deletions
@@ -0,0 +1,6 @@
name: 'Layout Discovery'
type: module
description: 'Provides a way for modules or themes to register layouts.'
package: Core
version: VERSION
core: 8.x
@@ -0,0 +1,22 @@
<?php
/**
* @file
* Install, update, and uninstall functions for the Layout Discovery module.
*/
/**
* Implements hook_requirements().
*/
function layout_discovery_requirements($phase) {
$requirements = [];
if ($phase === 'install') {
if (\Drupal::moduleHandler()->moduleExists('layout_plugin')) {
$requirements['layout_discovery'] = [
'description' => t('Layout Discovery cannot be installed because the Layout Plugin module is installed and incompatible.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
@@ -0,0 +1,108 @@
layout_onecol:
label: 'One column'
path: layouts/onecol
template: layout--onecol
library: layout_discovery/onecol
category: 'Columns: 1'
default_region: content
icon_map:
- [content]
regions:
content:
label: Content
layout_twocol:
label: 'Two column'
path: layouts/twocol
template: layout--twocol
library: layout_discovery/twocol
category: 'Columns: 2'
default_region: first
icon_map:
- [top]
- [first, second]
- [bottom]
regions:
top:
label: Top
first:
label: First
second:
label: Second
bottom:
label: Bottom
layout_twocol_bricks:
label: 'Two column bricks'
path: layouts/twocol_bricks
template: layout--twocol-bricks
library: layout_discovery/twocol_bricks
category: 'Columns: 2'
default_region: middle
icon_map:
- [top]
- [first_above, second_above]
- [middle]
- [first_below, second_below]
- [bottom]
regions:
top:
label: Top
first_above:
label: 'First above'
second_above:
label: 'Second above'
middle:
label: Middle
first_below:
label: 'First below'
second_below:
label: 'Second below'
bottom:
label: Bottom
layout_threecol_25_50_25:
label: 'Three column 25/50/25'
path: layouts/threecol_25_50_25
template: layout--threecol-25-50-25
library: layout_discovery/threecol_25_50_25
category: 'Columns: 3'
default_region: second
icon_map:
- [top]
- [first, second, second, third]
- [bottom]
regions:
top:
label: Top
first:
label: First
second:
label: Second
third:
label: Third
bottom:
label: Bottom
layout_threecol_33_34_33:
label: 'Three column 33/34/33'
path: layouts/threecol_33_34_33
template: layout--threecol-33-34-33
library: layout_discovery/threecol_33_34_33
category: 'Columns: 3'
default_region: first
icon_map:
- [top]
- [first, second, third]
- [bottom]
regions:
top:
label: Top
first:
label: First
second:
label: Second
third:
label: Third
bottom:
label: Bottom
@@ -0,0 +1,25 @@
onecol:
version: VERSION
css:
theme:
layouts/onecol/onecol.css: {}
twocol_bricks:
version: VERSION
css:
theme:
layouts/twocol_bricks/twocol_bricks.css: {}
twocol:
version: VERSION
css:
theme:
layouts/twocol/twocol.css: {}
threecol_25_50_25:
version: VERSION
css:
theme:
layouts/threecol_25_50_25/threecol_25_50_25.css: {}
threecol_33_34_33:
version: VERSION
css:
theme:
layouts/threecol_33_34_33/threecol_33_34_33.css: {}
@@ -0,0 +1,50 @@
<?php
/**
* @file
* Provides hook implementations for Layout Discovery.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_help().
*/
function layout_discovery_help($route_name) {
switch ($route_name) {
case 'help.page.layout_discovery':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Layout Discovery allows modules or themes to register layouts, and for other modules to list the available layouts and render them.') . '</p>';
$output .= '<p>' . t('For more information, see the <a href=":layout-discovery-documentation">online documentation for the Layout Discovery module</a>.', [':layout-discovery-documentation' => 'https://www.drupal.org/docs/8/api/layout-api']) . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function layout_discovery_theme() {
return \Drupal::service('plugin.manager.core.layout')->getThemeImplementations();
}
/**
* Prepares variables for layout templates.
*
* @param array &$variables
* An associative array containing:
* - content: An associative array containing the properties of the element.
* Properties used: #settings, #layout.
*/
function template_preprocess_layout(&$variables) {
$variables['settings'] = isset($variables['content']['#settings']) ? $variables['content']['#settings'] : [];
$variables['layout'] = isset($variables['content']['#layout']) ? $variables['content']['#layout'] : [];
// Create an attributes variable for each region.
foreach (Element::children($variables['content']) as $name) {
if (!isset($variables['content'][$name]['#attributes'])) {
$variables['content'][$name]['#attributes'] = [];
}
$variables['region_attributes'][$name] = new Attribute($variables['content'][$name]['#attributes']);
}
}
@@ -0,0 +1,22 @@
<?php
/**
* @file
* Post update functions for layout discovery.
*/
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
/**
* Recalculate dependencies for the entity_form_display entity.
*/
function layout_discovery_post_update_recalculate_entity_form_display_dependencies(&$sandbox = NULL) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_form_display');
}
/**
* Recalculate dependencies for the entity_view_display entity.
*/
function layout_discovery_post_update_recalculate_entity_view_display_dependencies(&$sandbox = NULL) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_view_display');
}
@@ -0,0 +1,7 @@
services:
plugin.manager.core.layout:
class: Drupal\Core\Layout\LayoutPluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@theme_handler']
layout.icon_builder:
class: Drupal\Core\Layout\Icon\SvgIconBuilder
shared: false
@@ -0,0 +1,25 @@
{#
/**
* @file
* Default theme implementation to display a one-column layout.
*
* Available variables:
* - content: The content for this layout.
* - attributes: HTML attributes for the layout <div>.
*
* @ingroup themeable
*/
#}
{%
set classes = [
'layout',
'layout--onecol',
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
<div {{ region_attributes.content.addClass('layout__region', 'layout__region--content') }}>
{{ content.content }}
</div>
</div>
{% endif %}
@@ -0,0 +1,7 @@
/*
* @file
* Provides the layout styles for layout_onecol.
*/
.layout--onecol .layout__region {
width: 100%;
}
@@ -0,0 +1,54 @@
{#
/**
* @file
* Default theme implementation for a three column layout.
*
* This template provides a three column 25%-50%-25% display layout, with
* additional areas for the top and the bottom.
*
* Available variables:
* - content: The content for this layout.
* - attributes: HTML attributes for the layout <div>.
*
* @ingroup themeable
*/
#}
{%
set classes = [
'layout',
'layout--threecol-25-50-25',
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
{% if content.top %}
<div {{ region_attributes.top.addClass('layout__region', 'layout__region--top') }}>
{{ content.top }}
</div>
{% endif %}
{% if content.first %}
<div {{ region_attributes.first.addClass('layout__region', 'layout__region--first') }}>
{{ content.first }}
</div>
{% endif %}
{% if content.second %}
<div {{ region_attributes.second.addClass('layout__region', 'layout__region--second') }}>
{{ content.second }}
</div>
{% endif %}
{% if content.third %}
<div {{ region_attributes.third.addClass('layout__region', 'layout__region--third') }}>
{{ content.third }}
</div>
{% endif %}
{% if content.bottom %}
<div {{ region_attributes.bottom.addClass('layout__region', 'layout__region--bottom') }}>
{{ content.bottom }}
</div>
{% endif %}
</div>
{% endif %}
@@ -0,0 +1,23 @@
/*
* @file
* Provides the layout styles for layout_threecol_25_50_25.
*/
.layout--threecol-25-50-25 {
display: flex;
flex-wrap: wrap;
}
.layout--threecol-25-50-25 > .layout__region,
.layout--threecol-25-50-25 > .layout__region--second {
flex: 0 1 100%;
}
@media screen and (min-width: 40em) {
.layout--threecol-25-50-25 > .layout__region--first,
.layout--threecol-25-50-25 > .layout__region--third {
flex: 0 1 25%;
}
.layout--threecol-25-50-25 > .layout__region--second {
flex: 0 1 50%;
}
}
@@ -0,0 +1,54 @@
{#
/**
* @file
* Default theme implementation for a three column layout.
*
* This template provides a three column 33%-34%-33% display layout, with
* additional areas for the top and the bottom.
*
* Available variables:
* - content: The content for this layout.
* - attributes: HTML attributes for the layout <div>.
*
* @ingroup themeable
*/
#}
{%
set classes = [
'layout',
'layout--threecol-33-34-33',
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
{% if content.top %}
<div {{ region_attributes.top.addClass('layout__region', 'layout__region--top') }}>
{{ content.top }}
</div>
{% endif %}
{% if content.first %}
<div {{ region_attributes.first.addClass('layout__region', 'layout__region--first') }}>
{{ content.first }}
</div>
{% endif %}
{% if content.second %}
<div {{ region_attributes.second.addClass('layout__region', 'layout__region--second') }}>
{{ content.second }}
</div>
{% endif %}
{% if content.third %}
<div {{ region_attributes.third.addClass('layout__region', 'layout__region--third') }}>
{{ content.third }}
</div>
{% endif %}
{% if content.bottom %}
<div {{ region_attributes.bottom.addClass('layout__region', 'layout__region--bottom') }}>
{{ content.bottom }}
</div>
{% endif %}
</div>
{% endif %}
@@ -0,0 +1,23 @@
/*
* @file
* Provides the layout styles for layout_threecol_33_34_33.
*/
.layout--threecol-33-34-33 {
display: flex;
flex-wrap: wrap;
}
.layout--threecol-33-34-33 > .layout__region {
flex: 0 1 100%;
}
@media screen and (min-width: 40em) {
.layout--threecol-33-34-33 > .layout__region--first,
.layout--threecol-33-34-33 > .layout__region--third {
flex: 0 1 33%;
}
.layout--threecol-33-34-33 > .layout__region--second {
flex: 0 1 34%;
}
}
@@ -0,0 +1,45 @@
{#
/**
* @file
* Default theme implementation to display a two-column layout.
*
* Available variables:
* - content: The content for this layout.
* - attributes: HTML attributes for the layout <div>.
*
* @ingroup themeable
*/
#}
{%
set classes = [
'layout',
'layout--twocol',
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
{% if content.top %}
<div {{ region_attributes.top.addClass('layout__region', 'layout__region--top') }}>
{{ content.top }}
</div>
{% endif %}
{% if content.first %}
<div {{ region_attributes.first.addClass('layout__region', 'layout__region--first') }}>
{{ content.first }}
</div>
{% endif %}
{% if content.second %}
<div {{ region_attributes.second.addClass('layout__region', 'layout__region--second') }}>
{{ content.second }}
</div>
{% endif %}
{% if content.bottom %}
<div {{ region_attributes.bottom.addClass('layout__region', 'layout__region--bottom') }}>
{{ content.bottom }}
</div>
{% endif %}
</div>
{% endif %}
@@ -0,0 +1,20 @@
/*
* @file
* Provides the layout styles for layout_twocol.
*/
.layout--twocol {
display: flex;
flex-wrap: wrap;
}
.layout--twocol > .layout__region {
flex: 0 1 100%;
}
@media screen and (min-width: 40em) {
.layout--twocol > .layout__region--first,
.layout--twocol > .layout__region--second {
flex: 0 1 50%;
}
}
@@ -0,0 +1,66 @@
{#
/**
* @file
* Default theme implementation for a two column layout.
*
* This template provides a two column display layout with full width areas at
* the top, bottom and in the middle.
*
* Available variables:
* - content: The content for this layout.
* - attributes: HTML attributes for the layout <div>.
*
* @ingroup themeable
*/
#}
{%
set classes = [
'layout',
'layout--twocol-bricks',
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
{% if content.top %}
<div {{ region_attributes.top.addClass('layout__region', 'layout__region--top') }}>
{{ content.top }}
</div>
{% endif %}
{% if content.first_above %}
<div {{ region_attributes.first_above.addClass('layout__region', 'layout__region--first-above') }}>
{{ content.first_above }}
</div>
{% endif %}
{% if content.second_above %}
<div {{ region_attributes.second_above.addClass('layout__region', 'layout__region--second-above') }}>
{{ content.second_above }}
</div>
{% endif %}
{% if content.middle %}
<div {{ region_attributes.middle.addClass('layout__region', 'layout__region--middle') }}>
{{ content.middle }}
</div>
{% endif %}
{% if content.first_below %}
<div {{ region_attributes.first_below.addClass('layout__region', 'layout__region--first-below') }}>
{{ content.first_below }}
</div>
{% endif %}
{% if content.second_below %}
<div {{ region_attributes.second_below.addClass('layout__region', 'layout__region--second-below') }}>
{{ content.second_below }}
</div>
{% endif %}
{% if content.bottom %}
<div {{ region_attributes.bottom.addClass('layout__region', 'layout__region--bottom') }}>
{{ content.bottom }}
</div>
{% endif %}
</div>
{% endif %}
@@ -0,0 +1,22 @@
/*
* @file
* Provides the layout styles for layout_twocol_bricks.
*/
.layout--twocol-bricks {
display: flex;
flex-wrap: wrap;
}
.layout--twocol-bricks > .layout__region {
flex: 0 1 100%;
}
@media screen and (min-width: 40em) {
.layout--twocol-bricks > .layout__region--first-above,
.layout--twocol-bricks > .layout__region--second-above,
.layout--twocol-bricks > .layout__region--first-below,
.layout--twocol-bricks > .layout__region--second-below {
flex: 0 1 50%;
}
}
@@ -0,0 +1,23 @@
{#
/**
* @file
* Template for a generic layout.
*/
#}
{%
set classes = [
'layout',
'layout--' ~ layout.id|clean_class,
]
%}
{% if content %}
<div{{ attributes.addClass(classes) }}>
{% for region in layout.getRegionNames %}
{% if content[region] %}
<div {{ region_attributes[region].addClass('layout__region', 'layout__region--' ~ region|clean_class) }}>
{{ content[region] }}
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
@@ -0,0 +1,42 @@
<?php
/**
* @file
* Contains database additions to drupal-8.4.0.bare.standard.php.gz for testing
* upgrade path of https://www.drupal.org/project/drupal/issues/2904550.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->delete('config')
->condition('name', [
'core.extension',
'core.entity_view_display.node.page.default',
'core.entity_form_display.node.page.default',
], 'IN')
->execute();
$connection->insert('config')
->fields([
'collection',
'name',
'data',
])
->values([
'collection' => '',
'name' => 'core.extension',
'data' => 'a:4:{s:6:"module";a:44:{s:14:"automated_cron";i:0;s:5:"block";i:0;s:13:"block_content";i:0;s:10:"breakpoint";i:0;s:8:"ckeditor";i:0;s:5:"color";i:0;s:7:"comment";i:0;s:6:"config";i:0;s:7:"contact";i:0;s:10:"contextual";i:0;s:8:"datetime";i:0;s:5:"dblog";i:0;s:18:"dynamic_page_cache";i:0;s:6:"editor";i:0;s:5:"field";i:0;s:12:"field_layout";i:0;s:8:"field_ui";i:0;s:4:"file";i:0;s:6:"filter";i:0;s:4:"help";i:0;s:7:"history";i:0;s:5:"image";i:0;s:16:"layout_discovery";i:0;s:4:"link";i:0;s:7:"menu_ui";i:0;s:4:"node";i:0;s:7:"options";i:0;s:10:"page_cache";i:0;s:4:"path";i:0;s:9:"quickedit";i:0;s:3:"rdf";i:0;s:6:"search";i:0;s:8:"shortcut";i:0;s:6:"system";i:0;s:8:"taxonomy";i:0;s:4:"text";i:0;s:7:"toolbar";i:0;s:4:"tour";i:0;s:6:"update";i:0;s:4:"user";i:0;s:8:"views_ui";i:0;s:17:"menu_link_content";i:1;s:5:"views";i:10;s:8:"standard";i:1000;}s:5:"theme";a:5:{s:6:"stable";i:0;s:6:"classy";i:0;s:6:"bartik";i:0;s:5:"seven";i:0;s:17:"test_layout_theme";i:0;}s:7:"profile";s:8:"standard";s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"R4IF-ClDHXxblLcG0L7MgsLvfBIMAvi_skumNFQwkDc";}}',
])
->values([
'collection' => '',
'name' => 'core.entity_view_display.node.page.default',
'data' => 'a:12:{s:4:"uuid";s:36:"bf0e7e89-41b9-4031-adef-09933affbfe0";s:8:"langcode";s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:2:{s:6:"config";a:2:{i:0;s:26:"field.field.node.page.body";i:1;s:14:"node.type.page";}s:6:"module";a:4:{i:0;s:12:"field_layout";i:1;s:17:"test_layout_theme";i:2;s:4:"text";i:3;s:4:"user";}}s:20:"third_party_settings";a:1:{s:12:"field_layout";a:2:{s:2:"id";s:17:"test_layout_theme";s:8:"settings";a:0:{}}}s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"g1S3_GLaxq4l3I9RIca5Mlz02MxI2KmOquZpHw59akM";}s:2:"id";s:17:"node.page.default";s:16:"targetEntityType";s:4:"node";s:6:"bundle";s:4:"page";s:4:"mode";s:7:"default";s:7:"content";a:2:{s:4:"body";a:6:{s:5:"label";s:6:"hidden";s:4:"type";s:12:"text_default";s:6:"weight";i:100;s:6:"region";s:7:"content";s:8:"settings";a:0:{}s:20:"third_party_settings";a:0:{}}s:5:"links";a:4:{s:6:"weight";i:101;s:6:"region";s:7:"content";s:8:"settings";a:0:{}s:20:"third_party_settings";a:0:{}}}s:6:"hidden";a:0:{}}',
])
->values([
'collection' => '',
'name' => 'core.entity_form_display.node.page.default',
'data' => 'a:12:{s:4:"uuid";s:36:"6d390c8a-e5aa-41ee-98d3-1d422e497283";s:8:"langcode";s:2:"en";s:6:"status";b:1;s:12:"dependencies";a:2:{s:6:"config";a:2:{i:0;s:26:"field.field.node.page.body";i:1;s:14:"node.type.page";}s:6:"module";a:4:{i:0;s:12:"field_layout";i:1;s:4:"path";i:2;s:17:"test_layout_theme";i:3;s:4:"text";}}s:20:"third_party_settings";a:1:{s:12:"field_layout";a:2:{s:2:"id";s:17:"test_layout_theme";s:8:"settings";a:0:{}}}s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"sb0qCkzU_8mNq29NehYAU8jCBXWPLeX0UN8sYFVGVcw";}s:2:"id";s:17:"node.page.default";s:16:"targetEntityType";s:4:"node";s:6:"bundle";s:4:"page";s:4:"mode";s:7:"default";s:7:"content";a:8:{s:4:"body";a:5:{s:4:"type";s:26:"text_textarea_with_summary";s:6:"weight";i:31;s:6:"region";s:7:"content";s:8:"settings";a:3:{s:4:"rows";i:9;s:12:"summary_rows";i:3;s:11:"placeholder";s:0:"";}s:20:"third_party_settings";a:0:{}}s:7:"created";a:5:{s:4:"type";s:18:"datetime_timestamp";s:6:"weight";i:10;s:6:"region";s:7:"content";s:8:"settings";a:0:{}s:20:"third_party_settings";a:0:{}}s:4:"path";a:5:{s:4:"type";s:4:"path";s:6:"weight";i:30;s:6:"region";s:7:"content";s:8:"settings";a:0:{}s:20:"third_party_settings";a:0:{}}s:7:"promote";a:5:{s:4:"type";s:16:"boolean_checkbox";s:8:"settings";a:1:{s:13:"display_label";b:1;}s:6:"weight";i:15;s:6:"region";s:7:"content";s:20:"third_party_settings";a:0:{}}s:6:"status";a:5:{s:4:"type";s:16:"boolean_checkbox";s:8:"settings";a:1:{s:13:"display_label";b:1;}s:6:"weight";i:120;s:6:"region";s:7:"content";s:20:"third_party_settings";a:0:{}}s:6:"sticky";a:5:{s:4:"type";s:16:"boolean_checkbox";s:8:"settings";a:1:{s:13:"display_label";b:1;}s:6:"weight";i:16;s:6:"region";s:7:"content";s:20:"third_party_settings";a:0:{}}s:5:"title";a:5:{s:4:"type";s:16:"string_textfield";s:6:"weight";i:-5;s:6:"region";s:7:"content";s:8:"settings";a:2:{s:4:"size";i:60;s:11:"placeholder";s:0:"";}s:20:"third_party_settings";a:0:{}}s:3:"uid";a:5:{s:4:"type";s:29:"entity_reference_autocomplete";s:6:"weight";i:5;s:6:"region";s:7:"content";s:8:"settings";a:3:{s:14:"match_operator";s:8:"CONTAINS";s:4:"size";i:60;s:11:"placeholder";s:0:"";}s:20:"third_party_settings";a:0:{}}}s:6:"hidden";a:0:{}}',
])
->execute();
@@ -0,0 +1,57 @@
<?php
namespace Drupal\Tests\layout_discovery\Functional\Update;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests the upgrade path for updating the layout discovery dependencies.
*
* @see layout_discovery_post_update_recalculate_entity_form_display_dependencies()
* @see layout_discovery_post_update_recalculate_entity_view_display_dependencies()
*
* @group layout_discovery
* @group legacy
*/
class LayoutDiscoveryDependenciesUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.theme-dependencies-in-module-key-2904550.php',
];
}
/**
* Tests updating the dependencies for layout discovery based entity displays.
*/
public function testUpdatedLayoutDiscoveryDependencies() {
$entities = [
EntityFormDisplay::load('node.page.default'),
EntityViewDisplay::load('node.page.default'),
];
foreach ($entities as $entity) {
$dependencies = $entity->getDependencies();
$this->assertTrue(in_array('test_layout_theme', $dependencies['module']));
$this->assertFalse(isset($dependencies['theme']));
}
$this->runUpdates();
$updated_entities = [
EntityFormDisplay::load('node.page.default'),
EntityViewDisplay::load('node.page.default'),
];
foreach ($updated_entities as $updated_entity) {
$dependencies = $updated_entity->getDependencies();
$this->assertFalse(in_array('test_layout_theme', $dependencies['module']));
$this->assertTrue(in_array('test_layout_theme', $dependencies['theme']));
}
}
}
@@ -0,0 +1,237 @@
<?php
namespace Drupal\Tests\layout_discovery\Kernel;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests Layout functionality.
*
* @group Layout
*/
class LayoutTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'layout_discovery', 'layout_test'];
/**
* The layout plugin manager.
*
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutPluginManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->layoutPluginManager = $this->container->get('plugin.manager.core.layout');
}
/**
* Tests that a layout provided by a theme has the preprocess function set.
*/
public function testThemeProvidedLayout() {
$this->container->get('theme_installer')->install(['test_layout_theme']);
$this->config('system.theme')->set('default', 'test_layout_theme')->save();
$theme_definitions = $this->container->get('theme.registry')->get();
$this->assertContains('template_preprocess_layout', $theme_definitions['test_layout_theme']['preprocess functions']);
}
/**
* Test rendering a layout.
*
* @dataProvider renderLayoutData
*/
public function testRenderLayout($layout_id, $config, $regions, array $html) {
$layout = $this->layoutPluginManager->createInstance($layout_id, $config);
$built['layout'] = $layout->build($regions);
$built['layout']['#prefix'] = 'Test prefix' . "\n";
$built['layout']['#suffix'] = 'Test suffix' . "\n";
// Assume each layout is contained by a form, in order to ensure the
// building of the layout does not interfere with form processing.
$form_state = new FormState();
$form_builder = $this->container->get('form_builder');
$form_builder->prepareForm('the_form_id', $built, $form_state);
$form_builder->processForm('the_form_id', $built, $form_state);
$this->render($built);
// Add in the wrapping form elements and prefix/suffix.
array_unshift($html, 'Test prefix');
array_unshift($html, '<form data-drupal-selector="the-form-id" action="/" method="post" id="the-form-id" accept-charset="UTF-8">');
// Retrieve the build ID from the rendered HTML since the string is random.
$build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML();
$form_id_input = '<input data-drupal-selector="edit-the-form-id" type="hidden" name="form_id" value="the_form_id"/>';
$html[] = 'Test suffix';
$html[] = $build_id_input . $form_id_input . '</form>';
// Match the HTML to the full form element.
$this->assertSame(implode("\n", $html), $this->cssSelect('#the-form-id')[0]->asXML());
}
/**
* {@inheritdoc}
*/
protected function render(array &$elements) {
$content = parent::render($elements);
// Strip leading whitespace from every line.
$this->content = preg_replace('/^\s+/m', '', $content);
return $this->content;
}
/**
* Data provider for testRenderLayout().
*/
public function renderLayoutData() {
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout layout--onecol">';
$html[] = '<div data-drupal-selector="edit-content" class="layout__region layout__region--content">';
$html[] = 'This is the content';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_onecol'] = [
'layout_onecol',
[],
[
'content' => [
'#markup' => 'This is the content',
],
],
$html,
];
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout-example-1col clearfix">';
$html[] = '<div data-drupal-selector="edit-top" class="region-top">';
$html[] = 'This string added by #process.';
$html[] = '</div>';
$html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
$html[] = 'This is the bottom';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_test_1col_with_form'] = [
'layout_test_1col',
[],
[
'top' => [
'#process' => [[static::class, 'processCallback']],
],
'bottom' => [
'#markup' => 'This is the bottom',
],
],
$html,
];
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout-example-1col clearfix">';
$html[] = '<div data-drupal-selector="edit-top" class="region-top">';
$html[] = 'This is the top';
$html[] = '</div>';
$html[] = '<div data-drupal-selector="edit-bottom" class="region-bottom">';
$html[] = 'This is the bottom';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_test_1col'] = [
'layout_test_1col',
[],
[
'top' => [
'#markup' => 'This is the top',
],
'bottom' => [
'#markup' => 'This is the bottom',
],
],
$html,
];
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout layout--layout-test-1col-no-template">';
$html[] = '<div data-drupal-selector="edit-top" class="layout__region layout__region--top">';
$html[] = 'This is the top';
$html[] = '</div>';
$html[] = '<div data-drupal-selector="edit-bottom" class="layout__region layout__region--bottom">';
$html[] = 'This is the bottom';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_test_1col_no_template'] = [
'layout_test_1col_no_template',
[],
[
'top' => [
'#markup' => 'This is the top',
],
'bottom' => [
'#markup' => 'This is the bottom',
],
],
$html,
];
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout-example-2col clearfix">';
$html[] = '<div data-drupal-selector="edit-left" class="class-added-by-preprocess region-left">';
$html[] = 'This is the left';
$html[] = '</div>';
$html[] = '<div data-drupal-selector="edit-right" class="region-right">';
$html[] = 'This is the right';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_test_2col'] = [
'layout_test_2col',
[],
[
'left' => [
'#markup' => 'This is the left',
],
'right' => [
'#markup' => 'This is the right',
],
],
$html,
];
$html = [];
$html[] = '<div data-drupal-selector="edit-layout" class="layout-test-plugin clearfix">';
$html[] = '<div>';
$html[] = '<span class="setting-1-label">Blah: </span>';
$html[] = 'Config value';
$html[] = '</div>';
$html[] = '<div data-drupal-selector="edit-main" class="region-main">';
$html[] = 'Main region';
$html[] = '</div>';
$html[] = '</div>';
$data['layout_test_plugin'] = [
'layout_test_plugin',
[
'setting_1' => 'Config value',
],
[
'main' => [
'#markup' => 'Main region',
],
],
$html,
];
return $data;
}
/**
* Provides a test #process callback.
*/
public static function processCallback($element) {
$element['#markup'] = 'This string added by #process.';
return $element;
}
}
@@ -0,0 +1,6 @@
name: 'Test layout theme'
type: theme
description: 'Theme for testing a theme-provided layout'
version: VERSION
base theme: classy
core: 8.x
@@ -0,0 +1,7 @@
test_layout_theme:
label: 'Test Layout - Theme'
category: 'Test Layout Theme'
template: templates/test-layout-theme
regions:
content:
label: Content