You are here

public function BlockContentForm::save in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block_content/src/BlockContentForm.php \Drupal\block_content\BlockContentForm::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

core/modules/block_content/src/BlockContentForm.php, line 172
Contains \Drupal\block_content\BlockContentForm.

Class

BlockContentForm
Form controller for the custom block edit forms.

Namespace

Drupal\block_content

Code

public function save(array $form, FormStateInterface $form_state) {
  $block = $this->entity;

  // Save as a new revision if requested to do so.
  if (!$form_state
    ->isValueEmpty('revision')) {
    $block
      ->setNewRevision();
  }
  $insert = $block
    ->isNew();
  $block
    ->save();
  $context = array(
    '@type' => $block
      ->bundle(),
    '%info' => $block
      ->label(),
  );
  $logger = $this
    ->logger('block_content');
  $block_type = $this->blockContentTypeStorage
    ->load($block
    ->bundle());
  $t_args = array(
    '@type' => $block_type
      ->label(),
    '%info' => $block
      ->label(),
  );
  if ($insert) {
    $logger
      ->notice('@type: added %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been created.', $t_args));
  }
  else {
    $logger
      ->notice('@type: updated %info.', $context);
    drupal_set_message($this
      ->t('@type %info has been updated.', $t_args));
  }
  if ($block
    ->id()) {
    $form_state
      ->setValue('id', $block
      ->id());
    $form_state
      ->set('id', $block
      ->id());
    if ($insert) {
      if (!($theme = $block
        ->getTheme())) {
        $theme = $this
          ->config('system.theme')
          ->get('default');
      }
      $form_state
        ->setRedirect('block.admin_add', array(
        'plugin_id' => 'block_content:' . $block
          ->uuid(),
        'theme' => $theme,
      ));
    }
    else {
      $form_state
        ->setRedirectUrl($block
        ->urlInfo('collection'));
    }
  }
  else {

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