You are here

public function FieldCollectionItemForm::save in Field collection 8.3

Same name and namespace in other branches
  1. 8 src/FieldCollectionItemForm.php \Drupal\field_collection\FieldCollectionItemForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/FieldCollectionItemForm.php, line 77

Class

FieldCollectionItemForm

Namespace

Drupal\field_collection

Code

public function save(array $form, FormStateInterface $form_state) {
  $field_collection_item = $this
    ->getEntity();
  if ($field_collection_item
    ->isNew()) {
    $host = $this->entityTypeManager
      ->getStorage($this
      ->getRequest()
      ->get('host_type'))
      ->load($this
      ->getRequest()
      ->get('host_id'));
    $field_collection_item
      ->setHostEntity($host);
    $field_collection_item
      ->save();
    $host
      ->save();
    $messages = drupal_get_messages(NULL, false);
    if (!isset($messages['warning']) && !isset($messages['error'])) {
      drupal_set_message(t('Successfully added a @type.', [
        '@type' => $field_collection_item
          ->bundle(),
      ]));
    }
  }
  else {
    $messages = drupal_get_messages(NULL, false);
    if (!isset($messages['warning']) && !isset($messages['error'])) {
      $field_collection_item
        ->save();
      drupal_set_message(t('Successfully edited %label.', [
        '%label' => $field_collection_item
          ->label(),
      ]));
    }
  }
  if ($field_collection_item
    ->id()) {
    $form_state
      ->setValue('id', $field_collection_item
      ->id());
    $form_state
      ->set('id', $field_collection_item
      ->id());
  }
  else {

    // In the unlikely case something went wrong on save, the block will be
    // rebuilt and block form redisplayed.
    drupal_set_message(t('The field collection item could not be saved.'), 'error');
    $form_state
      ->setRebuild();
  }

  /*
  $form_state->setRedirect(
    'field_collection_item.view',
    ['field_collection_item' => $field_collection_item->id()
  ]);
  */
}