.
+ *
+ * @ingroup themeable
+ */
+#}
+{%
+set classes = [
+'field-layout--onecol',
+]
+%}
+{% if content %}
+
+{% endif %}
diff --git a/core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig b/core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig
new file mode 100644
index 0000000..4dffc01
--- /dev/null
+++ b/core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig
@@ -0,0 +1,28 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display a two column layout.
+ *
+ * Available variables:
+ * - content: The content for this layout.
+ * - attributes: HTML attributes for the layout
.
+ *
+ * @ingroup themeable
+ */
+#}
+{%
+set classes = [
+'field-layout--twocol',
+]
+%}
+{% if content %}
+
+
+ {{ content.left }}
+
+
+
+ {{ content.right }}
+
+
+{% endif %}
diff --git a/core/modules/field_layout/layouts/twocol/twocol.layout.css b/core/modules/field_layout/layouts/twocol/twocol.layout.css
new file mode 100644
index 0000000..8e2f623
--- /dev/null
+++ b/core/modules/field_layout/layouts/twocol/twocol.layout.css
@@ -0,0 +1,14 @@
+.field-layout--twocol {
+ display: flex;
+ flex-wrap: wrap;
+ justify-content: space-between;
+}
+.field-layout--twocol > .field-layout-region {
+ flex: 0 1 50%;
+ max-width: 50%;
+}
+
+.field-layout--twocol > .field-layout-region--left {
+ max-width: calc(50% - 10px);
+ margin-right: 10px;
+}
diff --git a/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php b/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php
new file mode 100644
index 0000000..54a2d52
--- /dev/null
+++ b/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php
@@ -0,0 +1,67 @@
+getDefinition($layout_id);
+ }
+
+ /**
+ * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayoutId().
+ */
+ public function getLayoutId() {
+ return $this->getThirdPartySetting('field_layout', 'id');
+ }
+
+ /**
+ * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayoutSettings().
+ */
+ public function getLayoutSettings() {
+ return $this->getThirdPartySetting('field_layout', 'settings', []);
+ }
+
+ /**
+ * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::setLayoutId().
+ */
+ public function setLayoutId($layout_id, array $layout_settings = []) {
+ $this->setThirdPartySetting('field_layout', 'id', $layout_id);
+ $this->setThirdPartySetting('field_layout', 'settings', $layout_settings);
+ return $this;
+ }
+
+ /**
+ * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::setLayout().
+ */
+ public function setLayout(LayoutInterface $layout) {
+ $this->setLayoutId($layout->getPluginId(), $layout->getConfiguration());
+ return $this;
+ }
+
+ /**
+ * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayout().
+ */
+ public function getLayout() {
+ return \Drupal::service('plugin.manager.core.layout')->createInstance($this->getLayoutId(), $this->getLayoutSettings());
+ }
+
+ /**
+ * Overrides \Drupal\Core\Entity\EntityDisplayBase::init().
+ */
+ protected function init() {
+ $this->ensureLayout();
+ parent::init();
+ }
+
+ /**
+ * Overrides \Drupal\Core\Entity\EntityDisplayBase::preSave().
+ */
+ public function preSave(EntityStorageInterface $storage) {
+ $this->ensureLayout();
+ parent::preSave($storage);
+ }
+
+ /**
+ * @todo.
+ */
+ protected function ensureLayout() {
+ if (!$this->getLayoutId()) {
+ $this->setLayoutId('onecol');
+ }
+ else {
+ $this->setLayout($this->getLayout());
+ }
+ }
+
+ /**
+ * Overrides \Drupal\Core\Entity\EntityDisplayBase::calculateDependencies().
+ *
+ * @todo Remove once https://www.drupal.org/node/2821191 is resolved.
+ *
+ * @see \Drupal\Core\Plugin\PluginDependencyTrait::calculatePluginDependencies()
+ */
+ public function calculateDependencies() {
+ parent::calculateDependencies();
+
+ // This can be called during uninstallation, so check for a valid ID first.
+ if ($this->getLayoutId()) {
+ /** @var \Drupal\Core\Layout\LayoutInterface $layout */
+ $layout = $this->getLayout();
+ $definition = $layout->getPluginDefinition();
+
+ if (!in_array($definition->getProvider(), ['core', 'component'])) {
+ $this->addDependency('module', $definition->getProvider());
+ }
+ if ($config_dependencies = $definition->getConfigDependencies()) {
+ $this->addDependencies($config_dependencies);
+ }
+ if ($layout_dependencies = $layout->calculateDependencies()) {
+ $this->addDependencies($layout_dependencies);
+ }
+ }
+ }
+
+}
diff --git a/core/modules/field_layout/src/Entity/FieldLayoutEntityFormDisplay.php b/core/modules/field_layout/src/Entity/FieldLayoutEntityFormDisplay.php
new file mode 100644
index 0000000..a6a4f2d
--- /dev/null
+++ b/core/modules/field_layout/src/Entity/FieldLayoutEntityFormDisplay.php
@@ -0,0 +1,25 @@
+getLayoutDefinition($this->getLayoutId())->getDefaultRegion();
+ }
+
+}
diff --git a/core/modules/field_layout/src/Entity/FieldLayoutEntityViewDisplay.php b/core/modules/field_layout/src/Entity/FieldLayoutEntityViewDisplay.php
new file mode 100644
index 0000000..e578686
--- /dev/null
+++ b/core/modules/field_layout/src/Entity/FieldLayoutEntityViewDisplay.php
@@ -0,0 +1,25 @@
+getLayoutDefinition($this->getLayoutId())->getDefaultRegion();
+ }
+
+}
diff --git a/core/modules/field_layout/src/FieldLayoutBuilder.php b/core/modules/field_layout/src/FieldLayoutBuilder.php
new file mode 100644
index 0000000..1fbb53a
--- /dev/null
+++ b/core/modules/field_layout/src/FieldLayoutBuilder.php
@@ -0,0 +1,125 @@
+layoutPluginManager = $layout_plugin_manager;
+ $this->entityFieldManager = $entity_field_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('plugin.manager.core.layout'),
+ $container->get('entity_field.manager')
+ );
+ }
+
+ /**
+ * Applies the layout to an entity build.
+ *
+ * @param array $build
+ * A renderable array representing the entity content or form.
+ * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
+ * The entity display holding the display options configured for the entity
+ * components.
+ * @param string $display_context
+ * The display context, either 'form' or 'view'. If in a 'form' context, an
+ * alternate method will be used to render fields in their regions.
+ */
+ public function build(array &$build, EntityDisplayWithLayoutInterface $display, $display_context) {
+ $layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
+ if ($layout_definition && $fields = $this->getFields($build, $display, $display_context)) {
+ // Add the regions to the $build in the correct order.
+ $fill = [];
+ if ($display_context === 'form') {
+ $fill['#process'][] = '\Drupal\Core\Render\Element\RenderElement::processGroup';
+ $fill['#pre_render'][] = '\Drupal\Core\Render\Element\RenderElement::preRenderGroup';
+ }
+ $regions = array_fill_keys($layout_definition->getRegionNames(), $fill);
+
+ foreach ($fields as $name => $field) {
+ // If this is a form, #group can be used to relocate the fields. This
+ // avoids breaking hook_form_alter() implementations by not actually
+ // moving the field in the form structure.
+ if ($display_context === 'form') {
+ $build[$name]['#group'] = $field['region'];
+ }
+ // Otherwise, move the field from the top-level of $build into a
+ // region-specific section.
+ else {
+ $regions[$field['region']][$name] = $build[$name];
+ unset($build[$name]);
+ }
+ }
+ $build['field_layout'] = $display->getLayout()->build($regions);
+ }
+ }
+
+ /**
+ * Gets the fields that need to be processed.
+ *
+ * @param array $build
+ * A renderable array representing the entity content or form.
+ * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
+ * The entity display holding the display options configured for the entity
+ * components.
+ * @param string $display_context
+ * The display context, either 'form' or 'view'.
+ *
+ * @return array
+ * An array of configurable fields present in the build.
+ */
+ protected function getFields(array $build, EntityDisplayWithLayoutInterface $display, $display_context) {
+ $components = $display->getComponents();
+
+ $field_definitions = $this->entityFieldManager->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle());
+ $fields_to_exclude = array_filter($field_definitions, function (FieldDefinitionInterface $field_definition) use ($display_context) {
+ // Remove fields with a non-configurable display.
+ return !$field_definition->isDisplayConfigurable($display_context);
+ });
+ $components = array_diff_key($components, $fields_to_exclude);
+
+ // Only include fields present in the build.
+ $components = array_intersect_key($components, $build);
+
+ return $components;
+ }
+
+}
diff --git a/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php
new file mode 100644
index 0000000..f1e229b
--- /dev/null
+++ b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php
@@ -0,0 +1,206 @@
+layoutPluginManager->getDefinition($this->getEntity()->getLayoutId());
+ foreach ($layout_definition->getRegions() as $name => $region) {
+ $regions[$name] = [
+ 'title' => $region['label'],
+ 'message' => $this->t('No field is displayed.'),
+ ];
+ }
+
+ $regions['hidden'] = [
+ 'title' => $this->t('Disabled', [], ['context' => 'Plural']),
+ 'message' => $this->t('No field is hidden.'),
+ ];
+
+ return $regions;
+ }
+
+ /**
+ * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::form().
+ */
+ public function form(array $form, FormStateInterface $form_state) {
+ $form = parent::form($form, $form_state);
+
+ $form['field_layouts'] = [
+ '#type' => 'details',
+ '#title' => $this->t('Layout settings'),
+ ];
+
+ $layout_plugin = $this->getLayout($this->getEntity(), $form_state);
+
+ $form['field_layouts']['field_layout'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Select a layout'),
+ '#options' => $this->layoutPluginManager->getLayoutOptions(),
+ '#default_value' => $layout_plugin->getPluginId(),
+ '#ajax' => [
+ 'callback' => '::settingsAjax',
+ 'wrapper' => 'field-layout-settings-wrapper',
+ 'trigger_as' => ['name' => 'field_layout_change'],
+ ],
+ ];
+ $form['field_layouts']['submit'] = [
+ '#type' => 'submit',
+ '#name' => 'field_layout_change',
+ '#value' => $this->t('Change layout'),
+ '#submit' => ['::settingsAjaxSubmit'],
+ '#attributes' => ['class' => ['js-hide']],
+ '#ajax' => [
+ 'callback' => '::settingsAjax',
+ 'wrapper' => 'field-layout-settings-wrapper',
+ ],
+ ];
+
+ $form['field_layouts']['settings_wrapper'] = [
+ '#type' => 'container',
+ '#id' => 'field-layout-settings-wrapper',
+ '#tree' => TRUE,
+ ];
+
+ if ($layout_plugin instanceof PluginFormInterface) {
+ $form['field_layouts']['settings_wrapper']['layout_settings'] = [];
+ $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
+ $form['field_layouts']['settings_wrapper']['layout_settings'] = $layout_plugin->buildConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
+ }
+
+ return $form;
+ }
+
+ /**
+ * Gets the layout plugin for the currently selected field layout.
+ *
+ * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $entity
+ * The current form entity.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The current state of the form.
+ *
+ * @return \Drupal\Core\Layout\LayoutInterface
+ * The layout plugin.
+ */
+ protected function getLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) {
+ if (!$layout_plugin = $form_state->get('layout_plugin')) {
+ $stored_layout_id = $entity->getLayoutId();
+ // If a new field layout was selected, use that. Otherwise use the stored
+ // layout.
+ $layout_id = $form_state->getValue('field_layout', $stored_layout_id);
+ // If the current layout is the stored layout, use the stored layout
+ // settings. Otherwise leave the settings empty.
+ $layout_settings = $layout_id === $stored_layout_id ? $entity->getLayoutSettings() : [];
+
+ $layout_plugin = $this->layoutPluginManager->createInstance($layout_id, $layout_settings);
+ $form_state->set('layout_plugin', $layout_plugin);
+ }
+ return $layout_plugin;
+ }
+
+ /**
+ * Ajax callback for the field layout settings form.
+ */
+ public static function settingsAjax($form, FormStateInterface $form_state) {
+ return $form['field_layouts']['settings_wrapper'];
+ }
+
+ /**
+ * Submit handler for the non-JS case.
+ */
+ public function settingsAjaxSubmit($form, FormStateInterface $form_state) {
+ $form_state->set('layout_plugin', NULL);
+ $form_state->setRebuild();
+ }
+
+ /**
+ * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::validateForm().
+ */
+ public function validateForm(array &$form, FormStateInterface $form_state) {
+ parent::validateForm($form, $form_state);
+
+ $layout_plugin = $this->getLayout($this->getEntity(), $form_state);
+ if ($layout_plugin instanceof PluginFormInterface) {
+ $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
+ $layout_plugin->validateConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
+ }
+ }
+
+ /**
+ * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::submitForm().
+ */
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+ parent::submitForm($form, $form_state);
+
+ $entity = $this->getEntity();
+ $layout_plugin = $this->getLayout($entity, $form_state);
+ if ($layout_plugin instanceof PluginFormInterface) {
+ $layout_plugin->submitConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state));
+ }
+
+ // If the layout is changing the regions will also change, update the
+ // placement of all fields.
+ if ($this->updateLayout($entity, $form_state)) {
+ // @todo Devise a mechanism for mapping old regions to new ones in
+ // https://www.drupal.org/node/2796877.
+ $new_region = $entity->getDefaultRegion();
+ foreach ($form_state->getValue('fields') as $field_name => $values) {
+ if (($component = $entity->getComponent($field_name)) && $new_region !== 'hidden') {
+ $component['region'] = $new_region;
+ $entity->setComponent($field_name, $component);
+ }
+ else {
+ $entity->removeComponent($field_name);
+ }
+ }
+ }
+ }
+
+ /**
+ * Updates the entity with a new layout.
+ *
+ * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $entity
+ * The display entity.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The current form state.
+ *
+ * @return bool
+ * Returns TRUE if the layout has changed, FALSE if it is the same.
+ */
+ protected function updateLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) {
+ $old_layout = $entity->getLayoutId();
+ $new_layout = $form_state->getValue('field_layout');
+ $entity->setLayout($this->getLayout($entity, $form_state));
+ return $old_layout !== $new_layout;
+ }
+
+ /**
+ * Gets the form entity.
+ *
+ * @return \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface
+ * The current form entity.
+ */
+ abstract public function getEntity();
+
+}
diff --git a/core/modules/field_layout/src/Form/FieldLayoutEntityFormDisplayEditForm.php b/core/modules/field_layout/src/Form/FieldLayoutEntityFormDisplayEditForm.php
new file mode 100644
index 0000000..c42a9d8
--- /dev/null
+++ b/core/modules/field_layout/src/Form/FieldLayoutEntityFormDisplayEditForm.php
@@ -0,0 +1,44 @@
+layoutPluginManager = $layout_plugin_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('plugin.manager.field.field_type'),
+ $container->get('plugin.manager.field.widget'),
+ $container->get('plugin.manager.core.layout')
+ );
+ }
+
+}
diff --git a/core/modules/field_layout/src/Form/FieldLayoutEntityViewDisplayEditForm.php b/core/modules/field_layout/src/Form/FieldLayoutEntityViewDisplayEditForm.php
new file mode 100644
index 0000000..92bfbde
--- /dev/null
+++ b/core/modules/field_layout/src/Form/FieldLayoutEntityViewDisplayEditForm.php
@@ -0,0 +1,44 @@
+layoutPluginManager = $layout_plugin_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('plugin.manager.field.field_type'),
+ $container->get('plugin.manager.field.formatter'),
+ $container->get('plugin.manager.core.layout')
+ );
+ }
+
+}
diff --git a/core/modules/field_layout/tests/modules/field_layout_test/config/schema/field_layout_test.schema.yml b/core/modules/field_layout/tests/modules/field_layout_test/config/schema/field_layout_test.schema.yml
new file mode 100644
index 0000000..c397e73
--- /dev/null
+++ b/core/modules/field_layout/tests/modules/field_layout_test/config/schema/field_layout_test.schema.yml
@@ -0,0 +1,7 @@
+layout_plugin.settings.field_layout_test_plugin:
+ type: layout_plugin.settings
+ label: 'Layout test plugin settings'
+ mapping:
+ setting_1:
+ type: string
+ label: 'Setting 1'
diff --git a/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.info.yml b/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.info.yml
new file mode 100644
index 0000000..4d699e4
--- /dev/null
+++ b/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.info.yml
@@ -0,0 +1,8 @@
+name: 'Field Layout test'
+type: module
+description: 'Support module for Field Layout tests.'
+core: 8.x
+package: Testing
+version: VERSION
+dependencies:
+ - entity_test
diff --git a/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.routing.yml b/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.routing.yml
new file mode 100644
index 0000000..bcea288
--- /dev/null
+++ b/core/modules/field_layout/tests/modules/field_layout_test/field_layout_test.routing.yml
@@ -0,0 +1,7 @@
+entity.entity_test.test_view_mode:
+ path: '/entity_test/{entity_test}/test'
+ defaults:
+ _entity_view: 'entity_test.test'
+ _title: 'Test test view mode'
+ requirements:
+ _entity_access: 'entity_test.view'
diff --git a/core/modules/field_layout/tests/modules/field_layout_test/src/Plugin/Layout/FieldLayoutTestPlugin.php b/core/modules/field_layout/tests/modules/field_layout_test/src/Plugin/Layout/FieldLayoutTestPlugin.php
new file mode 100644
index 0000000..ae49263
--- /dev/null
+++ b/core/modules/field_layout/tests/modules/field_layout_test/src/Plugin/Layout/FieldLayoutTestPlugin.php
@@ -0,0 +1,48 @@
+ 'Default',
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function calculateDependencies() {
+ $dependencies = parent::calculateDependencies();
+ $dependencies['module'][] = 'dependency_from_calculateDependencies';
+ return $dependencies;
+ }
+
+}
diff --git a/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php b/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php
new file mode 100644
index 0000000..35b4bdd
--- /dev/null
+++ b/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php
@@ -0,0 +1,76 @@
+createContentType([
+ 'type' => 'article',
+ ]);
+ $this->createNode([
+ 'type' => 'article',
+ 'title' => 'The node title',
+ 'body' => [[
+ 'value' => 'The node body',
+ ]],
+ ]);
+ $this->drupalLogin($this->drupalCreateUser([
+ 'access administration pages',
+ 'administer content types',
+ 'administer nodes',
+ 'administer node fields',
+ 'administer node display',
+ 'administer node form display',
+ 'view the administration theme',
+ ]));
+ }
+
+ /**
+ * Tests an entity type that has fields shown by default.
+ */
+ public function testNodeView() {
+ // By default, the one column layout is used.
+ $this->drupalGet('node/1');
+ $this->assertSession()->elementExists('css', '.field-layout--onecol');
+ $this->assertSession()->elementExists('css', '.field-layout-region--content .field--name-body');
+
+ $this->drupalGet('admin/structure/types/manage/article/display');
+ $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
+ $this->assertSession()->optionExists('fields[body][region]', 'content');
+ }
+
+ /**
+ * Gets the region titles on the page.
+ *
+ * @return string[]
+ * An array of region titles.
+ */
+ protected function getRegionTitles() {
+ $region_titles = [];
+ $region_title_elements = $this->getSession()->getPage()->findAll('css', '.region-title td');
+ /** @var \Behat\Mink\Element\NodeElement[] $region_title_elements */
+ foreach ($region_title_elements as $region_title_element) {
+ $region_titles[] = $region_title_element->getText();
+ }
+ return $region_titles;
+ }
+
+}
diff --git a/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php
new file mode 100644
index 0000000..663c0d4
--- /dev/null
+++ b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php
@@ -0,0 +1,266 @@
+ 'The name for this entity',
+ 'field_test_text' => [[
+ 'value' => 'The field test text value',
+ ]],
+ ]);
+ $entity->save();
+ $this->drupalLogin($this->drupalCreateUser([
+ 'access administration pages',
+ 'view test entity',
+ 'administer entity_test content',
+ 'administer entity_test fields',
+ 'administer entity_test display',
+ 'administer entity_test form display',
+ 'view the administration theme',
+ ]));
+ }
+
+ /**
+ * Tests that layouts are unique per view mode.
+ */
+ public function testEntityViewModes() {
+ // By default, the field is not visible.
+ $this->drupalGet('entity_test/1/test');
+ $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text');
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text');
+
+ // Change the layout for the "test" view mode. See
+ // core.entity_view_mode.entity_test.test.yml.
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ $this->click('#edit-modes');
+ $this->getSession()->getPage()->checkField('display_modes_custom[test]');
+ $this->submitForm([], 'Save');
+ $this->clickLink('configure them');
+ $this->getSession()->getPage()->pressButton('Show row weights');
+ $this->getSession()->getPage()->selectFieldOption('fields[field_test_text][region]', 'content');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+
+ // Each view mode has a different layout.
+ $this->drupalGet('entity_test/1/test');
+ $this->assertSession()->elementExists('css', '.field-layout-region--content .field--name-field-test-text');
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text');
+ }
+
+ /**
+ * Tests the use of field layout for entity form displays.
+ */
+ public function testEntityForm() {
+ // By default, the one column layout is used.
+ $this->drupalGet('entity_test/manage/1/edit');
+ $this->assertFieldInRegion('field_test_text[0][value]', 'content');
+
+ // The one column layout is in use.
+ $this->drupalGet('entity_test/structure/entity_test/form-display');
+ $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
+
+ // Switch the layout to two columns.
+ $this->click('#edit-field-layouts');
+ $this->getSession()->getPage()->selectFieldOption('field_layout', 'twocol');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+
+ // The field is moved to the default region for the new layout.
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+ $this->assertEquals(['Left', 'Right', 'Disabled'], $this->getRegionTitles());
+
+ $this->drupalGet('entity_test/manage/1/edit');
+ // No fields are visible, and the regions don't display when empty.
+ $this->assertFieldInRegion('field_test_text[0][value]', 'left');
+ $this->assertSession()->elementExists('css', '.field-layout-region--left .field--name-field-test-text');
+
+ // After a refresh the new regions are still there.
+ $this->drupalGet('entity_test/structure/entity_test/form-display');
+ $this->assertEquals(['Left', 'Right', 'Disabled'], $this->getRegionTitles());
+
+ // Drag the field to the right region.
+ $field_test_text_row = $this->getSession()->getPage()->find('css', '#field-test-text');
+ $right_region_row = $this->getSession()->getPage()->find('css', '.region-right-message');
+ $field_test_text_row->find('css', '.handle')->dragTo($right_region_row);
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+
+ // The new layout is used.
+ $this->drupalGet('entity_test/manage/1/edit');
+ $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text');
+ $this->assertFieldInRegion('field_test_text[0][value]', 'right');
+
+ // Move the field to the right region without tabledrag.
+ $this->drupalGet('entity_test/structure/entity_test/form-display');
+ $this->getSession()->getPage()->pressButton('Show row weights');
+ $this->getSession()->getPage()->selectFieldOption('fields[field_test_text][region]', 'right');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+
+ // The updated region is used.
+ $this->drupalGet('entity_test/manage/1/edit');
+ $this->assertFieldInRegion('field_test_text[0][value]', 'right');
+
+ // The layout is still in use without Field UI.
+ $this->container->get('module_installer')->uninstall(['field_ui']);
+ $this->drupalGet('entity_test/manage/1/edit');
+ $this->assertFieldInRegion('field_test_text[0][value]', 'right');
+ }
+
+ /**
+ * Tests the use of field layout for entity view displays.
+ */
+ public function testEntityView() {
+ // The one column layout is in use.
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
+
+ // Switch the layout to two columns.
+ $this->click('#edit-field-layouts');
+ $this->getSession()->getPage()->selectFieldOption('field_layout', 'twocol');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+ $this->assertEquals(['Left', 'Right', 'Disabled'], $this->getRegionTitles());
+
+ $this->drupalGet('entity_test/1');
+ // No fields are visible, and the regions don't display when empty.
+ $this->assertSession()->elementNotExists('css', '.field-layout--twocol');
+ $this->assertSession()->elementNotExists('css', '.field-layout-region');
+ $this->assertSession()->elementNotExists('css', '.field--name-field-test-text');
+
+ // After a refresh the new regions are still there.
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ $this->assertEquals(['Left', 'Right', 'Disabled'], $this->getRegionTitles());
+
+ // Drag the field to the left region.
+ $this->assertTrue($this->assertSession()->optionExists('fields[field_test_text][region]', 'hidden')->isSelected());
+ $field_test_text_row = $this->getSession()->getPage()->find('css', '#field-test-text');
+ $left_region_row = $this->getSession()->getPage()->find('css', '.region-left-message');
+ $field_test_text_row->find('css', '.handle')->dragTo($left_region_row);
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->assertFalse($this->assertSession()->optionExists('fields[field_test_text][region]', 'hidden')->isSelected());
+ $this->submitForm([], 'Save');
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+
+ // The new layout is used.
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->elementExists('css', '.field-layout--twocol');
+ $this->assertSession()->elementExists('css', '.field-layout-region--left .field--name-field-test-text');
+
+ // Move the field to the right region without tabledrag.
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ $this->getSession()->getPage()->pressButton('Show row weights');
+ $this->getSession()->getPage()->selectFieldOption('fields[field_test_text][region]', 'right');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+ $this->assertSession()->pageTextContains('Your settings have been saved.');
+
+ // The updated region is used.
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text');
+
+ // The layout is still in use without Field UI.
+ $this->container->get('module_installer')->uninstall(['field_ui']);
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->elementExists('css', '.field-layout--twocol');
+ $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text');
+ }
+
+ /**
+ * Tests layout plugins with forms.
+ */
+ public function testLayoutForms() {
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ // Switch to a field layout with settings.
+ $this->click('#edit-field-layouts');
+
+ // Test switching between layouts with and without forms.
+ $this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_test_plugin');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->assertSession()->fieldExists('settings_wrapper[layout_settings][setting_1]');
+
+ $this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_test_2col');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->assertSession()->fieldNotExists('settings_wrapper[layout_settings][setting_1]');
+
+ $this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_test_plugin');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->assertSession()->fieldExists('settings_wrapper[layout_settings][setting_1]');
+
+ // Move the test field to the content region.
+ $this->getSession()->getPage()->pressButton('Show row weights');
+ $this->getSession()->getPage()->selectFieldOption('fields[field_test_text][region]', 'content');
+ $this->assertSession()->assertWaitOnAjaxRequest();
+ $this->submitForm([], 'Save');
+
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->pageTextContains('Blah: Default');
+
+ // Update the field layout settings.
+ $this->drupalGet('entity_test/structure/entity_test/display');
+ $this->click('#edit-field-layouts');
+ $this->getSession()->getPage()->fillField('settings_wrapper[layout_settings][setting_1]', 'Test text');
+ $this->submitForm([], 'Save');
+
+ $this->drupalGet('entity_test/1');
+ $this->assertSession()->pageTextContains('Blah: Test text');
+ }
+
+ /**
+ * Gets the region titles on the page.
+ *
+ * @return string[]
+ * An array of region titles.
+ */
+ protected function getRegionTitles() {
+ $region_titles = [];
+ $region_title_elements = $this->getSession()->getPage()->findAll('css', '.region-title td');
+ /** @var \Behat\Mink\Element\NodeElement[] $region_title_elements */
+ foreach ($region_title_elements as $region_title_element) {
+ $region_titles[] = $region_title_element->getText();
+ }
+ return $region_titles;
+ }
+
+ /**
+ * Asserts that a field exists in a given region.
+ *
+ * @param string $field_selector
+ * The field selector, one of field id|name|label|value.
+ * @param string $region_name
+ * The machine name of the region.
+ */
+ protected function assertFieldInRegion($field_selector, $region_name) {
+ $region_element = $this->getSession()->getPage()->find('css', ".field-layout-region--$region_name");
+ $this->assertNotNull($region_element);
+ $this->assertSession()->fieldExists($field_selector, $region_element);
+ }
+
+}
diff --git a/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php
new file mode 100644
index 0000000..b636b15
--- /dev/null
+++ b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php
@@ -0,0 +1,202 @@
+ 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'status' => TRUE,
+ 'content' => [
+ 'foo' => ['type' => 'visible'],
+ 'bar' => ['type' => 'hidden'],
+ 'name' => ['type' => 'hidden', 'region' => 'content'],
+ ],
+ ]);
+
+ $expected = [
+ 'langcode' => 'en',
+ 'status' => TRUE,
+ 'dependencies' => [],
+ 'third_party_settings' => [
+ 'field_layout' => [
+ 'id' => 'onecol',
+ 'settings' => [],
+ ],
+ ],
+ 'id' => 'entity_test.entity_test.default',
+ 'targetEntityType' => 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'content' => [
+ 'foo' => [
+ 'type' => 'visible',
+ ],
+ 'bar' => [
+ 'type' => 'hidden',
+ ],
+ ],
+ 'hidden' => [],
+ ];
+ $this->assertEntityValues($expected, $entity_display);
+
+ $entity_display->save();
+
+ $expected = [
+ 'langcode' => 'en',
+ 'status' => TRUE,
+ 'dependencies' => [
+ 'module' => [
+ 'entity_test',
+ 'field_layout',
+ ],
+ ],
+ 'third_party_settings' => [
+ 'field_layout' => [
+ 'id' => 'onecol',
+ 'settings' => [],
+ ],
+ 'entity_test' => [
+ 'foo' => 'bar',
+ ],
+ ],
+ 'id' => 'entity_test.entity_test.default',
+ 'targetEntityType' => 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'content' => [
+ 'foo' => [
+ 'type' => 'visible',
+ 'region' => 'content',
+ ],
+ ],
+ 'hidden' => [
+ 'bar' => TRUE,
+ ],
+ ];
+ $this->assertEntityValues($expected, $entity_display);
+
+ $entity_display->setLayoutId('field_layout_test_plugin');
+ $entity_display->save();
+
+ $expected = [
+ 'langcode' => 'en',
+ 'status' => TRUE,
+ 'dependencies' => [
+ 'module' => [
+ 'dependency_from_annotation',
+ 'dependency_from_calculateDependencies',
+ 'entity_test',
+ 'field_layout',
+ 'field_layout_test',
+ ],
+ ],
+ 'third_party_settings' => [
+ 'field_layout' => [
+ 'id' => 'field_layout_test_plugin',
+ 'settings' => [
+ 'setting_1' => 'Default',
+ ],
+ ],
+ 'entity_test' => [
+ 'foo' => 'bar',
+ ],
+ ],
+ 'id' => 'entity_test.entity_test.default',
+ 'targetEntityType' => 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'content' => [
+ 'foo' => [
+ 'type' => 'visible',
+ 'region' => 'content',
+ ],
+ ],
+ 'hidden' => [
+ 'bar' => TRUE,
+ ],
+ ];
+
+ $this->assertEntityValues($expected, $entity_display);
+
+ $entity_display->setLayoutId('field_layout_test_plugin', ['setting_1' => 'foobar']);
+ $entity_display->save();
+
+ $expected = [
+ 'langcode' => 'en',
+ 'status' => TRUE,
+ 'dependencies' => [
+ 'module' => [
+ 'dependency_from_annotation',
+ 'dependency_from_calculateDependencies',
+ 'entity_test',
+ 'field_layout',
+ 'field_layout_test',
+ ],
+ ],
+ 'third_party_settings' => [
+ 'field_layout' => [
+ 'id' => 'field_layout_test_plugin',
+ 'settings' => [
+ 'setting_1' => 'foobar',
+ ],
+ ],
+ 'entity_test' => [
+ 'foo' => 'bar',
+ ],
+ ],
+ 'id' => 'entity_test.entity_test.default',
+ 'targetEntityType' => 'entity_test',
+ 'bundle' => 'entity_test',
+ 'mode' => 'default',
+ 'content' => [
+ 'foo' => [
+ 'type' => 'visible',
+ 'region' => 'content',
+ ],
+ ],
+ 'hidden' => [
+ 'bar' => TRUE,
+ ],
+ ];
+
+ $this->assertEntityValues($expected, $entity_display);
+ }
+
+ /**
+ * Asserts than an entity has the correct values.
+ *
+ * @param mixed $expected
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * @param string $message
+ */
+ public static function assertEntityValues($expected, EntityInterface $entity, $message = '') {
+ $values = $entity->toArray();
+
+ static::assertArrayHasKey('uuid', $values);
+ unset($values['uuid']);
+
+ static::assertSame($expected, $values, $message);
+ }
+
+}
diff --git a/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php
new file mode 100644
index 0000000..866110c
--- /dev/null
+++ b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php
@@ -0,0 +1,277 @@
+pluginDefinition = new LayoutDefinition([
+ 'library' => 'field_layout/drupal.field_layout.twocol',
+ 'theme_hook' => 'field_layout__twocol',
+ 'regions' => [
+ 'left' => [
+ 'label' => 'Left',
+ ],
+ 'right' => [
+ 'label' => 'Right',
+ ],
+ ],
+ ]);
+ $this->layoutPlugin = new LayoutDefault([], 'twocol', $this->pluginDefinition);
+
+ $this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
+ $this->layoutPluginManager->getDefinition('unknown', FALSE)->willReturn(NULL);
+ $this->layoutPluginManager->getDefinition('twocol', FALSE)->willReturn($this->pluginDefinition);
+
+ $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
+
+ $this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal(), $this->entityFieldManager->reveal());
+ }
+
+ /**
+ * @covers ::build
+ * @covers ::getFields
+ */
+ public function testBuildView() {
+ $definitions = [];
+ $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
+ $non_configurable_field_definition->isDisplayConfigurable('view')->willReturn(FALSE);
+ $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
+ $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
+
+ $build = [
+ 'test1' => [
+ '#markup' => 'Test1',
+ ],
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ ];
+
+ $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
+ $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
+ $display->getTargetBundle()->willReturn('the_entity_type_bundle');
+ $display->getLayout()->willReturn($this->layoutPlugin);
+ $display->getLayoutId()->willReturn('twocol');
+ $display->getLayoutSettings()->willReturn([]);
+ $display->getComponents()->willReturn([
+ 'test1' => [
+ 'region' => 'right',
+ ],
+ 'non_configurable_field' => [
+ 'region' => 'left',
+ ],
+ ]);
+
+ $display_context = 'view';
+
+ $expected = [
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ 'field_layout' => [
+ 'left' => [],
+ 'right' => [
+ 'test1' => [
+ '#markup' => 'Test1',
+ ],
+ ],
+ '#settings' => [],
+ '#layout' => $this->pluginDefinition,
+ '#theme' => 'field_layout__twocol',
+ '#attached' => [
+ 'library' => [
+ 'field_layout/drupal.field_layout.twocol',
+ ],
+ ],
+ ],
+ ];
+ $this->fieldLayoutBuilder->build($build, $display->reveal(), $display_context);
+ $this->assertEquals($expected, $build);
+ $this->assertSame($expected, $build);
+ }
+
+ /**
+ * @covers ::build
+ * @covers ::getFields
+ */
+ public function testBuildForm() {
+ $definitions = [];
+ $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
+ $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
+ $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
+ $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
+
+ $build = [
+ 'test1' => [
+ '#markup' => 'Test1',
+ ],
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ ];
+
+ $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
+ $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
+ $display->getTargetBundle()->willReturn('the_entity_type_bundle');
+ $display->getLayout()->willReturn($this->layoutPlugin);
+ $display->getLayoutId()->willReturn('twocol');
+ $display->getLayoutSettings()->willReturn([]);
+ $display->getComponents()->willReturn([
+ 'test1' => [
+ 'region' => 'right',
+ ],
+ 'non_configurable_field' => [
+ 'region' => 'left',
+ ],
+ ]);
+
+ $display_context = 'form';
+
+ $expected = [
+ 'test1' => [
+ '#markup' => 'Test1',
+ '#group' => 'right',
+ ],
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ 'field_layout' => [
+ 'left' => [
+ '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
+ '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
+ ],
+ 'right' => [
+ '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
+ '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
+ ],
+ '#settings' => [],
+ '#layout' => $this->pluginDefinition,
+ '#theme' => 'field_layout__twocol',
+ '#attached' => [
+ 'library' => [
+ 'field_layout/drupal.field_layout.twocol',
+ ],
+ ],
+ ],
+ ];
+ $this->fieldLayoutBuilder->build($build, $display->reveal(), $display_context);
+ $this->assertEquals($expected, $build);
+ $this->assertSame($expected, $build);
+ }
+
+ /**
+ * @covers ::build
+ */
+ public function testBuildEmpty() {
+ $definitions = [];
+ $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
+ $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
+ $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
+ $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
+
+ $build = [
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ ];
+
+ $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
+ $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
+ $display->getTargetBundle()->willReturn('the_entity_type_bundle');
+ $display->getLayout()->willReturn($this->layoutPlugin);
+ $display->getLayoutId()->willReturn('twocol');
+ $display->getLayoutSettings()->willReturn([]);
+ $display->getComponents()->willReturn([
+ 'test1' => [
+ 'region' => 'right',
+ ],
+ 'non_configurable_field' => [
+ 'region' => 'left',
+ ],
+ ]);
+
+ $display_context = 'form';
+
+ $expected = [
+ 'non_configurable_field' => [
+ '#markup' => 'Non-configurable',
+ ],
+ ];
+ $this->fieldLayoutBuilder->build($build, $display->reveal(), $display_context);
+ $this->assertSame($expected, $build);
+ }
+
+ /**
+ * @covers ::build
+ */
+ public function testBuildNoLayout() {
+ $this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
+
+ $build = [
+ 'test1' => [
+ '#markup' => 'Test1',
+ ],
+ ];
+
+ $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
+ $display->getLayoutId()->willReturn('unknown');
+ $display->getLayoutSettings()->willReturn([]);
+ $display->getComponents()->shouldNotBeCalled();
+
+ $display_context = 'form';
+
+ $expected = [
+ 'test1' => [
+ '#markup' => 'Test1',
+ ],
+ ];
+ $this->fieldLayoutBuilder->build($build, $display->reveal(), $display_context);
+ $this->assertSame($expected, $build);
+ }
+
+}