Initial Drupal 11 with DDEV setup
This commit is contained in:
@ -0,0 +1,16 @@
|
||||
core.entity_view_display.*.*.*.third_party.field_layout:
|
||||
type: field_layout.third_party_settings
|
||||
|
||||
core.entity_form_display.*.*.*.third_party.field_layout:
|
||||
type: field_layout.third_party_settings
|
||||
|
||||
field_layout.third_party_settings:
|
||||
type: mapping
|
||||
label: 'Per-view-mode field layout settings'
|
||||
mapping:
|
||||
id:
|
||||
type: string
|
||||
label: 'Layout ID'
|
||||
settings:
|
||||
type: layout_plugin.settings.[%parent.id]
|
||||
label: 'Layout settings'
|
||||
8
web/core/modules/field_layout/field_layout.info.yml
Normal file
8
web/core/modules/field_layout/field_layout.info.yml
Normal file
@ -0,0 +1,8 @@
|
||||
name: 'Field Layout'
|
||||
type: module
|
||||
description: 'Allows users to configure the display and form display by arranging fields in several columns.'
|
||||
package: Core (Experimental)
|
||||
lifecycle: experimental
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- drupal:layout_discovery
|
||||
47
web/core/modules/field_layout/field_layout.install
Normal file
47
web/core/modules/field_layout/field_layout.install
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains install and update functions for Field Layout.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Entity\Display\EntityDisplayInterface;
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function field_layout_install(): void {
|
||||
// Ensure each entity display has a layout.
|
||||
$entity_save = function (EntityDisplayInterface $entity) {
|
||||
if ($entity instanceof EntityDisplayWithLayoutInterface) {
|
||||
$entity->ensureLayout()->save();
|
||||
}
|
||||
};
|
||||
array_map($entity_save, EntityViewDisplay::loadMultiple());
|
||||
array_map($entity_save, EntityFormDisplay::loadMultiple());
|
||||
|
||||
// Invalidate the render cache since all content will now have a layout.
|
||||
Cache::invalidateTags(['rendered']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function field_layout_uninstall(): void {
|
||||
// Reset each entity display to use the one-column layout to best approximate
|
||||
// the absence of layouts.
|
||||
$entity_save = function (EntityDisplayInterface $entity) {
|
||||
if ($entity instanceof EntityDisplayWithLayoutInterface) {
|
||||
$entity->setLayoutId('layout_onecol')->save();
|
||||
}
|
||||
};
|
||||
array_map($entity_save, EntityViewDisplay::loadMultiple());
|
||||
array_map($entity_save, EntityFormDisplay::loadMultiple());
|
||||
|
||||
// Invalidate the render cache since all content will no longer have a layout.
|
||||
Cache::invalidateTags(['rendered']);
|
||||
}
|
||||
2
web/core/modules/field_layout/field_layout.services.yml
Normal file
2
web/core/modules/field_layout/field_layout.services.yml
Normal file
@ -0,0 +1,2 @@
|
||||
parameters:
|
||||
field_layout.skip_procedural_hook_scan: true
|
||||
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Display;
|
||||
|
||||
use Drupal\Core\Entity\Display\EntityDisplayInterface;
|
||||
use Drupal\Core\Layout\LayoutInterface;
|
||||
|
||||
/**
|
||||
* Provides a common interface for entity displays that have layout.
|
||||
*/
|
||||
interface EntityDisplayWithLayoutInterface extends EntityDisplayInterface {
|
||||
|
||||
/**
|
||||
* Gets the default region.
|
||||
*
|
||||
* @return string
|
||||
* The default region for this display.
|
||||
*/
|
||||
public function getDefaultRegion();
|
||||
|
||||
/**
|
||||
* Gets the layout plugin ID for this display.
|
||||
*
|
||||
* @return string
|
||||
* The layout plugin ID.
|
||||
*/
|
||||
public function getLayoutId();
|
||||
|
||||
/**
|
||||
* Gets the layout plugin settings for this display.
|
||||
*
|
||||
* @return mixed[]
|
||||
* The layout plugin settings.
|
||||
*/
|
||||
public function getLayoutSettings();
|
||||
|
||||
/**
|
||||
* Sets the layout plugin ID for this display.
|
||||
*
|
||||
* @param string|null $layout_id
|
||||
* Either a valid layout plugin ID, or NULL to remove the layout setting.
|
||||
* @param array $layout_settings
|
||||
* (optional) An array of settings for this layout.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLayoutId($layout_id, array $layout_settings = []);
|
||||
|
||||
/**
|
||||
* Sets the layout plugin for this display.
|
||||
*
|
||||
* @param \Drupal\Core\Layout\LayoutInterface $layout
|
||||
* A layout plugin.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLayout(LayoutInterface $layout);
|
||||
|
||||
/**
|
||||
* Gets the layout plugin for this display.
|
||||
*
|
||||
* @return \Drupal\Core\Layout\LayoutInterface
|
||||
* The layout plugin.
|
||||
*/
|
||||
public function getLayout();
|
||||
|
||||
/**
|
||||
* Ensures this entity has a layout.
|
||||
*
|
||||
* @param string $default_layout_id
|
||||
* (optional) The layout ID to use as a default. Defaults to
|
||||
* 'layout_onecol'.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function ensureLayout($default_layout_id = 'layout_onecol');
|
||||
|
||||
}
|
||||
@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Entity;
|
||||
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Layout\LayoutInterface;
|
||||
|
||||
/**
|
||||
* Provides shared code for entity displays.
|
||||
*
|
||||
* Both EntityViewDisplay and EntityFormDisplay must maintain their parent
|
||||
* hierarchy, while being identically enhanced by Field Layout. This trait
|
||||
* contains the code they both share.
|
||||
*/
|
||||
trait FieldLayoutEntityDisplayTrait {
|
||||
|
||||
/**
|
||||
* Gets a layout definition.
|
||||
*
|
||||
* @param string $layout_id
|
||||
* The layout ID.
|
||||
*
|
||||
* @return \Drupal\Core\Layout\LayoutDefinition
|
||||
* The layout definition.
|
||||
*/
|
||||
protected function getLayoutDefinition($layout_id) {
|
||||
return \Drupal::service('plugin.manager.core.layout')->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 = []) {
|
||||
if ($this->getLayoutId() !== $layout_id) {
|
||||
// @todo Devise a mechanism for mapping old regions to new ones in
|
||||
// https://www.drupal.org/node/2796877.
|
||||
$layout_definition = $this->getLayoutDefinition($layout_id);
|
||||
$new_region = $layout_definition->getDefaultRegion();
|
||||
$layout_regions = $layout_definition->getRegions();
|
||||
foreach ($this->getComponents() as $name => $component) {
|
||||
if (isset($component['region']) && !isset($layout_regions[$component['region']])) {
|
||||
$component['region'] = $new_region;
|
||||
$this->setComponent($name, $component);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->setThirdPartySetting('field_layout', 'id', $layout_id);
|
||||
// Instantiate the plugin and consult it for the updated plugin
|
||||
// configuration. Once layouts are no longer stored as third party settings,
|
||||
// this will be handled by the code in
|
||||
// \Drupal\Core\Config\Entity\ConfigEntityBase::set() that handles
|
||||
// \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
||||
$layout_settings = $this->doGetLayout($layout_id, $layout_settings)->getConfiguration();
|
||||
$this->setThirdPartySetting('field_layout', 'settings', $layout_settings);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setLayout(LayoutInterface $layout) {
|
||||
$this->setLayoutId($layout->getPluginId(), $layout->getConfiguration());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLayout() {
|
||||
return $this->doGetLayout($this->getLayoutId(), $this->getLayoutSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the layout plugin.
|
||||
*
|
||||
* @param string $layout_id
|
||||
* A layout plugin ID.
|
||||
* @param array $layout_settings
|
||||
* An array of settings.
|
||||
*
|
||||
* @return \Drupal\Core\Layout\LayoutInterface
|
||||
* The layout plugin.
|
||||
*/
|
||||
protected function doGetLayout($layout_id, array $layout_settings) {
|
||||
return \Drupal::service('plugin.manager.core.layout')->createInstance($layout_id, $layout_settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\Core\Entity\EntityDisplayBase::init().
|
||||
*/
|
||||
protected function init() {
|
||||
$this->ensureLayout();
|
||||
parent::init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\Core\Entity\EntityDisplayBase::preSave().
|
||||
*/
|
||||
public function preSave(EntityStorageInterface $storage) {
|
||||
parent::preSave($storage);
|
||||
|
||||
// Ensure the plugin configuration is updated. Once layouts are no longer
|
||||
// stored as third party settings, this will be handled by the code in
|
||||
// \Drupal\Core\Config\Entity\ConfigEntityBase::preSave() that handles
|
||||
// \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
||||
if ($this->getLayoutId()) {
|
||||
$this->setLayout($this->getLayout());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function ensureLayout($default_layout_id = 'layout_onecol') {
|
||||
if (!$this->getLayoutId()) {
|
||||
$this->setLayoutId($default_layout_id);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\Core\Entity\EntityDisplayBase::calculateDependencies().
|
||||
*
|
||||
* Ensure the plugin dependencies are included. Once layouts are no longer
|
||||
* stored as third party settings, this will be handled by the code in
|
||||
* \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies() that
|
||||
* handles \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
parent::calculateDependencies();
|
||||
|
||||
// This can be called during uninstallation, so check for a valid ID first.
|
||||
if ($this->getLayoutId()) {
|
||||
$this->calculatePluginDependencies($this->getLayout());
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getDefaultRegion().
|
||||
*/
|
||||
public function getDefaultRegion() {
|
||||
return $this->getLayoutDefinition($this->getLayoutId())->getDefaultRegion();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Entity;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
|
||||
/**
|
||||
* Provides an entity form display entity that has a layout.
|
||||
*/
|
||||
class FieldLayoutEntityFormDisplay extends EntityFormDisplay implements EntityDisplayWithLayoutInterface {
|
||||
|
||||
use FieldLayoutEntityDisplayTrait;
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Entity;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
|
||||
/**
|
||||
* Provides an entity view display entity that has a layout.
|
||||
*/
|
||||
class FieldLayoutEntityViewDisplay extends EntityViewDisplay implements EntityDisplayWithLayoutInterface {
|
||||
|
||||
use FieldLayoutEntityDisplayTrait;
|
||||
|
||||
}
|
||||
161
web/core/modules/field_layout/src/FieldLayoutBuilder.php
Normal file
161
web/core/modules/field_layout/src/FieldLayoutBuilder.php
Normal file
@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Drupal\Core\Entity\EntityFieldManagerInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
use Drupal\Core\Layout\LayoutPluginManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Builds a field layout.
|
||||
*/
|
||||
class FieldLayoutBuilder implements ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* The layout plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
|
||||
*/
|
||||
protected $layoutPluginManager;
|
||||
|
||||
/**
|
||||
* The entity field manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
|
||||
*/
|
||||
protected $entityFieldManager;
|
||||
|
||||
/**
|
||||
* Constructs a new FieldLayoutBuilder.
|
||||
*
|
||||
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
|
||||
* The layout plugin manager.
|
||||
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
|
||||
* The entity field manager.
|
||||
*/
|
||||
public function __construct(LayoutPluginManagerInterface $layout_plugin_manager, EntityFieldManagerInterface $entity_field_manager) {
|
||||
$this->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.
|
||||
*/
|
||||
public function buildView(array &$build, EntityDisplayWithLayoutInterface $display) {
|
||||
$layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
|
||||
if ($layout_definition && $fields = $this->getFields($build, $display, 'view')) {
|
||||
// Add the regions to the $build in the correct order.
|
||||
$regions = array_fill_keys($layout_definition->getRegionNames(), []);
|
||||
|
||||
foreach ($fields as $name => $field) {
|
||||
// If the region is controlled by the layout, move the field from the
|
||||
// top-level of $build into a region-specific section. Custom regions
|
||||
// could be set by other code at run-time; these should be ignored.
|
||||
// @todo Ideally the array structure would remain unchanged, see
|
||||
// https://www.drupal.org/node/2846393.
|
||||
if (isset($regions[$field['region']])) {
|
||||
$regions[$field['region']][$name] = $build[$name];
|
||||
unset($build[$name]);
|
||||
}
|
||||
}
|
||||
// Ensure this will not conflict with any existing array elements by
|
||||
// prefixing with an underscore.
|
||||
$build['_field_layout'] = $display->getLayout()->build($regions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the layout to an entity form.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
public function buildForm(array &$build, EntityDisplayWithLayoutInterface $display) {
|
||||
$layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
|
||||
if ($layout_definition && $fields = $this->getFields($build, $display, 'form')) {
|
||||
$fill = [];
|
||||
$fill['#process'][] = '\Drupal\Core\Render\Element\RenderElementBase::processGroup';
|
||||
$fill['#pre_render'][] = '\Drupal\Core\Render\Element\RenderElementBase::preRenderGroup';
|
||||
// Add the regions to the $build in the correct order.
|
||||
$regions = array_fill_keys($layout_definition->getRegionNames(), $fill);
|
||||
|
||||
foreach ($fields as $name => $field) {
|
||||
// As 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 a #group is already set,
|
||||
// do not overwrite it.
|
||||
if (isset($regions[$field['region']]) && !isset($build[$name]['#group'])) {
|
||||
if (!empty($build['#parents'])) {
|
||||
$build[$name]['#group'] = implode('][', array_merge($build['#parents'], ['_field_layout', $field['region']]));
|
||||
}
|
||||
else {
|
||||
$build[$name]['#group'] = $field['region'];
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ensure this will not conflict with any existing array elements by
|
||||
// prefixing with an underscore.
|
||||
$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();
|
||||
|
||||
// Ignore any extra fields from the list of field definitions. Field
|
||||
// definitions can have a non-configurable display, but all extra fields are
|
||||
// always displayed.
|
||||
$field_definitions = array_diff_key(
|
||||
$this->entityFieldManager->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle()),
|
||||
$this->entityFieldManager->getExtraFields($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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Form;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\SubformState;
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
|
||||
/**
|
||||
* Provides shared code for entity display forms.
|
||||
*
|
||||
* Both EntityViewDisplayEditForm and EntityFormDisplayEditForm must maintain
|
||||
* their parent hierarchy, while being identically enhanced by Field Layout.
|
||||
* This trait contains the code they both share.
|
||||
*/
|
||||
trait FieldLayoutEntityDisplayFormTrait {
|
||||
|
||||
/**
|
||||
* The field layout plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Layout\LayoutPluginManagerInterface
|
||||
*/
|
||||
protected $layoutPluginManager;
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::getRegions().
|
||||
*/
|
||||
public function getRegions() {
|
||||
$regions = [];
|
||||
|
||||
$layout_definition = $this->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,
|
||||
];
|
||||
|
||||
$form['field_layouts']['settings_wrapper']['icon'] = $layout_plugin->getPluginDefinition()->getIcon();
|
||||
|
||||
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();
|
||||
// Use selected layout if it exists, falling back to 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) {
|
||||
$subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
|
||||
$layout_plugin->submitConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
|
||||
}
|
||||
|
||||
$entity->setLayout($layout_plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the form entity.
|
||||
*
|
||||
* @return \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface
|
||||
* The current form entity.
|
||||
*/
|
||||
abstract public function getEntity();
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Form;
|
||||
|
||||
use Drupal\Component\Plugin\PluginManagerBase;
|
||||
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
|
||||
use Drupal\Core\Entity\EntityFieldManagerInterface;
|
||||
use Drupal\Core\Field\FieldTypePluginManagerInterface;
|
||||
use Drupal\Core\Layout\LayoutPluginManagerInterface;
|
||||
use Drupal\field_ui\Form\EntityFormDisplayEditForm;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Edit form for the EntityFormDisplay entity type.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class FieldLayoutEntityFormDisplayEditForm extends EntityFormDisplayEditForm {
|
||||
|
||||
use FieldLayoutEntityDisplayFormTrait;
|
||||
|
||||
/**
|
||||
* FieldLayoutEntityFormDisplayEditForm constructor.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
|
||||
* The field type manager.
|
||||
* @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
|
||||
* The widget plugin manager.
|
||||
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
|
||||
* The layout plugin manager.
|
||||
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
|
||||
* The entity display_repository.
|
||||
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
|
||||
* The entity field manager.
|
||||
*/
|
||||
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, LayoutPluginManagerInterface $layout_plugin_manager, ?EntityDisplayRepositoryInterface $entity_display_repository = NULL, ?EntityFieldManagerInterface $entity_field_manager = NULL) {
|
||||
parent::__construct($field_type_manager, $plugin_manager, $entity_display_repository, $entity_field_manager);
|
||||
$this->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'),
|
||||
$container->get('entity_display.repository'),
|
||||
$container->get('entity_field.manager')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Form;
|
||||
|
||||
use Drupal\Component\Plugin\PluginManagerBase;
|
||||
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
|
||||
use Drupal\Core\Entity\EntityFieldManagerInterface;
|
||||
use Drupal\Core\Field\FieldTypePluginManagerInterface;
|
||||
use Drupal\Core\Layout\LayoutPluginManagerInterface;
|
||||
use Drupal\field_ui\Form\EntityViewDisplayEditForm;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Edit form for the EntityViewDisplay entity type.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class FieldLayoutEntityViewDisplayEditForm extends EntityViewDisplayEditForm {
|
||||
|
||||
use FieldLayoutEntityDisplayFormTrait;
|
||||
|
||||
/**
|
||||
* FieldLayoutEntityViewDisplayEditForm constructor.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
|
||||
* The field type manager.
|
||||
* @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
|
||||
* The formatter plugin manager.
|
||||
* @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
|
||||
* The field layout plugin manager.
|
||||
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
|
||||
* The entity display_repository.
|
||||
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
|
||||
* The entity field manager.
|
||||
*/
|
||||
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, LayoutPluginManagerInterface $layout_plugin_manager, ?EntityDisplayRepositoryInterface $entity_display_repository = NULL, ?EntityFieldManagerInterface $entity_field_manager = NULL) {
|
||||
parent::__construct($field_type_manager, $plugin_manager, $entity_display_repository, $entity_field_manager);
|
||||
$this->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'),
|
||||
$container->get('entity_display.repository'),
|
||||
$container->get('entity_field.manager')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
81
web/core/modules/field_layout/src/Hook/FieldLayoutHooks.php
Normal file
81
web/core/modules/field_layout/src/Hook/FieldLayoutHooks.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\field_layout\Hook;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityFormInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\field_layout\FieldLayoutBuilder;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\field_layout\Form\FieldLayoutEntityFormDisplayEditForm;
|
||||
use Drupal\field_layout\Form\FieldLayoutEntityViewDisplayEditForm;
|
||||
use Drupal\field_layout\Entity\FieldLayoutEntityFormDisplay;
|
||||
use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Hook\Attribute\Hook;
|
||||
|
||||
/**
|
||||
* Hook implementations for field_layout.
|
||||
*/
|
||||
class FieldLayoutHooks {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
#[Hook('help')]
|
||||
public function help($route_name, RouteMatchInterface $route_match): ?string {
|
||||
switch ($route_name) {
|
||||
case 'help.page.field_layout':
|
||||
$output = '<h2>' . $this->t('About') . '</h2>';
|
||||
$output .= '<p>' . $this->t('The Field Layout module allows you to arrange fields into regions on forms and displays of entities such as nodes and users.') . '</p>';
|
||||
$output .= '<p>' . $this->t('For more information, see the <a href=":field-layout-documentation">online documentation for the Field Layout module</a>.', [
|
||||
':field-layout-documentation' => 'https://www.drupal.org/documentation/modules/field_layout',
|
||||
]) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_type_alter().
|
||||
*/
|
||||
#[Hook('entity_type_alter')]
|
||||
public function entityTypeAlter(array &$entity_types) : void {
|
||||
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
|
||||
$entity_types['entity_view_display']->setClass(FieldLayoutEntityViewDisplay::class);
|
||||
$entity_types['entity_form_display']->setClass(FieldLayoutEntityFormDisplay::class);
|
||||
// The form classes are only needed when Field UI is installed.
|
||||
if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
|
||||
$entity_types['entity_view_display']->setFormClass('edit', FieldLayoutEntityViewDisplayEditForm::class);
|
||||
$entity_types['entity_form_display']->setFormClass('edit', FieldLayoutEntityFormDisplayEditForm::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_view_alter().
|
||||
*/
|
||||
#[Hook('entity_view_alter')]
|
||||
public function entityViewAlter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void {
|
||||
if ($display instanceof EntityDisplayWithLayoutInterface) {
|
||||
\Drupal::classResolver(FieldLayoutBuilder::class)->buildView($build, $display);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_alter().
|
||||
*/
|
||||
#[Hook('form_alter')]
|
||||
public function formAlter(&$form, FormStateInterface $form_state, $form_id) : void {
|
||||
$form_object = $form_state->getFormObject();
|
||||
if ($form_object instanceof ContentEntityFormInterface && ($display = $form_object->getFormDisplay($form_state))) {
|
||||
if ($display instanceof EntityDisplayWithLayoutInterface) {
|
||||
\Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form, $display);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
layout_plugin.settings.test_layout_main_and_footer:
|
||||
type: layout_plugin.settings
|
||||
label: 'Layout test plugin settings'
|
||||
mapping:
|
||||
setting_1:
|
||||
type: string
|
||||
label: 'Setting 1'
|
||||
@ -0,0 +1,7 @@
|
||||
name: 'Field Layout test'
|
||||
type: module
|
||||
description: 'Support module for Field Layout tests.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- drupal:entity_test
|
||||
@ -0,0 +1,14 @@
|
||||
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'
|
||||
field_layout_test.embedded_form:
|
||||
path: '/field-layout-embedded-form'
|
||||
defaults:
|
||||
_form: 'Drupal\field_layout_test\Form\EmbeddedForm'
|
||||
_title: 'Embedded entity form'
|
||||
requirements:
|
||||
_permission: 'access administration pages'
|
||||
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\field_layout_test\Form;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Form\SubformState;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field_layout\FieldLayoutBuilder;
|
||||
|
||||
/**
|
||||
* Provides the EmbeddedForm class.
|
||||
*
|
||||
* @package Drupal\field_layout_test\Form
|
||||
*/
|
||||
class EmbeddedForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'field_layout_test_embedded_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['foo'] = [
|
||||
'#type' => 'fieldset',
|
||||
'#title' => $this->t('Wrapper'),
|
||||
'#tree' => TRUE,
|
||||
'#parents' => ['foo'],
|
||||
];
|
||||
$entity = EntityTest::load(1);
|
||||
if ($entity) {
|
||||
if ($entity) {
|
||||
$display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
|
||||
$subform_state = SubformState::createForSubform($form['foo'], $form, $form_state);
|
||||
$display->buildForm($entity, $form['foo'], $subform_state);
|
||||
\Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form['foo'], $display, $subform_state);
|
||||
}
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\field_layout_test\Hook;
|
||||
|
||||
use Drupal\Core\Hook\Attribute\Hook;
|
||||
|
||||
/**
|
||||
* Hook implementations for field_layout_test.
|
||||
*/
|
||||
class FieldLayoutTestHooks {
|
||||
|
||||
/**
|
||||
* Implements hook_layout_alter().
|
||||
*/
|
||||
#[Hook('layout_alter')]
|
||||
public function layoutAlter(&$definitions): void {
|
||||
/** @var \Drupal\Core\Layout\LayoutDefinition[] $definitions */
|
||||
if (\Drupal::state()->get('field_layout_test.alter_regions') && isset($definitions['layout_onecol'])) {
|
||||
$definitions['layout_onecol']->setRegions(['foo' => ['label' => 'Foo']]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\field_layout_test\Plugin\Layout;
|
||||
|
||||
use Drupal\Core\Layout\Attribute\Layout;
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
/**
|
||||
* Provides a Layout plugin for field_layout tests.
|
||||
*/
|
||||
#[Layout(
|
||||
id: 'test_layout_content_and_footer',
|
||||
label: new TranslatableMarkup('Test plugin: Content and Footer'),
|
||||
category: new TranslatableMarkup('Layout test'),
|
||||
description: new TranslatableMarkup('Test layout'),
|
||||
regions: [
|
||||
"content" => [
|
||||
"label" => new TranslatableMarkup("Content Region"),
|
||||
],
|
||||
"footer" => [
|
||||
"label" => new TranslatableMarkup("Footer Region"),
|
||||
],
|
||||
],
|
||||
)]
|
||||
class TestLayoutContentFooter extends LayoutDefault {
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\field_layout_test\Plugin\Layout;
|
||||
|
||||
use Drupal\Core\Layout\Attribute\Layout;
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
/**
|
||||
* Provides an attributed layout plugin for field_layout tests.
|
||||
*/
|
||||
#[Layout(
|
||||
id: 'test_layout_main_and_footer',
|
||||
label: new TranslatableMarkup('Test plugin: Main and Footer'),
|
||||
category: new TranslatableMarkup('Layout test'),
|
||||
description: new TranslatableMarkup('Test layout'),
|
||||
regions: [
|
||||
"main" => [
|
||||
"label" => new TranslatableMarkup("Main Region"),
|
||||
],
|
||||
"footer" => [
|
||||
"label" => new TranslatableMarkup("Footer Region"),
|
||||
],
|
||||
],
|
||||
config_dependencies: [
|
||||
"module" => [
|
||||
"layout_discovery",
|
||||
],
|
||||
],
|
||||
)]
|
||||
class TestLayoutMainFooter extends LayoutDefault {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return [
|
||||
'setting_1' => 'Default',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function calculateDependencies() {
|
||||
$dependencies = parent::calculateDependencies();
|
||||
$dependencies['module'][] = 'system';
|
||||
return $dependencies;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests using field layout for entity displays.
|
||||
*
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'field_layout',
|
||||
'field_ui',
|
||||
'node',
|
||||
'field_layout_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->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(): void {
|
||||
// By default, the one-column layout is used.
|
||||
$this->drupalGet('node/1');
|
||||
$this->assertSession()->elementExists('css', '.layout--onecol');
|
||||
$this->assertSession()->elementTextContains('css', '.layout__region--content', 'The node body');
|
||||
|
||||
$this->drupalGet('admin/structure/types/manage/article/display');
|
||||
$this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
|
||||
$this->assertSession()->optionExists('fields[body][region]', 'content');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that changes to the regions still leave the fields visible.
|
||||
*/
|
||||
public function testRegionChanges(): void {
|
||||
$this->drupalGet('admin/structure/types/manage/article/display');
|
||||
$this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
|
||||
$this->assertSession()->optionExists('fields[body][region]', 'content');
|
||||
|
||||
\Drupal::state()->set('field_layout_test.alter_regions', TRUE);
|
||||
\Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
|
||||
|
||||
$this->drupalGet('admin/structure/types/manage/article/display');
|
||||
$this->assertEquals(['Foo', 'Disabled'], $this->getRegionTitles());
|
||||
$this->assertSession()->optionExists('fields[body][region]', 'hidden');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the region titles on the page.
|
||||
*
|
||||
* @return string[]
|
||||
* An array of region titles.
|
||||
*/
|
||||
protected function getRegionTitles(): array {
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\Functional;
|
||||
|
||||
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
|
||||
|
||||
/**
|
||||
* Generic module test for field_layout.
|
||||
*
|
||||
* @group field_layout
|
||||
*/
|
||||
class GenericTest extends GenericModuleTestBase {}
|
||||
@ -0,0 +1,321 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\FunctionalJavascript;
|
||||
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
|
||||
|
||||
/**
|
||||
* Tests using field layout for entity displays.
|
||||
*
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutTest extends WebDriverTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'field_layout',
|
||||
'field_ui',
|
||||
'field_layout_test',
|
||||
'layout_test',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$entity = EntityTest::create([
|
||||
'name' => '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(): void {
|
||||
// By default, the field is not visible.
|
||||
$this->drupalGet('entity_test/1/test');
|
||||
$this->assertSession()->elementNotExists('css', '.layout__region--content ');
|
||||
$this->drupalGet('entity_test/1');
|
||||
$this->assertSession()->elementNotExists('css', '.layout__region--content');
|
||||
|
||||
// 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->submitForm([], 'Save');
|
||||
|
||||
// Each view mode has a different layout.
|
||||
$this->drupalGet('entity_test/1/test');
|
||||
$this->assertSession()->elementTextContains('css', '.layout__region--content', 'The field test text value');
|
||||
$this->drupalGet('entity_test/1');
|
||||
$this->assertSession()->elementNotExists('css', '.layout__region--content');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the use of field layout for entity form displays.
|
||||
*/
|
||||
public function testEntityForm(): void {
|
||||
// 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', '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(['Top', 'First', 'Second', 'Bottom', '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]', 'first');
|
||||
$this->assertSession()->elementExists('css', '.layout__region--first .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(['Top', 'First', 'Second', 'Bottom', 'Disabled'], $this->getRegionTitles());
|
||||
$this->assertSession()->waitForElement('css', '.tabledrag-handle');
|
||||
$id = $this->getSession()->getPage()->find('css', '[name="form_build_id"]')->getValue();
|
||||
|
||||
// Drag the field to the second region.
|
||||
$field_test_text_row = $this->getSession()->getPage()->find('css', '#field-test-text');
|
||||
$second_region_row = $this->getSession()->getPage()->find('css', '.region-second-message');
|
||||
$field_test_text_row->find('css', '.handle')->dragTo($second_region_row);
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$this->assertSession()->waitForElement('css', "[name='form_build_id']:not([value='$id'])");
|
||||
$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', '.layout__region--second .field--name-field-test-text');
|
||||
$this->assertFieldInRegion('field_test_text[0][value]', 'second');
|
||||
|
||||
// Tests if this layout works in an embedded context.
|
||||
$this->drupalGet('/field-layout-embedded-form');
|
||||
$this->assertSession()->elementExists('css', '.layout__region--second .field--name-field-test-text');
|
||||
$this->assertFieldInRegion('foo[field_test_text][0][value]', 'second');
|
||||
|
||||
// Move the field to the second 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]', 'second');
|
||||
$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]', 'second');
|
||||
|
||||
// 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]', 'second');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the use of field layout for entity view displays.
|
||||
*/
|
||||
public function testEntityView(): void {
|
||||
// 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', 'layout_twocol');
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$this->submitForm([], 'Save');
|
||||
|
||||
$this->assertSession()->pageTextContains('Your settings have been saved.');
|
||||
$this->assertEquals(['Top', 'First', 'Second', 'Bottom', 'Disabled'], $this->getRegionTitles());
|
||||
|
||||
$this->drupalGet('entity_test/1');
|
||||
// No fields are visible, and the regions don't display when empty.
|
||||
$this->assertSession()->elementNotExists('css', '.layout--twocol');
|
||||
$this->assertSession()->elementNotExists('css', '.layout__region');
|
||||
$this->assertSession()->pageTextNotContains('The field test text value');
|
||||
|
||||
// After a refresh the new regions are still there.
|
||||
$this->drupalGet('entity_test/structure/entity_test/display');
|
||||
$this->assertEquals(['Top', 'First', 'Second', 'Bottom', 'Disabled'], $this->getRegionTitles());
|
||||
$this->assertSession()->waitForElement('css', '.tabledrag-handle');
|
||||
$id = $this->getSession()->getPage()->find('css', '[name="form_build_id"]')->getValue();
|
||||
|
||||
// Drag the field to the first 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');
|
||||
$first_region_row = $this->getSession()->getPage()->find('css', '.region-first-message');
|
||||
$field_test_text_row->find('css', '.handle')->dragTo($first_region_row);
|
||||
$this->assertSession()->assertWaitOnAjaxRequest();
|
||||
$this->assertFalse($this->assertSession()->optionExists('fields[field_test_text][region]', 'hidden')->isSelected());
|
||||
$this->assertSession()->waitForElement('css', "[name='form_build_id']:not([value='$id'])");
|
||||
$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', '.layout--twocol');
|
||||
$this->assertSession()->elementTextContains('css', '.layout__region--first', 'The field test text value');
|
||||
|
||||
// Move the field to the second 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]', 'second');
|
||||
$this->submitForm([], 'Save');
|
||||
$this->assertSession()->pageTextContains('Your settings have been saved.');
|
||||
|
||||
// The updated region is used.
|
||||
$this->drupalGet('entity_test/1');
|
||||
$this->assertSession()->elementTextContains('css', '.layout__region--second', 'The field test text value');
|
||||
|
||||
// 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', '.layout--twocol');
|
||||
$this->assertSession()->elementTextContains('css', '.layout__region--second', 'The field test text value');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests layout plugins with forms.
|
||||
*/
|
||||
public function testLayoutForms(): void {
|
||||
$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()->assertExpectedAjaxRequest(1);
|
||||
$this->assertSession()->fieldExists('settings_wrapper[layout_settings][setting_1]');
|
||||
|
||||
$this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_test_2col');
|
||||
$this->assertSession()->assertExpectedAjaxRequest(2);
|
||||
$this->assertSession()->fieldNotExists('settings_wrapper[layout_settings][setting_1]');
|
||||
|
||||
$this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_test_plugin');
|
||||
$this->assertSession()->assertExpectedAjaxRequest(3);
|
||||
$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->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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests changing the formatter and region at the same time.
|
||||
*/
|
||||
public function testChangingFormatterAndRegion(): void {
|
||||
$assert_session = $this->assertSession();
|
||||
$page = $this->getSession()->getPage();
|
||||
|
||||
// Add the test field to the content region.
|
||||
$this->drupalGet('entity_test/structure/entity_test/display');
|
||||
$page->find('css', '#field-test-text .handle')->dragTo($page->find('css', '.region-content-message'));
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$page->pressButton('Save');
|
||||
$assert_session->fieldValueEquals('fields[field_test_text][region]', 'content');
|
||||
$assert_session->fieldValueEquals('fields[field_test_text][type]', 'text_default');
|
||||
|
||||
// Switch the layout to two columns.
|
||||
$this->click('#edit-field-layouts');
|
||||
$page->selectFieldOption('field_layout', 'layout_twocol');
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$page->pressButton('Save');
|
||||
$assert_session->fieldValueEquals('fields[field_test_text][region]', 'first');
|
||||
|
||||
// Change the formatter and move to another region.
|
||||
$page->selectFieldOption('fields[field_test_text][type]', 'text_trimmed');
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$page->find('css', '#field-test-text .handle')->dragTo($page->find('css', '.region-second-message'));
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$page->pressButton('Save');
|
||||
|
||||
// Assert that both the formatter and region change are persisted.
|
||||
$assert_session->fieldValueEquals('fields[field_test_text][region]', 'second');
|
||||
$assert_session->fieldValueEquals('fields[field_test_text][type]', 'text_trimmed');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the region titles on the page.
|
||||
*
|
||||
* @return string[]
|
||||
* An array of region titles.
|
||||
*/
|
||||
protected function getRegionTitles(): array {
|
||||
$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.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
protected function assertFieldInRegion(string $field_selector, string $region_name): void {
|
||||
$region_element = $this->getSession()->getPage()->find('css', ".layout__region--$region_name");
|
||||
$this->assertNotNull($region_element);
|
||||
$this->assertSession()->fieldExists($field_selector, $region_element);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\Kernel;
|
||||
|
||||
use Drupal\field_layout\Entity\FieldLayoutEntityViewDisplay;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field_layout\Entity\FieldLayoutEntityDisplayTrait
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutEntityDisplayTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = [
|
||||
'layout_discovery',
|
||||
'field_layout',
|
||||
'entity_test',
|
||||
'field_layout_test',
|
||||
'field_test',
|
||||
'system',
|
||||
];
|
||||
|
||||
/**
|
||||
* @covers ::preSave
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testPreSave(): void {
|
||||
// Create an entity display with one hidden and one visible field.
|
||||
$entity_display = FieldLayoutEntityViewDisplay::create([
|
||||
'targetEntityType' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'mode' => 'default',
|
||||
'status' => TRUE,
|
||||
'content' => [
|
||||
'foo' => ['type' => 'field_no_settings'],
|
||||
'name' => ['type' => 'hidden', 'region' => 'content'],
|
||||
],
|
||||
'hidden' => [
|
||||
'bar' => TRUE,
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'langcode' => 'en',
|
||||
'status' => TRUE,
|
||||
'dependencies' => [],
|
||||
'third_party_settings' => [
|
||||
'field_layout' => [
|
||||
'id' => 'layout_onecol',
|
||||
'settings' => [
|
||||
'label' => '',
|
||||
],
|
||||
],
|
||||
],
|
||||
'id' => 'entity_test.entity_test.default',
|
||||
'targetEntityType' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'mode' => 'default',
|
||||
'content' => [
|
||||
'foo' => [
|
||||
'type' => 'field_no_settings',
|
||||
],
|
||||
],
|
||||
'hidden' => [
|
||||
'bar' => TRUE,
|
||||
],
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Save the display.
|
||||
// the 'content' property and the visible field has the default region set.
|
||||
$entity_display->save();
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'layout_discovery',
|
||||
];
|
||||
// A third party setting is added by the entity_test module.
|
||||
$expected['third_party_settings']['entity_test'] = ['foo' => 'bar'];
|
||||
// The visible field is assigned the default region.
|
||||
$expected['content']['foo']['region'] = 'content';
|
||||
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a new layout that has default settings and complex dependencies,
|
||||
// but do not save yet.
|
||||
$entity_display->setLayoutId('test_layout_main_and_footer');
|
||||
|
||||
// The default settings were added.
|
||||
$expected['third_party_settings']['field_layout'] = [
|
||||
'id' => 'test_layout_main_and_footer',
|
||||
'settings' => [
|
||||
'setting_1' => 'Default',
|
||||
],
|
||||
];
|
||||
// The field was moved to the default region.
|
||||
$expected['content']['foo'] = [
|
||||
'type' => 'field_no_settings',
|
||||
'region' => 'main',
|
||||
'weight' => -4,
|
||||
'settings' => [],
|
||||
'third_party_settings' => [],
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// After saving, the dependencies have been updated.
|
||||
$entity_display->save();
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'field_layout_test',
|
||||
'layout_discovery',
|
||||
'system',
|
||||
];
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a layout with provided settings.
|
||||
$entity_display->setLayoutId('test_layout_main_and_footer', ['setting_1' => 'foobar']);
|
||||
$entity_display->save();
|
||||
|
||||
// The setting overrides the default value.
|
||||
$expected['third_party_settings']['field_layout']['settings']['setting_1'] = 'foobar';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Move a field to the non-default region.
|
||||
$component = $entity_display->getComponent('foo');
|
||||
$component['region'] = 'footer';
|
||||
$entity_display->setComponent('foo', $component);
|
||||
$entity_display->save();
|
||||
|
||||
// The field region is saved.
|
||||
$expected['content']['foo']['region'] = 'footer';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
// Assign a different layout that shares the same non-default region.
|
||||
$entity_display->setLayoutId('test_layout_content_and_footer');
|
||||
$entity_display->save();
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
'field_layout',
|
||||
'field_layout_test',
|
||||
];
|
||||
// The layout has been updated.
|
||||
$expected['third_party_settings']['field_layout'] = [
|
||||
'id' => 'test_layout_content_and_footer',
|
||||
'settings' => [
|
||||
'label' => '',
|
||||
],
|
||||
];
|
||||
// The field remains in its current region instead of moving to the default.
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
|
||||
$this->container->get('module_installer')->uninstall(['field_layout']);
|
||||
|
||||
$entity_storage = $this->container->get('entity_type.manager')->getStorage('entity_view_display');
|
||||
$entity_display = $entity_storage->load('entity_test.entity_test.default');
|
||||
|
||||
// The dependencies have been updated.
|
||||
$expected['dependencies']['module'] = [
|
||||
'entity_test',
|
||||
];
|
||||
// All field_layout settings were removed.
|
||||
unset($expected['third_party_settings']['field_layout']);
|
||||
// The field has returned to the default content region.
|
||||
$expected['content']['foo']['region'] = 'content';
|
||||
$this->assertEntityValues($expected, $entity_display->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts than an entity has the correct values.
|
||||
*
|
||||
* @param array $expected
|
||||
* The expected values.
|
||||
* @param array $values
|
||||
* The actual values.
|
||||
* @param string $message
|
||||
* (optional) An error message.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function assertEntityValues(array $expected, array $values, string $message = ''): void {
|
||||
|
||||
static::assertArrayHasKey('uuid', $values);
|
||||
unset($values['uuid']);
|
||||
|
||||
static::assertEquals($expected, $values, $message);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\Kernel;
|
||||
|
||||
use Drupal\Tests\layout_builder\Kernel\LayoutBuilderCompatibilityTestBase;
|
||||
|
||||
/**
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutUninstallTest extends LayoutBuilderCompatibilityTestBase {
|
||||
|
||||
/**
|
||||
* Ensures field layout can be uninstalled with layout builder enabled.
|
||||
*/
|
||||
public function testFieldLayoutUninstall(): void {
|
||||
// Setup user schema so user hook uninstall hook doesn't break.
|
||||
$this->installSchema('user', 'users_data');
|
||||
|
||||
// Setup layout builder and same displays.
|
||||
$this->installLayoutBuilder();
|
||||
|
||||
// Ensure install hook can handle displays without a layout.
|
||||
$this->container->get('module_installer')->install(['field_layout']);
|
||||
|
||||
// Ensure uninstall hook can handle displays without a layout.
|
||||
$this->container->get('module_installer')->uninstall(['field_layout']);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,344 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\field_layout\Unit;
|
||||
|
||||
use Drupal\Core\Entity\EntityFieldManagerInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
|
||||
use Drupal\field_layout\FieldLayoutBuilder;
|
||||
use Drupal\Core\Layout\LayoutPluginManagerInterface;
|
||||
use Drupal\Core\Layout\LayoutDefault;
|
||||
use Drupal\Core\Layout\LayoutDefinition;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder
|
||||
* @group field_layout
|
||||
*/
|
||||
class FieldLayoutBuilderTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The layout plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Layout\LayoutPluginManager|\Prophecy\Prophecy\ProphecyInterface
|
||||
*/
|
||||
protected $layoutPluginManager;
|
||||
|
||||
/**
|
||||
* The entity field manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
|
||||
*/
|
||||
protected $entityFieldManager;
|
||||
|
||||
/**
|
||||
* The field layout builder.
|
||||
*
|
||||
* @var \Drupal\field_layout\FieldLayoutBuilder
|
||||
*/
|
||||
protected $fieldLayoutBuilder;
|
||||
|
||||
/**
|
||||
* The layout plugin.
|
||||
*
|
||||
* @var \Drupal\Core\Layout\LayoutInterface
|
||||
*/
|
||||
protected $layoutPlugin;
|
||||
|
||||
/**
|
||||
* The layout plugin definition.
|
||||
*
|
||||
* @var \Drupal\Core\Layout\LayoutDefinition
|
||||
*/
|
||||
protected $pluginDefinition;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->pluginDefinition = new LayoutDefinition([
|
||||
'library' => 'field_layout/drupal.layout.twocol',
|
||||
'theme_hook' => 'layout__twocol',
|
||||
'regions' => [
|
||||
'left' => [
|
||||
'label' => 'Left',
|
||||
],
|
||||
'right' => [
|
||||
'label' => 'Right',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$this->layoutPlugin = new LayoutDefault([], 'two_column', $this->pluginDefinition);
|
||||
|
||||
$this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
|
||||
$this->layoutPluginManager->getDefinition('unknown', FALSE)->willReturn(NULL);
|
||||
$this->layoutPluginManager->getDefinition('two_column', FALSE)->willReturn($this->pluginDefinition);
|
||||
|
||||
$this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
|
||||
|
||||
$this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal(), $this->entityFieldManager->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildView
|
||||
* @covers ::getFields
|
||||
*/
|
||||
public function testBuildView(): void {
|
||||
$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();
|
||||
$definitions['non_configurable_field_with_extra_field'] = $non_configurable_field_definition->reveal();
|
||||
$this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
|
||||
$extra_fields = [];
|
||||
$extra_fields['non_configurable_field_with_extra_field'] = [
|
||||
'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
|
||||
];
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn($extra_fields);
|
||||
|
||||
$build = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'#markup' => 'Non-configurable with extra field',
|
||||
],
|
||||
];
|
||||
|
||||
$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('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'test2' => [
|
||||
'region' => 'unknown_region',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'_field_layout' => [
|
||||
'left' => [
|
||||
'non_configurable_field_with_extra_field' => [
|
||||
'#markup' => 'Non-configurable with extra field',
|
||||
],
|
||||
],
|
||||
'right' => [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
],
|
||||
'#in_preview' => FALSE,
|
||||
'#settings' => [
|
||||
'label' => '',
|
||||
],
|
||||
'#layout' => $this->pluginDefinition,
|
||||
'#theme' => 'layout__twocol',
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'field_layout/drupal.layout.twocol',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildView($build, $display->reveal());
|
||||
$this->assertEquals($expected, $build);
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
* @covers ::getFields
|
||||
*/
|
||||
public function testBuildForm(): void {
|
||||
$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);
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
|
||||
|
||||
$build = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
'#group' => 'existing_group',
|
||||
],
|
||||
'test3' => [
|
||||
'#markup' => 'Test3',
|
||||
],
|
||||
'field_layout' => [
|
||||
'#markup' => 'Field created through the UI happens to be named "Layout"',
|
||||
],
|
||||
'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('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'test2' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
'test3' => [
|
||||
'region' => 'unknown_region',
|
||||
],
|
||||
'field_layout' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
'#group' => 'right',
|
||||
],
|
||||
'test2' => [
|
||||
'#markup' => 'Test2',
|
||||
'#group' => 'existing_group',
|
||||
],
|
||||
'test3' => [
|
||||
'#markup' => 'Test3',
|
||||
],
|
||||
'field_layout' => [
|
||||
'#markup' => 'Field created through the UI happens to be named "Layout"',
|
||||
'#group' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
'_field_layout' => [
|
||||
'left' => [
|
||||
'#process' => ['\Drupal\Core\Render\Element\RenderElementBase::processGroup'],
|
||||
'#pre_render' => ['\Drupal\Core\Render\Element\RenderElementBase::preRenderGroup'],
|
||||
],
|
||||
'right' => [
|
||||
'#process' => ['\Drupal\Core\Render\Element\RenderElementBase::processGroup'],
|
||||
'#pre_render' => ['\Drupal\Core\Render\Element\RenderElementBase::preRenderGroup'],
|
||||
],
|
||||
'#in_preview' => FALSE,
|
||||
'#settings' => [
|
||||
'label' => '',
|
||||
],
|
||||
'#layout' => $this->pluginDefinition,
|
||||
'#theme' => 'layout__twocol',
|
||||
'#attached' => [
|
||||
'library' => [
|
||||
'field_layout/drupal.layout.twocol',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertEquals($expected, $build);
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
*/
|
||||
public function testBuildFormEmpty(): void {
|
||||
$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);
|
||||
$this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
|
||||
|
||||
$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('two_column');
|
||||
$display->getLayoutSettings()->willReturn([]);
|
||||
$display->getComponents()->willReturn([
|
||||
'test1' => [
|
||||
'region' => 'right',
|
||||
],
|
||||
'non_configurable_field' => [
|
||||
'region' => 'left',
|
||||
],
|
||||
]);
|
||||
|
||||
$expected = [
|
||||
'non_configurable_field' => [
|
||||
'#markup' => 'Non-configurable',
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildForm
|
||||
*/
|
||||
public function testBuildFormNoLayout(): void {
|
||||
$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();
|
||||
|
||||
$expected = [
|
||||
'test1' => [
|
||||
'#markup' => 'Test1',
|
||||
],
|
||||
];
|
||||
$this->fieldLayoutBuilder->buildForm($build, $display->reveal());
|
||||
$this->assertSame($expected, $build);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user