Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\block_content;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Core\Entity\ContentEntityForm;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Form handler for the custom block edit forms.
|
Chris@14
|
11 *
|
Chris@14
|
12 * @internal
|
Chris@0
|
13 */
|
Chris@0
|
14 class BlockContentForm extends ContentEntityForm {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The block content entity.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var \Drupal\block_content\BlockContentInterface
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $entity;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
27 $block = $this->entity;
|
Chris@0
|
28
|
Chris@0
|
29 $form = parent::form($form, $form_state);
|
Chris@0
|
30
|
Chris@0
|
31 if ($this->operation == 'edit') {
|
Chris@0
|
32 $form['#title'] = $this->t('Edit custom block %label', ['%label' => $block->label()]);
|
Chris@0
|
33 }
|
Chris@0
|
34 // Override the default CSS class name, since the user-defined custom block
|
Chris@0
|
35 // type name in 'TYPE-block-form' potentially clashes with third-party class
|
Chris@0
|
36 // names.
|
Chris@0
|
37 $form['#attributes']['class'][0] = 'block-' . Html::getClass($block->bundle()) . '-form';
|
Chris@0
|
38
|
Chris@0
|
39 return $form;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
46 $block = $this->entity;
|
Chris@0
|
47
|
Chris@0
|
48 $insert = $block->isNew();
|
Chris@0
|
49 $block->save();
|
Chris@0
|
50 $context = ['@type' => $block->bundle(), '%info' => $block->label()];
|
Chris@0
|
51 $logger = $this->logger('block_content');
|
Chris@0
|
52 $block_type = $this->getBundleEntity();
|
Chris@0
|
53 $t_args = ['@type' => $block_type->label(), '%info' => $block->label()];
|
Chris@0
|
54
|
Chris@0
|
55 if ($insert) {
|
Chris@0
|
56 $logger->notice('@type: added %info.', $context);
|
Chris@17
|
57 $this->messenger()->addStatus($this->t('@type %info has been created.', $t_args));
|
Chris@0
|
58 }
|
Chris@0
|
59 else {
|
Chris@0
|
60 $logger->notice('@type: updated %info.', $context);
|
Chris@17
|
61 $this->messenger()->addStatus($this->t('@type %info has been updated.', $t_args));
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 if ($block->id()) {
|
Chris@0
|
65 $form_state->setValue('id', $block->id());
|
Chris@0
|
66 $form_state->set('id', $block->id());
|
Chris@0
|
67 if ($insert) {
|
Chris@0
|
68 if (!$theme = $block->getTheme()) {
|
Chris@0
|
69 $theme = $this->config('system.theme')->get('default');
|
Chris@0
|
70 }
|
Chris@0
|
71 $form_state->setRedirect(
|
Chris@0
|
72 'block.admin_add',
|
Chris@0
|
73 [
|
Chris@0
|
74 'plugin_id' => 'block_content:' . $block->uuid(),
|
Chris@0
|
75 'theme' => $theme,
|
Chris@0
|
76 ]
|
Chris@0
|
77 );
|
Chris@0
|
78 }
|
Chris@0
|
79 else {
|
Chris@18
|
80 $form_state->setRedirectUrl($block->toUrl('collection'));
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 else {
|
Chris@0
|
84 // In the unlikely case something went wrong on save, the block will be
|
Chris@0
|
85 // rebuilt and block form redisplayed.
|
Chris@17
|
86 $this->messenger()->addError($this->t('The block could not be saved.'));
|
Chris@0
|
87 $form_state->setRebuild();
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 }
|