You are here

function _ds_field_ui_table_layouts in Display Suite 8.2

Same name and namespace in other branches
  1. 8.4 includes/field_ui.inc \_ds_field_ui_table_layouts()
  2. 8.3 includes/field_ui.inc \_ds_field_ui_table_layouts()
  3. 7.2 includes/ds.field_ui.inc \_ds_field_ui_table_layouts()
  4. 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.

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 672
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 = array(
    '' => t('- None -'),
  );
  $optgroup = '';
  foreach ($ds_layouts as $key => $layout_definition) {
    $optgroup = t('Other');

    // Create new layout option group.
    if (!empty($layout_definition['category'])) {
      $optgroup = (string) $layout_definition['category'];
    }
    if (!isset($layout_options[$optgroup])) {
      $layout_options[$optgroup] = array();
    }

    // Stack the layout.
    $layout_options[$optgroup][$key] = $layout_definition['label'];
  }

  // If there is only one $optgroup, move it to the root.
  if (count($layout_options) == 2) {
    $options = $layout_options[$optgroup];
    $layout_options = array_merge(array(
      '' => t('- None -'),
    ), $options);
  }

  // Add layouts form.
  $form['ds_layouts'] = array(
    '#type' => 'details',
    '#title' => t('Layout for @bundle in @view_mode', array(
      '@bundle' => str_replace('_', ' ', $bundle),
      '@view_mode' => str_replace('_', ' ', $view_mode),
    )),
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
    '#collapsed' => FALSE,
    '#weight' => -100,
  );

  // @todo cleanup
  $layout = array();

  /* @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');
    $layout = $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');
  }
  if (!empty($layout) && isset($layout['layout']) && isset($ds_layouts[$layout['layout']])) {
    $layout['region_names'] = $ds_layouts[$layout['layout']]['regions'];
    $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\layout_plugin\Plugin\Layout\LayoutInterface $layout_plugin */
    $layout_plugin = Layout::layoutPluginManager()
      ->createInstance($layout['layout'], $layout['settings'] ?: []);
    $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 vertial 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 = array();
    $entity_displays = \Drupal::configFactory()
      ->listAll('core.entity_view_display.' . $entity_type . '.' . $bundle);
    foreach ($entity_displays as $name) {
      $row = \Drupal::config($name)
        ->get();
      if ($row['mode'] == $view_mode) {
        continue;
      }
      if ($row['targetEntityType'] == $entity_type && $row['bundle'] == $bundle) {
        $options[$row['id']] = Unicode::ucfirst(str_replace('_', ' ', $row['targetEntityType'])) . ' > ' . Unicode::ucfirst(str_replace('_', ' ', $row['bundle'])) . ' > ' . Unicode::ucfirst(str_replace('_', ' ', $row['mode']));
      }
    }
    if (!empty($options)) {

      // Clone from another layout.
      $form['ds_clone'] = array(
        '#type' => 'details',
        '#group' => 'additional_settings',
        '#title' => t('Clone layout'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['ds_clone']['clone'] = array(
        '#title' => t('Select an existing layout to clone.'),
        '#type' => 'select',
        '#options' => $options,
        '#weight' => 20,
      );
      $form['ds_clone']['clone_submit'] = array(
        '#type' => 'submit',
        '#value' => t('Clone layout'),
        '#submit' => array(
          'ds_field_ui_layout_clone',
        ),
        '#weight' => 21,
      );
    }
  }
  $form['ds_layouts']['old_layout'] = array(
    '#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');
}