public function ChangeLayoutForm::submitForm in Display Suite 8.3
Same name and namespace in other branches
- 8.4 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm::submitForm()
- 8.2 src/Form/ChangeLayoutForm.php \Drupal\ds\Form\ChangeLayoutForm::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ ChangeLayoutForm.php, line 200
Class
- ChangeLayoutForm
- Provides a configuration form for configurable actions.
Namespace
Drupal\ds\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
// Prepare some variables.
$old_layout = $form['#old_layout'];
$new_layout = $form['#new_layout'];
$old_layout_info = $form['#old_layout_info'];
$new_layout_key = $form['#new_layout_key'];
$entity_type = $form['#entity_type'];
$bundle = $form['#entity_bundle'];
$display_mode = $form['#mode'];
// Create new third party settings.
$third_party_settings = $old_layout;
$third_party_settings['layout']['id'] = $new_layout_key;
$third_party_settings['layout']['library'] = NULL;
if ($library = $new_layout
->getLibrary()) {
$third_party_settings['layout']['library'] = $library;
}
unset($third_party_settings['regions']);
// Default wrappers.
$third_party_settings['layout']['settings']['wrappers'] = [];
foreach ($new_layout
->getRegions() as $region_name => $content) {
$third_party_settings['layout']['settings']['wrappers'][$region_name] = 'div';
}
// Map old regions to new ones.
$fields = [];
foreach ($old_layout_info
->getRegions() as $region => $region_title) {
$new_region = $form_state
->getValue('ds_' . $region);
if ($new_region != '' && isset($old_layout['regions'][$region])) {
foreach ($old_layout['regions'][$region] as $field) {
if (!isset($third_party_settings['regions'][$new_region])) {
$third_party_settings['regions'][$new_region] = [];
}
$third_party_settings['regions'][$new_region][] = $field;
$fields[$field] = $new_region;
}
}
}
// Save configuration.
/* @var $entity_display \Drupal\Core\Entity\Display\EntityDisplayInterface*/
$entity_display = $this->entityTypeManager
->getStorage('entity_view_display')
->load($entity_type . '.' . $bundle . '.' . $display_mode);
foreach (array_keys($third_party_settings) as $key) {
$entity_display
->setThirdPartySetting('ds', $key, $third_party_settings[$key]);
}
// Map regions on fields.
foreach ($entity_display
->getComponents() as $name => $options) {
if (isset($options['region']) && isset($fields[$name])) {
$options['region'] = $fields[$name];
$entity_display
->setComponent($name, $options);
}
}
// Map field groups, if available.
$groups = $entity_display
->getThirdPartySettings('field_group');
if (!empty($groups)) {
foreach (array_keys($groups) as $group_name) {
$region = 'hidden';
if (isset($fields[$group_name])) {
$region = $fields[$group_name];
}
$groups[$group_name]['region'] = $region;
$entity_display
->setThirdPartySetting('field_group', $group_name, $groups[$group_name]);
}
}
// Now save.
$entity_display
->save();
// Clear entity info cache.
$this->entityFieldManager
->clearCachedFieldDefinitions();
// Show message.
$this
->messenger()
->addMessage($this
->t('The layout change has been saved.'));
}