function ds_field_ui_layouts_save in Display Suite 8.3
Same name and namespace in other branches
- 8.4 includes/field_ui.inc \ds_field_ui_layouts_save()
- 8.2 includes/field_ui.inc \ds_field_ui_layouts_save()
- 7.2 includes/ds.field_ui.inc \ds_field_ui_layouts_save()
- 7 ds.field_ui.inc \ds_field_ui_layouts_save()
Save the layout settings from the 'Manage display' screen.
1 string reference to 'ds_field_ui_layouts_save'
- _ds_field_ui_table_layouts in includes/
field_ui.inc - Add the layouts fieldset on the Field UI screen.
File
- includes/
field_ui.inc, line 321 - Field UI functions for Display Suite.
Code
function ds_field_ui_layouts_save($form, FormStateInterface $form_state) {
$save_display = FALSE;
// Get default values.
$entity_type = $form['#entity_type'];
/* @var \Drupal\Core\Entity\EntityFormInterface $entity_form */
$entity_form = $form_state
->getFormObject();
// Get the entity display.
/* @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
$display = $entity_form
->getEntity();
if ($display instanceof LayoutBuilderEnabledInterface && $display
->isLayoutBuilderEnabled()) {
$display
->unsetThirdPartySetting('ds', 'layout');
$display
->unsetThirdPartySetting('ds', 'regions');
$display
->unsetThirdPartySetting('ds', 'fields');
$display
->save();
$form_state
->set('ignore_ds_fields', FALSE);
return;
}
// Get view mode.
$view_mode = $display
->getMode();
// Determine layout variables.
$layout = $form_state
->getValue('ds_layout');
$disable_css = $form_state
->getValue('disable_css');
$entity_classes = $form_state
->getValue('entity_classes');
if (empty($entity_classes)) {
$entity_classes = 'all_classes';
}
$old_layout = $form_state
->getValue('old_layout');
$new_layout = $layout != $old_layout || empty($old_layout);
$key = array_search('ds_field_ui_change_layout_submit', $form['actions']['submit']['#submit']);
if ($key && !empty($old_layout)) {
return;
}
$ds_layouts = Ds::getLayouts();
// Save layout and add regions if necessary.
$record = [];
$record['layout'] = [
'id' => $layout,
'library' => !empty($layout) ? $ds_layouts[$layout]
->getLibrary() : FALSE,
'disable_css' => $disable_css ? TRUE : FALSE,
'entity_classes' => $entity_classes,
];
$record['regions'] = [];
// Remove old layout if necessary.
if ($new_layout && !empty($old_layout) || empty($layout)) {
$display
->unsetThirdPartySetting('ds', 'layout');
$display
->unsetThirdPartySetting('ds', 'regions');
// If layout is empty, reset all fields to content, if the region is not
// set to hidden at the moment. This takes field groups into account as well
// automatically.
if (empty($layout)) {
$form_state
->set('ignore_ds_fields', FALSE);
$display
->unsetThirdPartySetting('ds', 'fields');
// Check for Field Group form params. If it's available, reset the
// available and default regions.
$field_group_params = $form_state
->get('field_group_params');
if (isset($field_group_params) && !empty($field_group_params->available_regions) && !empty($field_group_params->default_region)) {
$field_group_params->available_regions = [
'content',
'hidden',
];
$field_group_params->default_region = 'hidden';
$form_state
->set('field_group_params', $field_group_params);
}
$fields = $form_state
->getValue('fields');
if (!empty($fields)) {
foreach (array_keys($fields) as $name) {
if (isset($fields[$name]['region'])) {
$reset_region = 'content';
if ($fields[$name]['region'] == 'hidden') {
$reset_region = 'hidden';
}
$fields[$name]['region'] = $reset_region;
}
}
}
$form_state
->setValue('fields', $fields);
}
$display
->save();
}
if ($new_layout && !empty($layout)) {
$save_display = TRUE;
// Move current visible fields into a default region, so
// we keep their current settings.
$layouts = Ds::getLayouts();
/** @var \Drupal\Core\Layout\LayoutDefinition $sl */
$sl = $layouts[$layout];
// TODO use default region method?
$first_region = key($sl
->getRegions());
$record['layout']['settings']['classes'] = [];
$record['layout']['settings']['wrappers'] = [];
// Set default region values.
foreach ($sl
->getRegions() as $region_name => $content) {
$record['layout']['settings']['wrappers'][$region_name] = 'div';
}
$record['layout']['settings']['outer_wrapper'] = 'div';
$record['layout']['settings']['attributes'] = '';
$record['layout']['settings']['link_attribute'] = FALSE;
$record['layout']['settings']['link_custom'] = '';
$fields = _ds_sort_fields((array) $form_state
->getValue('fields'), 'weight');
foreach ($fields as $field_key => $field) {
// Ignore new fieldgroup, new field or existing field.
if (in_array($field_key, [
'_add_new_field',
'_add_existing_field',
'_add_new_group',
])) {
continue;
}
// Can either be form or display.
if (isset($field['type']) && $field['type'] != 'hidden') {
$record['regions'][$first_region][] = $field_key;
}
}
// In case this is the full node view mode and if the comment module
// is enabled for this content type, add it as well.
if ($entity_type == 'node' && $view_mode == 'full' && \Drupal::moduleHandler()
->moduleExists('comment')) {
$record['regions'][$first_region][] = 'comments';
}
}
elseif (!empty($layout)) {
$save_display = TRUE;
$fields = _ds_sort_fields((array) $form_state
->getValue('fields'), 'weight');
foreach ($fields as $key => $field) {
// Make sure to hide hidden fields.
if ($field['region'] == 'hidden') {
$form_state
->setValue([
'fields',
$key,
'type',
], 'hidden');
continue;
}
if (!isset($record['regions'][$field['region']])) {
$record['regions'][$field['region']] = [];
}
$record['regions'][$field['region']][] = $key;
}
/* @var \Drupal\layout_plugin\Plugin\Layout\LayoutInterface $layout_plugin */
$layout_plugin = \Drupal::service('plugin.manager.core.layout')
->createInstance($layout, []);
$layout_form = isset($form['layout_configuration']) ? $form['layout_configuration'] : [];
foreach (Element::children($form) as $name) {
if (!empty($form[$name]['#ds_layout_configuration'])) {
$layout_form[$name] = $form[$name];
}
}
if ($layout_plugin instanceof PluginFormInterface) {
$layout_form_state = (new FormState())
->setValues($form_state
->getValue('layout_configuration', []));
$layout_plugin
->submitConfigurationForm($layout_form, $layout_form_state);
}
// Get the layout settings from the layout_plugin.
$record['layout']['settings'] = $layout_plugin
->getConfiguration();
// Let other modules alter the layout settings.
\Drupal::moduleHandler()
->alter('ds_layout_settings', $record, $form_state);
}
// Save the configuration.
if ($save_display) {
// Let other modules alter the layout settings.
\Drupal::moduleHandler()
->alter('ds_layout_settings', $record, $form_state);
foreach (array_keys($record) as $key) {
$display
->setThirdPartySetting('ds', $key, $record[$key]);
}
$display
->save();
}
}