function ds_field_ui_layouts_save in Display Suite 8.2
Same name and namespace in other branches
- 8.4 includes/field_ui.inc \ds_field_ui_layouts_save()
- 8.3 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 292 - 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();
// Get view mode.
$view_mode = $display
->getMode();
// Determine layout variables.
$layout = $form_state
->getValue('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 = array();
$record['layout'] = array(
'id' => $layout,
'path' => !empty($layout) ? $ds_layouts[$layout]['path'] : '',
'library' => isset($ds_layouts[$layout]['library']) ? $ds_layouts[$layout]['library'] : FALSE,
'disable_css' => $disable_css ? TRUE : FALSE,
'entity_classes' => $entity_classes,
);
$record['regions'] = array();
// Remove old layout if necessary.
if ($new_layout && !empty($old_layout) || empty($layout)) {
$display
->unsetThirdPartySetting('ds', 'layout');
$display
->unsetThirdPartySetting('ds', 'regions');
$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();
$sl = $layouts[$layout];
$first_region = key($sl['regions']);
$record['layout']['settings']['classes'] = array();
$record['layout']['settings']['wrappers'] = array();
// Set default region values.
foreach ($sl['regions'] 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($form_state
->getValue('fields'), 'weight');
foreach ($fields as $field_key => $field) {
// Ignore new fieldgroup, new field or existing field.
if (in_array($field_key, array(
'_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($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']] = array();
}
$record['regions'][$field['region']][] = $key;
}
/* @var \Drupal\layout_plugin\Plugin\Layout\LayoutInterface $layout_plugin */
$layout_plugin = Layout::layoutPluginManager()
->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];
}
}
$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();
}
}