You are here

function _ds_field_ui_fields in Display Suite 8.3

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

Add the fields to the Field UI form.

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: A collection of form_state properties.

1 call to _ds_field_ui_fields()
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 1142
Field UI functions for Display Suite.

Code

function _ds_field_ui_fields($entity_type, $bundle, $view_mode, array &$form, FormStateInterface $form_state) {

  // Do not add the fields if there is no layout.
  if (!isset($form['#ds_layout'])) {
    return;
  }

  // Get the fields and put them on the form.
  $fields = Ds::getFields($entity_type);

  // Get field settings.
  $field_settings = $form['#ds_layout']['fields'];
  $form['#field_settings'] = $field_settings;
  if (empty($form['fields'])) {

    // EntityDisplayFormBase doesn't add the fields table and field_ui library
    // when there are no fields.  Since DS needs those, we add them.
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
    $form['fields'] = [
      '#type' => 'field_ui_table',
      '#weight' => -101,
      '#header' => [
        t('Field'),
        t('Weight'),
        t('Parent'),
        t('Region'),
        t('Label'),
        [
          'data' => t('Format'),
          'colspan' => 3,
        ],
      ],
      '#regions' => [],
      '#attributes' => [
        'class' => [
          'field-ui-overview',
        ],
        'id' => 'field-display-overview',
      ],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'field-weight',
        ],
        [
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'field-parent',
          'subgroup' => 'field-parent',
          'source' => 'field-name',
        ],
        [
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'field-region',
          'subgroup' => 'field-region',
          'source' => 'field-name',
        ],
      ],
    ];

    // Check for the 'There are no fields yet added. You can add new fields on
    // the Manage fields page.' warning message. We really don't need it.
    $warnings = \Drupal::messenger()
      ->messagesByType(MessengerInterface::TYPE_WARNING);
    if (count($warnings) == 1) {
      \Drupal::messenger()
        ->deleteByType(MessengerInterface::TYPE_WARNING);
    }

    // In overviews involving nested rows from contributed modules (i.e
    // field_group), the 'plugin type' selects can trigger a series of changes
    // in child rows. The #ajax behavior is therefore not attached directly to
    // the selects, but triggered by the client-side script through a hidden
    // #ajax 'Refresh' button. A hidden 'refresh_rows' input tracks the name of
    // affected rows.
    $form['refresh_rows'] = [
      '#type' => 'hidden',
    ];
    $form['refresh'] = [
      '#type' => 'submit',
      '#value' => t('Refresh'),
      '#op' => 'refresh_table',
      '#submit' => [
        '::multistepSubmit',
      ],
      '#ajax' => [
        'callback' => '::multistepAjax',
        'wrapper' => 'field-display-overview-wrapper',
        'effect' => 'fade',
        // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
        // spinners will be added manually by the client-side script.
        'progress' => 'none',
      ],
      '#attributes' => [
        'class' => [
          'visually-hidden',
        ],
      ],
    ];
  }
  $table =& $form['fields'];
  $form['#ds_fields'] = [];
  $field_label_options = [
    'above' => t('Above'),
    'inline' => t('Inline'),
    'hidden' => t('- Hidden -'),
    'visually_hidden' => t('- Visually Hidden -'),
  ];
  \Drupal::moduleHandler()
    ->alter('ds_label_options', $field_label_options);

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

  /* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = $entity_form
    ->getEntity();
  $parent_options = [];
  if (function_exists('field_group_field_ui_form_params')) {
    $field_group_params = field_group_field_ui_form_params($form, $display);
    foreach ($field_group_params->groups as $name => $group) {
      $parent_options[$name] = $group->label;
    }
    $parent_options['_add_new_group'] = t('Add new group');
  }
  foreach ($fields as $key => $field) {
    if (isset($field_settings[$key]['formatter'])) {
      $field['formatter'] = $field_settings[$key]['formatter'];
    }
    $configuration = [
      'field' => $field,
      'field_name' => $key,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'view_mode' => $view_mode,
    ];

    // Check if we can display this field here.

    /* @var $plugin_instance DsFieldInterface */
    $plugin_instance = \Drupal::service('plugin.manager.ds')
      ->createInstance($field['plugin_id'], $configuration);
    if (!$plugin_instance
      ->isAllowed()) {
      continue;
    }

    // Don't filter out fields when $displays is empty.
    if (!empty($displays)) {
      $continue = TRUE;
      foreach ($displays as $limitation) {
        list($limit_bundle, $limit_view_mode) = explode('|', $limitation);
        if ($limit_bundle == '*' || $limit_bundle == $bundle) {
          if ($limit_view_mode == '*' || $limit_view_mode == $view_mode) {
            $continue = FALSE;
          }
        }
      }
      if ($continue) {
        continue;
      }
    }
    $form['#ds_fields'][] = $key;

    // Fetch saved plugin settings.
    $form_state_plugin_settings = $form_state
      ->get('plugin_settings');

    // Check on formatter settings.
    $plugin_settings = [];
    if (isset($form_state_plugin_settings[$key])) {
      $plugin_settings = $form_state_plugin_settings[$key];
    }
    elseif (isset($field_settings[$key]['settings']) || isset($field_settings[$key]['ft']) || isset($field_settings[$key]['ds_limit'])) {
      if (isset($field_settings[$key]['settings'])) {
        $plugin_settings = $field_settings[$key]['settings'];
      }
      if (isset($field_settings[$key]['ft'])) {
        $plugin_settings['ft'] = $field_settings[$key]['ft'];
      }
      if (isset($field_settings[$key]['ds_limit'])) {
        $plugin_settings['ds_limit'] = $field_settings[$key]['ds_limit'];
      }
      $form_state_plugin_settings[$key] = $plugin_settings;
    }
    $plugin_instance
      ->setConfiguration($plugin_settings);

    // Save fetched plugin settings.
    $form_state
      ->set('plugin_settings', $form_state_plugin_settings);
    $hidden = [
      'hidden' => t('- Hidden -'),
    ];

    // Get the formatters from the field instance.
    $formatters = $plugin_instance
      ->formatters();

    // This should be temporary. Don't want to copy stuff from the object to
    // the field each ajax refresh.
    if (!empty($formatters)) {
      $formatters = $hidden + $formatters;
    }
    else {
      $formatters = $hidden + [
        'default' => t('Default'),
      ];
    }
    $table[$key] = [
      '#row_type' => 'field',
      '#js_settings' => [
        'field',
      ],
      '#region_callback' => 'field_ui_display_overview_row_region',
      '#attributes' => [
        'class' => [
          'draggable',
          'tabledrag-leaf',
        ],
      ],
      'human_name' => [
        '#plain_text' => $field['title'],
      ],
      'weight' => [
        '#type' => 'textfield',
        '#default_value' => isset($field_settings[$key]['weight']) ? $field_settings[$key]['weight'] : 0,
        '#size' => 3,
        '#attributes' => [
          'class' => [
            'field-weight',
          ],
        ],
      ],
      'parent_wrapper' => [
        'parent' => [
          '#type' => 'select',
          '#empty_value' => '',
          '#options' => $parent_options,
          '#default_value' => isset($field_group_params->parents[$key]) ? $field_group_params->parents[$key] : '',
          '#attributes' => [
            'class' => [
              'field-parent',
            ],
          ],
          '#parents' => [
            'fields',
            $key,
            'parent',
          ],
        ],
        'hidden_name' => [
          '#type' => 'hidden',
          '#default_value' => $key,
          '#attributes' => [
            'class' => [
              'field-name',
            ],
          ],
        ],
      ],
      'label' => [
        '#type' => 'select',
        '#options' => $field_label_options,
        '#default_value' => isset($field_settings[$key]['label']) ? $field_settings[$key]['label'] : 'hidden',
      ],
      'plugin' => [
        'type' => [
          '#type' => 'select',
          '#options' => $formatters,
          '#default_value' => isset($field_settings[$key]['formatter']) ? $field_settings[$key]['formatter'] : 'hidden',
          '#attributes' => [
            'class' => [
              'field-plugin-type',
            ],
          ],
        ],
      ],
      'settings_summary' => [],
      'settings_edit' => [],
    ];
    if ($form_state
      ->get('plugin_settings_edit') == $key) {
      $table[$key]['settings_summary']['#attributes']['colspan'] = 2;
      $settings_form = ds_field_settings_form($plugin_instance, $form_state);
      ds_field_row_form_format_construct($table, $key, $settings_form, $form_state);
    }
    else {

      // After saving, the settings are updated here as well. First we create
      // the element for the table cell.
      $summary = ds_field_settings_summary($plugin_instance, $plugin_settings, $form_state);
      if (!empty($summary)) {
        $table[$key]['settings_summary'] = $summary;
        ds_field_row_form_format_summary_construct($table, $key, $form_state);
      }
    }
  }
}