Update core 8.3.0

This commit is contained in:
Rob Davies 2017-04-13 15:53:35 +01:00
parent da7a7918f8
commit cd7a898e66
6144 changed files with 132297 additions and 87747 deletions

View file

@ -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');
}

View file

@ -0,0 +1,153 @@
<?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;
}
/**
* 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 $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());
}
}
}

View file

@ -0,0 +1,24 @@
<?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;
/**
* {@inheritdoc}
*/
public function getDefaultRegion() {
// This cannot be provided by the trait due to
// https://bugs.php.net/bug.php?id=71414 which is fixed in PHP 7.0.6.
return $this->getLayoutDefinition($this->getLayoutId())->getDefaultRegion();
}
}

View file

@ -0,0 +1,24 @@
<?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;
/**
* {@inheritdoc}
*/
public function getDefaultRegion() {
// This cannot be provided by the trait due to
// https://bugs.php.net/bug.php?id=71414 which is fixed in PHP 7.0.6.
return $this->getLayoutDefinition($this->getLayoutId())->getDefaultRegion();
}
}

View file

@ -0,0 +1,153 @@
<?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) {
// Move the field from the top-level of $build into a region-specific
// section.
// @todo Ideally the array structure would remain unchanged, see
// https://www.drupal.org/node/2846393.
$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\RenderElement::processGroup';
$fill['#pre_render'][] = '\Drupal\Core\Render\Element\RenderElement::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($build[$name]['#group'])) {
$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;
}
}

View file

@ -0,0 +1,177 @@
<?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,
];
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();
}

View file

@ -0,0 +1,44 @@
<?php
namespace Drupal\field_layout\Form;
use Drupal\Component\Plugin\PluginManagerBase;
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.
*/
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.
*/
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, LayoutPluginManagerInterface $layout_plugin_manager) {
parent::__construct($field_type_manager, $plugin_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')
);
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Drupal\field_layout\Form;
use Drupal\Component\Plugin\PluginManagerBase;
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.
*/
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.
*/
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, LayoutPluginManagerInterface $layout_plugin_manager) {
parent::__construct($field_type_manager, $plugin_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')
);
}
}