You are here

function ds_field_ui_fields_save in Display Suite 8.3

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

Save the field settings from the 'Manage display' screen.

1 string reference to 'ds_field_ui_fields_save'
_ds_field_ui_table_layouts in includes/field_ui.inc
Add the layouts fieldset on the Field UI screen.

File

includes/field_ui.inc, line 535
Field UI functions for Display Suite.

Code

function ds_field_ui_fields_save($form, FormStateInterface $form_state) {
  if (empty($form['#ds_fields']) || $form_state
    ->has('ignore_ds_fields')) {
    return;
  }

  // Get the entity display.

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

  /* @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
  $display = $entity_form
    ->getEntity();
  $field_settings = [];

  // Fetch values from form_state.
  $all_field_values = $form_state
    ->getValue('fields');
  $all_plugin_settings = $form_state
    ->get('plugin_settings');

  // Save settings for each Display Suite field.
  $ds_fields = $form['#ds_fields'];
  foreach ($ds_fields as $field) {
    $field_values = $all_field_values[$field];

    // In case the region is hidden, do not save.
    if (isset($field_values['region']) && $field_values['region'] == 'hidden') {
      continue;
    }

    // Build settings.
    $settings = [];
    $settings['plugin_id'] = $field;
    $settings['weight'] = $field_values['weight'];
    $settings['label'] = $field_values['label'];
    $settings['formatter'] = $field_values['plugin']['type'];

    // Any formatter settings.
    if (isset($all_plugin_settings[$field]['settings'])) {
      $settings['settings'] = $all_plugin_settings[$field]['settings'];
    }
    elseif (isset($all_plugin_settings[$field])) {

      // When the settings for that fields aren't changed the structure is
      // different.
      // @todo figure out how we can fix this.
      $settings['settings'] = $all_plugin_settings[$field];
    }
    $field_settings[$field] = $settings;

    // Always unset the field template settings from the regular settings array.
    // @todo needs serious review.
    unset($field_settings[$field]['settings']['ft']);

    // Add field template plugin settings if provided.
    $values = isset($all_plugin_settings[$field]['ft']) ? $all_plugin_settings[$field]['ft'] : [];
    if (!empty($values)) {
      $field_settings[$field]['ft'] = $values;
      $default_field_function = \Drupal::config('ds.settings')
        ->get('ft-default');
      $function = isset($values['id']) ? $values['id'] : $default_field_function;
      $field_settings[$field]['ft']['id'] = $function;

      // It's important for schema validation to never save empty arrays.
      if (empty($field_settings[$field]['ft']['settings'])) {
        unset($field_settings[$field]['ft']['settings']);
      }
    }

    // Last but not least unset the settings if they are empty after moving the
    // field template settings.
    if (empty($field_settings[$field]['settings'])) {
      unset($field_settings[$field]['settings']);
    }
  }

  // Save the record.
  $display
    ->unsetThirdPartySetting('ds', 'fields');
  if (!empty($field_settings)) {
    $display
      ->setThirdPartySetting('ds', 'fields', $field_settings);
  }
  $display
    ->save();

  // Clear the ds_fields cache.
  Cache::invalidateTags([
    'ds_fields_info',
  ]);
}