function BlockContentForm::save

Same name and namespace in other branches
  1. 9 core/modules/block_content/src/BlockContentForm.php \Drupal\block_content\BlockContentForm::save()
  2. 8.9.x core/modules/block_content/src/BlockContentForm.php \Drupal\block_content\BlockContentForm::save()
  3. 10 core/modules/block_content/src/BlockContentForm.php \Drupal\block_content\BlockContentForm::save()

Overrides EntityForm::save

File

core/modules/block_content/src/BlockContentForm.php, line 89

Class

BlockContentForm
Form handler for the content block edit forms.

Namespace

Drupal\block_content

Code

public function save(array $form, FormStateInterface $form_state) {
    $block = $this->entity;
    $insert = $block->isNew();
    $block->save();
    $context = [
        '@type' => $block->bundle(),
        '%info' => $block->label(),
    ];
    $logger = $this->logger('block_content');
    $block_type = $this->getBundleEntity();
    $t_args = [
        '@type' => $block_type->label(),
        '%info' => $block->label(),
    ];
    if ($insert) {
        $logger->info('@type: added %info.', $context);
        $this->messenger()
            ->addStatus($this->t('@type %info has been created.', $t_args));
    }
    else {
        $logger->info('@type: updated %info.', $context);
        $this->messenger()
            ->addStatus($this->t('@type %info has been updated.', $t_args));
    }
    if ($block->id()) {
        $form_state->setValue('id', $block->id());
        $form_state->set('id', $block->id());
        $theme = $block->getTheme();
        if ($insert && $theme) {
            $form_state->setRedirect('block.admin_add', [
                'plugin_id' => 'block_content:' . $block->uuid(),
                'theme' => $theme,
                'region' => $this->getRequest()->query
                    ->getString('region'),
            ]);
        }
        else {
            $form_state->setRedirectUrl($block->toUrl('collection'));
        }
    }
    else {
        // In the unlikely case something went wrong on save, the block will be
        // rebuilt and block form redisplayed.
        $this->messenger()
            ->addError($this->t('The block could not be saved.'));
        $form_state->setRebuild();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.