public function FieldOverview::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/FieldOverview.php, line 542
Definition of Drupal\field_ui\FieldOverview.

Class

FieldOverview
Field UI field overview form.

Namespace

Drupal\field_ui

Code

public function submit(array $form, array &$form_state) {
  $form_values = $form_state['values']['fields'];
  $bundle_settings = field_bundle_settings($this->entity_type, $this->bundle);

  // Update field weights.
  foreach ($form_values as $key => $values) {
    if (in_array($key, $form['#fields'])) {
      $instance = field_read_instance($this->entity_type, $key, $this->bundle);
      $instance['widget']['weight'] = $values['weight'];
      field_update_instance($instance);
    }
    elseif (in_array($key, $form['#extra'])) {
      $bundle_settings['extra_fields']['form'][$key]['weight'] = $values['weight'];
    }
  }
  field_bundle_settings($this->entity_type, $this->bundle, $bundle_settings);
  $destinations = array();

  // Create new field.
  $field = array();
  if (!empty($form_values['_add_new_field']['field_name'])) {
    $values = $form_values['_add_new_field'];
    $field = array(
      'field_name' => $values['field_name'],
      'type' => $values['type'],
      'translatable' => $values['translatable'],
    );
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => $this->entity_type,
      'bundle' => $this->bundle,
      'label' => $values['label'],
      'widget' => array(
        'type' => $values['widget_type'],
        'weight' => $values['weight'],
      ),
    );

    // Create the field and instance.
    try {
      field_create_field($field);
      field_create_instance($instance);
      $destinations[] = $this->adminPath . '/fields/' . $field['field_name'] . '/field-settings';
      $destinations[] = $this->adminPath . '/fields/' . $field['field_name'];

      // Store new field information for any additional submit handlers.
      $form_state['fields_added']['_add_new_field'] = $field['field_name'];
    } catch (Exception $e) {
      drupal_set_message(t('There was a problem creating field %label: !message', array(
        '%label' => $instance['label'],
        '!message' => $e
          ->getMessage(),
      )), 'error');
    }
  }

  // Re-use existing field.
  if (!empty($form_values['_add_existing_field']['field_name'])) {
    $values = $form_values['_add_existing_field'];
    $field = field_info_field($values['field_name']);
    if (!empty($field['locked'])) {
      drupal_set_message(t('The field %label cannot be added because it is locked.', array(
        '%label' => $values['label'],
      )), 'error');
    }
    else {
      $instance = array(
        'field_name' => $field['field_name'],
        'entity_type' => $this->entity_type,
        'bundle' => $this->bundle,
        'label' => $values['label'],
        'widget' => array(
          'type' => $values['widget_type'],
          'weight' => $values['weight'],
        ),
      );
      try {
        field_create_instance($instance);
        $destinations[] = $this->adminPath . '/fields/' . $instance['field_name'] . '/edit';

        // Store new field information for any additional submit handlers.
        $form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
      } catch (Exception $e) {
        drupal_set_message(t('There was a problem creating field instance %label: @message.', array(
          '%label' => $instance['label'],
          '@message' => $e
            ->getMessage(),
        )), 'error');
      }
    }
  }
  if ($destinations) {
    $destination = drupal_get_destination();
    $destinations[] = $destination['destination'];
    unset($_GET['destination']);
    $form_state['redirect'] = field_ui_get_destinations($destinations);
  }
  else {
    drupal_set_message(t('Your settings have been saved.'));
  }
}