function _ds_field_ui_table_layouts in Display Suite 8.4
Same name and namespace in other branches
- 8.2 includes/field_ui.inc \_ds_field_ui_table_layouts()
- 8.3 includes/field_ui.inc \_ds_field_ui_table_layouts()
- 7.2 includes/ds.field_ui.inc \_ds_field_ui_table_layouts()
- 7 ds.field_ui.inc \_ds_field_ui_table_layouts()
Add the layouts fieldset on the Field UI screen.
Parameters
string $entity_type: The name of the entity type.
string $bundle: The name of the bundle.
string $view_mode: The name of the view_mode.
array $form: A collection of form properties.
\Drupal\Core\Form\FormStateInterface $form_state: The form_state.
1 call to _ds_field_ui_table_layouts()
- ds_field_ui_fields_layouts in includes/
field_ui.inc - Adds the Display Suite fields and layouts to the form.
File
- includes/
field_ui.inc, line 745 - Field UI functions for Display Suite.
Code
function _ds_field_ui_table_layouts($entity_type, $bundle, $view_mode, array &$form, FormStateInterface $form_state) {
$ds_layouts = Ds::getLayouts();
$layout_options = [];
unset($ds_layouts['layout_builder_blank']);
/** @var \Drupal\Core\Layout\LayoutDefinition $layout_definition */
foreach ($ds_layouts as $key => $layout_definition) {
// Create new layout option group.
$optgroup = $layout_definition
->getCategory() ?: t('Other');
if ($optgroup instanceof TranslatableMarkup) {
$optgroup = (string) $optgroup;
}
if (!isset($layout_options[$optgroup])) {
$layout_options[$optgroup] = [];
}
// Stack the layout.
$layout_options[$optgroup][$key] = $layout_definition
->getLabel();
}
// If there is only one $optgroup, move it to the root.
if (count($layout_options) == 1) {
$layout_options = reset($layout_options);
}
// Add layouts form.
$form['ds_layouts'] = [
'#type' => 'details',
'#title' => t('Layout for @bundle in @view_mode', [
'@bundle' => str_replace('_', ' ', $bundle),
'@view_mode' => str_replace('_', ' ', $view_mode),
]),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#collapsed' => FALSE,
'#weight' => -100,
];
// @todo cleanup
$layout = [];
/* @var \Drupal\Core\Entity\EntityFormInterface $entity_form */
$entity_form = $form_state
->getFormObject();
/* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
$display = $entity_form
->getEntity();
if ($display
->getThirdPartySetting('ds', 'layout')) {
$layout_configuration = $display
->getThirdPartySetting('ds', 'layout');
if (isset($ds_layouts[$layout_configuration['id']])) {
$layout = (array) $ds_layouts[$layout_configuration['id']];
$layout['layout'] = $layout_configuration['id'];
$layout['disable_css'] = $layout_configuration['disable_css'];
$layout['entity_classes'] = $layout_configuration['entity_classes'];
$layout['settings'] = $layout_configuration['settings'] ?: [];
$layout['regions'] = $display
->getThirdPartySetting('ds', 'regions');
$layout['fields'] = $display
->getThirdPartySetting('ds', 'fields');
}
else {
\Drupal::messenger()
->addMessage(t('The configured layout (@layout) is missing, please select a new one', [
'@layout' => $layout_configuration['id'],
]), 'error');
}
}
if (!empty($layout) && isset($layout['layout']) && isset($ds_layouts[$layout['layout']])) {
$layout['region_names'] = $ds_layouts[$layout['layout']]
->getRegions();
$form['#ds_layout'] = $layout;
}
// Load the layout preview form.
$layout['layout_options'] = $layout_options;
_ds_field_ui_table_layouts_preview($form, $form_state, $ds_layouts, $layout, $display);
if (!empty($layout) && !empty($layout['settings'])) {
/* @var \Drupal\Core\Layout\LayoutInterface $layout_plugin */
$layout_plugin = \Drupal::service('plugin.manager.core.layout')
->createInstance($layout['layout'], $layout['settings'] ?: []);
if ($layout_plugin instanceof PluginFormInterface) {
$layout_configuration_form = $layout_plugin
->buildConfigurationForm([], $form_state);
// Merge 'details' elements in 'additional_settings' group into the main
// form.
foreach (Element::children($layout_configuration_form) as $name) {
$element = $layout_configuration_form[$name];
if ($element['#type'] == 'details' && $element['#group'] == 'additional_settings') {
$form[$name] = $layout_configuration_form[$name];
unset($layout_configuration_form[$name]);
$form[$name]['#ds_layout_configuration'] = TRUE;
$form[$name]['#parents'] = [
'layout_configuration',
$name,
];
$form[$name]['#tree'] = TRUE;
if ($layout['layout'] === 'ds_reset') {
$form[$name]['#access'] = FALSE;
}
}
}
// If anything is left, then we put it on it's own vertical tab.
if (!Element::isEmpty($layout_configuration_form)) {
$form['layout_configuration'] = array_merge($layout_configuration_form, [
'#group' => 'additional_settings',
'#type' => 'details',
'#title' => t('Layout settings'),
'#tree' => TRUE,
]);
}
}
}
else {
// See if we can clone from another view mode.
$options = [];
$entity_displays = \Drupal::configFactory()
->listAll('core.entity_view_display.' . $entity_type . '.' . $bundle);
if (!empty($entity_displays)) {
$entity_type_info = \Drupal::entityTypeManager()
->getDefinition($entity_type);
foreach ($entity_displays as $name) {
$row = \Drupal::config($name)
->get();
if ($row['mode'] === $view_mode) {
continue;
}
if ($row['mode'] != 'default') {
$view_mode_label = \Drupal::entityTypeManager()
->getStorage('entity_view_mode')
->load($entity_type . '.' . $row['mode'])
->label();
}
else {
$view_mode_label = 'Default';
}
if ($row['targetEntityType'] === $entity_type && $row['bundle'] === $bundle) {
if ($entity_type_info
->getBundleEntityType()) {
$bundle_info = \Drupal::entityTypeManager()
->getStorage($entity_type_info
->getBundleEntityType())
->load($bundle);
$options[$row['id']] = $entity_type_info
->getLabel() . ' > ' . $bundle_info
->label() . ' > ' . $view_mode_label;
}
else {
$options[$row['id']] = $entity_type_info
->getLabel() . ' > ' . $view_mode_label;
}
}
}
if (!empty($options)) {
natcasesort($options);
// Clone from another layout.
$form['ds_clone'] = [
'#type' => 'details',
'#group' => 'additional_settings',
'#title' => t('Clone layout'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['ds_clone']['clone'] = [
'#title' => t('Select an existing layout to clone.'),
'#type' => 'select',
'#options' => $options,
'#weight' => 20,
];
$form['ds_clone']['clone_submit'] = [
'#type' => 'submit',
'#value' => t('Clone layout'),
'#submit' => [
'ds_field_ui_layout_clone',
],
'#weight' => 21,
];
}
}
}
$form['ds_layouts']['old_layout'] = [
'#type' => 'value',
'#value' => isset($layout['layout']) ? $layout['layout'] : 0,
];
// Add validate and submit handlers. Layout needs be first so
// we can reset the type key for Field API fields.
$form['#validate'][] = 'ds_field_ui_layouts_validate';
array_unshift($form['actions']['submit']['#submit'], 'ds_field_ui_fields_save');
array_unshift($form['actions']['submit']['#submit'], 'ds_field_ui_layouts_save');
}