You are here

function ds_field_template_settings_form in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 ds.module \ds_field_template_settings_form()
  2. 8.2 ds.module \ds_field_template_settings_form()

Field template settings form.

2 calls to ds_field_template_settings_form()
ds_field_formatter_third_party_settings_form in ./ds.module
Implements hook_field_formatter_third_party_settings_form().
ds_field_settings_form in includes/field_ui.inc
Creates a form for Display Suite fields.

File

./ds.module, line 897
Display Suite core functions.

Code

function ds_field_template_settings_form(array &$form, FormStateInterface &$form_state, array $context) {
  $functions = Ds::getFieldLayoutOptions();
  $default_field_function = \Drupal::config('ds.settings')
    ->get('ft-default');
  if (empty($default_field_function)) {
    $default_field_function = 'default';
  }

  // @todo fix me - and rename 'instance' to field :)
  if (is_array($context['instance'])) {
    $key = $context['instance']['field_name'];
  }
  else {
    $key = $context['instance']
      ->getName();
  }

  // Check if this is the layout builder field block form
  $is_layout_builder = $form_state
    ->getBuildInfo()['base_form_id'] == 'layout_builder_configure_block';

  // Plugin settings.
  if ($is_layout_builder) {
    $field_settings = $context['formatter']
      ->getThirdPartySetting('ds', 'ft');
  }
  else {
    $plugin_settings = $form_state
      ->get('plugin_settings');
    $field_settings = isset($plugin_settings[$key]['ft']) ? $plugin_settings[$key]['ft'] : [];
  }

  // In case with an ajax refresh we fetch the function from a different place.
  $values = $form_state
    ->getValues();
  if (isset($values['fields'][$key]['settings_edit_form']['settings']['ft']['id'])) {
    $field_function = $values['fields'][$key]['settings_edit_form']['settings']['ft']['id'];
  }
  elseif (isset($values['fields'][$key]['settings_edit_form']['third_party_settings']['ds']['ft']['id'])) {
    $field_function = $values['fields'][$key]['settings_edit_form']['third_party_settings']['ds']['ft']['id'];
  }
  elseif (isset($values['settings']['formatter']['third_party_settings']['ds']['ft']['id'])) {
    $field_function = $values['settings']['formatter']['third_party_settings']['ds']['ft']['id'];
  }
  else {
    $field_function = isset($field_settings['id']) ? $field_settings['id'] : $default_field_function;
  }
  if (!isset($functions[$field_function])) {
    $field_function = $default_field_function;
  }
  $form['ft'] = [
    '#weight' => 20,
  ];
  $form['ft']['id'] = [
    '#title' => t('Choose a Field Template'),
    '#type' => 'select',
    '#options' => $functions,
    '#default_value' => $field_function,
    '#submit' => [
      'ds_field_ui_display_overview_multistep_submit',
    ],
  ];

  // Support layout builder field blocks
  if ($is_layout_builder) {
    $form['ft']['id']['#ajax'] = [
      'callback' => 'ds_layout_builder_multistep_js',
      'wrapper' => 'formatter-settings-wrapper',
    ];
  }
  else {
    $form['ft']['id']['#ajax'] = [
      'callback' => 'ds_field_ui_display_overview_multistep_js',
      'wrapper' => 'field-display-overview-wrapper',
    ];
  }

  // Create field layout plugin instance.
  $config = isset($field_settings['settings']) ? $field_settings['settings'] : [];
  $field_layout_instance = \Drupal::service('plugin.manager.ds.field.layout')
    ->createInstance($field_function, $config);

  // Alter the form to add specific field layout settings.
  $form['ft']['settings'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'ft-settings',
      ],
    ],
  ];
  $field_layout_instance
    ->alterForm($form['ft']['settings']);
}