public function DisplayOverview::submit

Overrides Drupal\field_ui\OverviewBase::submit().

Parameters

array $form: An associative array containing the structure of the form.

array $form_state: A reference to a keyed array containing the current state of the form.

Overrides OverviewBase::submit

File

drupal/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php, line 419
Definition of Drupal\field_ui\DisplayOverview.

Class

DisplayOverview
Field UI display overview form.

Namespace

Drupal\field_ui

Code

public function submit(array $form, array &$form_state) {
  $form_values = $form_state['values'];

  // Save data for 'regular' fields.
  foreach ($form['#fields'] as $field_name) {

    // Retrieve the stored instance settings to merge with the incoming
    // values.
    $instance = field_read_instance($this->entity_type, $field_name, $this->bundle);
    $values = $form_values['fields'][$field_name];

    // Get formatter settings. They lie either directly in submitted form
    // values (if the whole form was submitted while some formatter
    // settings were being edited), or have been persisted in
    // $form_state.
    $settings = array();
    if (isset($values['settings_edit_form']['settings'])) {
      $settings = $values['settings_edit_form']['settings'];
    }
    elseif (isset($form_state['formatter_settings'][$field_name])) {
      $settings = $form_state['formatter_settings'][$field_name];
    }
    elseif (isset($instance['display'][$this->view_mode]['settings'])) {
      $settings = $instance['display'][$this->view_mode]['settings'];
    }

    // Only save settings actually used by the selected formatter.
    $default_settings = field_info_formatter_settings($values['type']);
    $settings = array_intersect_key($settings, $default_settings);
    $instance['display'][$this->view_mode] = array(
      'label' => $values['label'],
      'type' => $values['type'],
      'weight' => $values['weight'],
      'settings' => $settings,
    );
    field_update_instance($instance);
  }

  // Get current bundle settings.
  $bundle_settings = field_bundle_settings($this->entity_type, $this->bundle);

  // Save data for 'extra' fields.
  foreach ($form['#extra'] as $name) {
    $bundle_settings['extra_fields']['display'][$name][$this->view_mode] = array(
      'weight' => $form_values['fields'][$name]['weight'],
      'visible' => $form_values['fields'][$name]['type'] == 'content',
    );
  }

  // Save view modes data.
  if ($this->view_mode == 'default' && !empty($form_values['view_modes_custom'])) {
    $entity_info = entity_get_info($this->entity_type);
    foreach ($form_values['view_modes_custom'] as $view_mode_name => $value) {

      // Display a message for each view mode newly configured to use custom
      // settings.
      $view_mode_settings = field_view_mode_settings($this->entity_type, $this->bundle);
      if (!empty($value) && empty($view_mode_settings[$view_mode_name]['custom_settings'])) {
        $view_mode_label = $entity_info['view_modes'][$view_mode_name]['label'];
        $path = field_ui_bundle_admin_path($this->entity_type, $this->bundle) . "/display/{$view_mode_name}";
        drupal_set_message(t('The %view_mode mode now uses custom display settings. You might want to <a href="@url">configure them</a>.', array(
          '%view_mode' => $view_mode_label,
          '@url' => url($path),
        )));

        // Initialize the newly customized view mode with the display settings
        // from the default view mode.
        _field_ui_add_default_view_mode_settings($this->entity_type, $this->bundle, $view_mode_name, $bundle_settings);
      }
      $bundle_settings['view_modes'][$view_mode_name]['custom_settings'] = !empty($value);
    }
  }

  // Save updated bundle settings.
  field_bundle_settings($this->entity_type, $this->bundle, $bundle_settings);
  drupal_set_message(t('Your settings have been saved.'));
}