public function FieldOverview::submitForm

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

Overrides OverviewBase::submitForm

File

drupal/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php, line 576
Contains \Drupal\field_ui\FieldOverview.

Class

FieldOverview
Field UI field overview form.

Namespace

Drupal\field_ui

Code

public function submitForm(array &$form, array &$form_state) {
  $form_values = $form_state['values']['fields'];
  $entity_form_display = entity_get_form_display($this->entity_type, $this->bundle, $this->mode);

  // Collect data for 'regular' fields.
  foreach ($form['#fields'] as $field_name) {
    $options = $entity_form_display
      ->getComponent($field_name);
    $options['weight'] = $form_values[$field_name]['weight'];
    $entity_form_display
      ->setComponent($field_name, $options);
  }

  // Collect data for 'extra' fields.
  foreach ($form['#extra'] as $field_name) {
    $entity_form_display
      ->setComponent($field_name, array(
      'weight' => $form_values[$field_name]['weight'],
    ));
  }

  // Save the form display.
  $entity_form_display
    ->save();
  $destinations = array();

  // Create new field.
  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'],
    );

    // Create the field and instance.
    try {
      $this->entityManager
        ->getStorageController('field_entity')
        ->create($field)
        ->save();
      $new_instance = $this->entityManager
        ->getStorageController('field_instance')
        ->create($instance);
      $new_instance
        ->save();

      // Make sure the field is displayed in the 'default' form mode (using
      // the configured widget and default settings).
      entity_get_form_display($this->entity_type, $this->bundle, 'default')
        ->setComponent($field['field_name'], array(
        'type' => $values['widget_type'],
        'weight' => $values['weight'],
      ))
        ->save();

      // Make sure the field is displayed in the 'default' view mode (using
      // default formatter and settings). It stays hidden for other view
      // modes until it is explicitly configured.
      entity_get_display($this->entity_type, $this->bundle, 'default')
        ->setComponent($field['field_name'])
        ->save();

      // Always show the field settings step, as the cardinality needs to be
      // configured for new fields.
      $destinations[] = $this->adminPath . '/fields/' . $new_instance
        ->id() . '/field';
      $destinations[] = $this->adminPath . '/fields/' . $new_instance
        ->id();

      // 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'],
      );
      try {
        $new_instance = $this->entityManager
          ->getStorageController('field_instance')
          ->create($instance);
        $new_instance
          ->save();

        // Make sure the field is displayed in the 'default' form mode (using
        // the configured widget and default settings).
        entity_get_form_display($this->entity_type, $this->bundle, 'default')
          ->setComponent($field['field_name'], array(
          'type' => $values['widget_type'],
          'weight' => $values['weight'],
        ))
          ->save();

        // Make sure the field is displayed in the 'default' view mode (using
        // default formatter and settings). It stays hidden for other view
        // modes until it is explicitly configured.
        entity_get_display($this->entity_type, $this->bundle, 'default')
          ->setComponent($field['field_name'])
          ->save();
        $destinations[] = $this->adminPath . '/fields/' . $new_instance
          ->id();

        // 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']);
    $path = array_shift($destinations);
    $options = drupal_parse_url($path);
    $options['query']['destinations'] = $destinations;
    $form_state['redirect'] = array(
      $options['path'],
      $options,
    );
  }
  else {
    drupal_set_message(t('Your settings have been saved.'));
  }
}