View source
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field_ui\Form\EntityViewDisplayEditForm;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionStorageInterface;
class LayoutBuilderEntityViewDisplayForm extends EntityViewDisplayEditForm {
protected $entity;
protected $sectionStorage;
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$this->sectionStorage = $section_storage;
return parent::buildForm($form, $form_state);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['#fields'] = array_diff($form['#fields'], [
OverridesSectionStorage::FIELD_NAME,
]);
unset($form['fields'][OverridesSectionStorage::FIELD_NAME]);
$is_enabled = $this->entity
->isLayoutBuilderEnabled();
if ($is_enabled) {
$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';
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';
}
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;
}
protected function isCanonicalMode($mode) {
$canonical_mode = 'full';
if ($mode === $canonical_mode) {
return TRUE;
}
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;
}
protected function hasOverrides(LayoutEntityDisplayInterface $display) {
if (!$display
->isOverridable()) {
return FALSE;
}
$entity_type = $this->entityTypeManager
->getDefinition($display
->getTargetEntityTypeId());
$query = $this->entityTypeManager
->getStorage($display
->getTargetEntityTypeId())
->getQuery()
->accessCheck(FALSE)
->exists(OverridesSectionStorage::FIELD_NAME);
if ($bundle_key = $entity_type
->getKey('bundle')) {
$query
->condition($bundle_key, $display
->getTargetBundle());
}
return (bool) $query
->count()
->execute();
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$set_enabled = (bool) $form_state
->getValue([
'layout',
'enabled',
], FALSE);
$already_enabled = $entity
->isLayoutBuilderEnabled();
if ($already_enabled || $set_enabled) {
$form['#fields'] = [];
$form['#extra'] = [];
}
parent::copyFormValuesToEntity($entity, $form, $form_state);
}
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'));
}
}
protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
if ($this->entity
->isLayoutBuilderEnabled()) {
return [];
}
return parent::buildFieldRow($field_definition, $form, $form_state);
}
protected function buildExtraFieldRow($field_id, $extra_field) {
if ($this->entity
->isLayoutBuilderEnabled()) {
return [];
}
return parent::buildExtraFieldRow($field_id, $extra_field);
}
}