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.
12 class BlockContentForm extends ContentEntityForm {
15 * The block content entity.
17 * @var \Drupal\block_content\BlockContentInterface
24 public function form(array $form, FormStateInterface $form_state) {
25 $block = $this->entity;
27 $form = parent::form($form, $form_state);
29 if ($this->operation == 'edit') {
30 $form['#title'] = $this->t('Edit custom block %label', ['%label' => $block->label()]);
32 // Override the default CSS class name, since the user-defined custom block
33 // type name in 'TYPE-block-form' potentially clashes with third-party class
35 $form['#attributes']['class'][0] = 'block-' . Html::getClass($block->bundle()) . '-form';
43 public function save(array $form, FormStateInterface $form_state) {
44 $block = $this->entity;
46 $insert = $block->isNew();
48 $context = ['@type' => $block->bundle(), '%info' => $block->label()];
49 $logger = $this->logger('block_content');
50 $block_type = $this->getBundleEntity();
51 $t_args = ['@type' => $block_type->label(), '%info' => $block->label()];
54 $logger->notice('@type: added %info.', $context);
55 drupal_set_message($this->t('@type %info has been created.', $t_args));
58 $logger->notice('@type: updated %info.', $context);
59 drupal_set_message($this->t('@type %info has been updated.', $t_args));
63 $form_state->setValue('id', $block->id());
64 $form_state->set('id', $block->id());
66 if (!$theme = $block->getTheme()) {
67 $theme = $this->config('system.theme')->get('default');
69 $form_state->setRedirect(
72 'plugin_id' => 'block_content:' . $block->uuid(),
78 $form_state->setRedirectUrl($block->urlInfo('collection'));
82 // In the unlikely case something went wrong on save, the block will be
83 // rebuilt and block form redisplayed.
84 drupal_set_message($this->t('The block could not be saved.'), 'error');
85 $form_state->setRebuild();