View source
<?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;
trait FieldLayoutEntityDisplayFormTrait {
protected $layoutPluginManager;
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;
}
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;
}
protected function getLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) {
if (!($layout_plugin = $form_state
->get('layout_plugin'))) {
$stored_layout_id = $entity
->getLayoutId();
$layout_id = $form_state
->getValue('field_layout', $stored_layout_id);
$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;
}
public static function settingsAjax($form, FormStateInterface $form_state) {
return $form['field_layouts']['settings_wrapper'];
}
public function settingsAjaxSubmit($form, FormStateInterface $form_state) {
$form_state
->set('layout_plugin', NULL);
$form_state
->setRebuild();
}
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);
}
}
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);
}
public abstract function getEntity();
}