You are here

function ds_field_ui_regions in Display Suite 8.4

Same name and namespace in other branches
  1. 8.2 includes/field_ui.inc \ds_field_ui_regions()
  2. 8.3 includes/field_ui.inc \ds_field_ui_regions()
  3. 7.2 includes/ds.field_ui.inc \ds_field_ui_regions()
  4. 7 ds.field_ui.inc \ds_field_ui_regions()

Add Regions to 'Manage fields' or 'Manage display' screen.

Parameters

array $form: The form to add layout fieldset and extra Display Suite fields.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array The altered form

1 string reference to 'ds_field_ui_regions'
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 134
Field UI functions for Display Suite.

Code

function ds_field_ui_regions(array $form, FormStateInterface $form_state) {

  // Get the entity_type, bundle and view mode.
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];

  /* @var \Drupal\Core\Entity\EntityFormInterface $entity_form */
  $entity_form = $form_state
    ->getFormObject();

  /* @var \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display */
  $entity_display = $entity_form
    ->getEntity();
  $view_mode = $entity_display
    ->getMode();

  // Ignore field_group options.
  if ($form_state
    ->has('no_field_group')) {
    unset($form['fields']['_add_new_group']);
    $form['field_group']['#access'] = FALSE;
  }

  // Check layout.
  $layout = isset($form['#ds_layout']) ? $form['#ds_layout'] : FALSE;

  // Build an array which keys are the field names and
  // values are the region they are rendered in.
  $field_regions = [];

  // Change UI to add Region column if we have a layout.
  if ($layout) {
    foreach ($layout['regions'] as $region_name => $field_names) {
      foreach ($field_names as $field_name) {
        $field_regions[$field_name] = $region_name;
      }
    }
    $table =& $form['fields'];
    $table['#header'] = [
      t('Field'),
      t('Weight'),
      t('Parent'),
      t('Region'),
      t('Label'),
      t('Formatter'),
      t('Widget'),
      [
        'data' => t('Operations'),
        'colspan' => 2,
      ],
    ];
    $table['#regions'] = [];
    $region_options = [];
    foreach ($layout['region_names'] as $region_key => $region_info) {
      $region_options[$region_key] = $region_info['label'];
      $table['#regions'][$region_key] = [
        'title' => $region_info['label'],
        'message' => t('No fields are displayed in this region'),
      ];
    }

    // Let other modules alter the regions.
    $context = [
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'view_mode' => $view_mode,
    ];
    $region_info = [
      'region_options' => &$region_options,
      'table_regions' => &$table['#regions'],
    ];
    \Drupal::moduleHandler()
      ->alter('ds_layout_region', $context, $region_info);
    $region_options['hidden'] = t('Disabled');
    $table['#regions']['hidden'] = [
      'title' => t('Disabled'),
      'message' => t('No fields are hidden.'),
    ];
    $region = [
      '#type' => 'select',
      '#options' => $region_options,
      '#default_value' => 'hidden',
      '#attributes' => [
        'class' => [
          'field-region',
        ],
      ],
    ];

    // Update existing rows by changing rowHandler and adding regions.
    foreach (Element::children($table) as $name) {
      $row =& $table[$name];
      $row['#js_settings'] = [
        'rowHandler' => 'field',
      ];
      $row['#region_callback'] = 'ds_field_ui_row_region';

      // Remove hidden format.
      if (isset($row['plugin']['type']['#options']['hidden'])) {
        unset($row['plugin']['type']['#options']['hidden']);
      }

      // Add label class.
      if (isset($row['label'])) {
        if ($form_state
          ->has('plugin_settings')) {
          $plugin_settings = $form_state
            ->get('plugin_settings');
          if (isset($plugin_settings[$name]['ft']['settings']) && !empty($plugin_settings[$name]['ft']['settings']['lb'])) {
            $row['human_name']['#plain_text'] = $plugin_settings[$name]['ft']['settings']['lb'] . ' ' . t('(Original: @orig)', [
              '@orig' => $row['human_name']['#plain_text'],
            ]);
          }
        }
      }

      // Add region.
      // Core added regions to Field UI, which default to 'content'
      if (!isset($row['region'])) {
        $split = 7;
        $default = isset($field_regions[$name]) && isset($region_options[$field_regions[$name]]) ? $field_regions[$name] : 'hidden';
        $second = array_splice($row, $split);
        $row['region'] = $region;
        $row['region']['#default_value'] = $default;
        $row = array_merge($row, $second);
      }
      else {
        $region_default_value = $row['region']['#default_value'];
        $default = isset($field_regions[$name]) && isset($region_options[$field_regions[$name]]) ? $field_regions[$name] : 'hidden';
        if ($region_default_value == 'content' && !isset($region_options['content'])) {
          $default = key($region_options);
        }
        $row['region']['#options'] = $region_options;
        $row['region']['#default_value'] = $default;
      }
    }
  }
  return $form;
}