3 namespace Drupal\block_content;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Form\FormStateInterface;
10 * Form handler for the custom block edit forms.
14 class BlockContentForm extends ContentEntityForm {
17 * The block content entity.
19 * @var \Drupal\block_content\BlockContentInterface
26 public function form(array $form, FormStateInterface $form_state) {
27 $block = $this->entity;
29 $form = parent::form($form, $form_state);
31 if ($this->operation == 'edit') {
32 $form['#title'] = $this->t('Edit custom block %label', ['%label' => $block->label()]);
34 // Override the default CSS class name, since the user-defined custom block
35 // type name in 'TYPE-block-form' potentially clashes with third-party class
37 $form['#attributes']['class'][0] = 'block-' . Html::getClass($block->bundle()) . '-form';
45 public function save(array $form, FormStateInterface $form_state) {
46 $block = $this->entity;
48 $insert = $block->isNew();
50 $context = ['@type' => $block->bundle(), '%info' => $block->label()];
51 $logger = $this->logger('block_content');
52 $block_type = $this->getBundleEntity();
53 $t_args = ['@type' => $block_type->label(), '%info' => $block->label()];
56 $logger->notice('@type: added %info.', $context);
57 drupal_set_message($this->t('@type %info has been created.', $t_args));
60 $logger->notice('@type: updated %info.', $context);
61 drupal_set_message($this->t('@type %info has been updated.', $t_args));
65 $form_state->setValue('id', $block->id());
66 $form_state->set('id', $block->id());
68 if (!$theme = $block->getTheme()) {
69 $theme = $this->config('system.theme')->get('default');
71 $form_state->setRedirect(
74 'plugin_id' => 'block_content:' . $block->uuid(),
80 $form_state->setRedirectUrl($block->urlInfo('collection'));
84 // In the unlikely case something went wrong on save, the block will be
85 // rebuilt and block form redisplayed.
86 drupal_set_message($this->t('The block could not be saved.'), 'error');
87 $form_state->setRebuild();