class LayoutBuilderEntityViewDisplayForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm
Edit form for the LayoutBuilderEntityViewDisplay entity type.
@internal Form classes are internal.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\field_ui\Form\EntityDisplayFormBase
- class \Drupal\field_ui\Form\EntityViewDisplayEditForm
- class \Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm
- class \Drupal\field_ui\Form\EntityViewDisplayEditForm
- class \Drupal\field_ui\Form\EntityDisplayFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of LayoutBuilderEntityViewDisplayForm
1 file declares its use of LayoutBuilderEntityViewDisplayForm
- layout_builder.module in core/
modules/ layout_builder/ layout_builder.module - Provides hook implementations for Layout Builder.
File
- core/
modules/ layout_builder/ src/ Form/ LayoutBuilderEntityViewDisplayForm.php, line 19
Namespace
Drupal\layout_builder\FormView source
class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
/**
* The entity being used by this form.
*
* @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface
*/
protected $entity;
/**
* The storage section.
*
* @var \Drupal\layout_builder\DefaultsSectionStorageInterface
*/
protected $sectionStorage;
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$this->sectionStorage = $section_storage;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Remove the Layout Builder field from the list.
$form['#fields'] = array_diff($form['#fields'], [
OverridesSectionStorage::FIELD_NAME,
]);
unset($form['fields'][OverridesSectionStorage::FIELD_NAME]);
$is_enabled = $this->entity
->isLayoutBuilderEnabled();
if ($is_enabled) {
// Hide the table of fields.
$form['fields']['#access'] = FALSE;
$form['#fields'] = [];
$form['#extra'] = [];
}
$form['manage_layout'] = [
'#type' => 'link',
'#title' => $this
->t('Manage layout'),
'#weight' => -10,
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this->sectionStorage
->getLayoutBuilderUrl(),
'#access' => $is_enabled,
];
$form['layout'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Layout options'),
'#tree' => TRUE,
];
$form['layout']['enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use Layout Builder'),
'#default_value' => $is_enabled,
];
$form['#entity_builders']['layout_builder'] = '::entityFormEntityBuild';
// @todo Expand to work for all view modes in
// https://www.drupal.org/node/2907413.
if ($this
->isCanonicalMode($this->entity
->getMode())) {
$entity_type = $this->entityTypeManager
->getDefinition($this->entity
->getTargetEntityTypeId());
$form['layout']['allow_custom'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow each @entity to have its layout customized.', [
'@entity' => $entity_type
->getSingularLabel(),
]),
'#default_value' => $this->entity
->isOverridable(),
'#states' => [
'disabled' => [
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
'invisible' => [
':input[name="layout[enabled]"]' => [
'checked' => FALSE,
],
],
],
];
if (!$is_enabled) {
$form['layout']['allow_custom']['#attributes']['disabled'] = 'disabled';
}
// Prevent turning off overrides while any exist.
if ($this
->hasOverrides($this->entity)) {
$form['layout']['enabled']['#disabled'] = TRUE;
$form['layout']['enabled']['#description'] = $this
->t('You must revert all customized layouts of this display before you can disable this option.');
$form['layout']['allow_custom']['#disabled'] = TRUE;
$form['layout']['allow_custom']['#description'] = $this
->t('You must revert all customized layouts of this display before you can disable this option.');
unset($form['layout']['allow_custom']['#states']);
unset($form['#entity_builders']['layout_builder']);
}
}
else {
$form['layout']['allow_custom'] = [
'#type' => 'value',
'#value' => $this->entity
->isOverridable(),
];
}
return $form;
}
/**
* Determines if the mode is used by the canonical route.
*
* @param string $mode
* The view mode.
*
* @return bool
* TRUE if the mode is valid, FALSE otherwise.
*/
protected function isCanonicalMode($mode) {
// @todo This is a convention core uses but is not a given, nor is it easily
// introspectable. Address in https://www.drupal.org/node/2907413.
$canonical_mode = 'full';
if ($mode === $canonical_mode) {
return TRUE;
}
// The default mode is valid if the canonical mode is not enabled.
if ($mode === 'default') {
$query = $this->entityTypeManager
->getStorage($this->entity
->getEntityTypeId())
->getQuery()
->condition('targetEntityType', $this->entity
->getTargetEntityTypeId())
->condition('bundle', $this->entity
->getTargetBundle())
->condition('status', TRUE)
->condition('mode', $canonical_mode);
return !$query
->count()
->execute();
}
return FALSE;
}
/**
* Determines if the defaults have any overrides.
*
* @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
* The entity display.
*
* @return bool
* TRUE if there are any overrides of this default, FALSE otherwise.
*/
protected function hasOverrides(LayoutEntityDisplayInterface $display) {
if (!$display
->isOverridable()) {
return FALSE;
}
$entity_type = $this->entityTypeManager
->getDefinition($display
->getTargetEntityTypeId());
$query = $this->entityTypeManager
->getStorage($display
->getTargetEntityTypeId())
->getQuery()
->exists(OverridesSectionStorage::FIELD_NAME);
if ($bundle_key = $entity_type
->getKey('bundle')) {
$query
->condition($bundle_key, $display
->getTargetBundle());
}
return (bool) $query
->count()
->execute();
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// Do not process field values if Layout Builder is or will be enabled.
$set_enabled = (bool) $form_state
->getValue([
'layout',
'enabled',
], FALSE);
/** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $entity */
$already_enabled = $entity
->isLayoutBuilderEnabled();
if ($already_enabled || $set_enabled) {
$form['#fields'] = [];
$form['#extra'] = [];
}
parent::copyFormValuesToEntity($entity, $form, $form_state);
}
/**
* Entity builder for layout options on the entity view display form.
*/
public function entityFormEntityBuild($entity_type_id, LayoutEntityDisplayInterface $display, &$form, FormStateInterface &$form_state) {
$set_enabled = (bool) $form_state
->getValue([
'layout',
'enabled',
], FALSE);
$already_enabled = $display
->isLayoutBuilderEnabled();
if ($set_enabled) {
$overridable = (bool) $form_state
->getValue([
'layout',
'allow_custom',
], FALSE);
$display
->setOverridable($overridable);
if (!$already_enabled) {
$display
->enableLayoutBuilder();
}
}
elseif ($already_enabled) {
$form_state
->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl('disable'));
}
}
/**
* {@inheritdoc}
*/
protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
if ($this->entity
->isLayoutBuilderEnabled()) {
return [];
}
return parent::buildFieldRow($field_definition, $form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function buildExtraFieldRow($field_id, $extra_field) {
if ($this->entity
->isLayoutBuilderEnabled()) {
return [];
}
return parent::buildExtraFieldRow($field_id, $extra_field);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityDisplayFormBase:: |
protected | property | The entity display repository. | |
EntityDisplayFormBase:: |
protected | property | The entity field manager. | |
EntityDisplayFormBase:: |
protected | property | A list of field types. | |
EntityDisplayFormBase:: |
protected | property | The widget or formatter plugin manager. | |
EntityDisplayFormBase:: |
protected | function | Returns an array of applicable widget or formatter options for a field. | |
EntityDisplayFormBase:: |
protected | function | Returns entity (form) displays for the current entity display type. | |
EntityDisplayFormBase:: |
protected | function | Returns form or view modes statuses for the bundle used by this form. | |
EntityDisplayFormBase:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
protected | function | Returns the extra fields of the entity type and bundle used by this form. | |
EntityDisplayFormBase:: |
protected | function | Collects the definitions of fields whose display is configurable. | |
EntityDisplayFormBase:: |
public | function | Returns an associative array of all regions. | |
EntityDisplayFormBase:: |
public | function | Get the regions needed to create the overview form. | |
EntityDisplayFormBase:: |
public | function | Returns the region to which a row in the display overview belongs. | |
EntityDisplayFormBase:: |
public | function | Ajax handler for multistep buttons. | |
EntityDisplayFormBase:: |
public | function | Form submission handler for multistep buttons. | |
EntityDisplayFormBase:: |
public | function | Determines the rendering order of an array representing a tree. | |
EntityDisplayFormBase:: |
protected | function | Saves the updated display mode statuses. | |
EntityDisplayFormBase:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
public | function | Performs pre-render tasks on field_ui_table elements. | |
EntityDisplayFormBase:: |
public | function | Constructs a new EntityDisplayFormBase. | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
EntityViewDisplayEditForm:: |
protected | property |
The display context. Either 'view' or 'form'. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Alters the widget or formatter settings summary. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns the ID of the default widget or formatter plugin for a field type. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns an array of form or view mode options. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns the form or view modes used by this form. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns a link to the form or view mode admin page. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns an entity display object to be used by this form. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function | Returns an array of visibility options for field labels. | |
EntityViewDisplayEditForm:: |
protected | function |
Returns the Url object for a specific entity (form) display edit form. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Returns an array containing the table headers. Overrides EntityDisplayFormBase:: |
|
EntityViewDisplayEditForm:: |
protected | function |
Adds the widget or formatter third party settings forms. Overrides EntityDisplayFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LayoutBuilderEntityViewDisplayForm:: |
protected | property |
The entity being used by this form. Overrides EntityDisplayFormBase:: |
|
LayoutBuilderEntityViewDisplayForm:: |
protected | property | The storage section. | |
LayoutBuilderEntityViewDisplayForm:: |
protected | function |
Builds the table row structure for a single extra field. Overrides EntityViewDisplayEditForm:: |
|
LayoutBuilderEntityViewDisplayForm:: |
protected | function |
Builds the table row structure for a single field. Overrides EntityViewDisplayEditForm:: |
|
LayoutBuilderEntityViewDisplayForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
LayoutBuilderEntityViewDisplayForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityDisplayFormBase:: |
|
LayoutBuilderEntityViewDisplayForm:: |
public | function | Entity builder for layout options on the entity view display form. | |
LayoutBuilderEntityViewDisplayForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityDisplayFormBase:: |
|
LayoutBuilderEntityViewDisplayForm:: |
protected | function | Determines if the defaults have any overrides. | |
LayoutBuilderEntityViewDisplayForm:: |
protected | function | Determines if the mode is used by the canonical route. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |