You are here

public function MediaBundleForm::save in Media entity 8

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/MediaBundleForm.php, line 360

Class

MediaBundleForm
Form controller for node type forms.

Namespace

Drupal\media_entity

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $this->entity;
  $status = $bundle
    ->save();
  $t_args = [
    '%name' => $bundle
      ->label(),
  ];
  if ($status == SAVED_UPDATED) {
    drupal_set_message($this
      ->t('The media bundle %name has been updated.', $t_args));
  }
  elseif ($status == SAVED_NEW) {
    drupal_set_message($this
      ->t('The media bundle %name has been added.', $t_args));
    $this
      ->logger('media')
      ->notice('Added bundle %name.', $t_args);
  }

  // Override the "status" base field default value, for this bundle.
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('media', $bundle
    ->id());
  $media = $this->entityTypeManager
    ->getStorage('media')
    ->create(array(
    'bundle' => $bundle
      ->id(),
  ));
  $value = (bool) $form_state
    ->getValue([
    'options',
    'status',
  ]);
  if ($media->status->value != $value) {
    $fields['status']
      ->getConfig($bundle
      ->id())
      ->setDefaultValue($value)
      ->save();
  }
  $form_state
    ->setRedirectUrl($bundle
    ->toUrl('collection'));
}