Mercurial > hg > isophonics-drupal-site
comparison core/modules/block_content/src/BlockContentForm.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\block_content; | |
4 | |
5 use Drupal\Component\Utility\Html; | |
6 use Drupal\Core\Entity\ContentEntityForm; | |
7 use Drupal\Core\Form\FormStateInterface; | |
8 | |
9 /** | |
10 * Form handler for the custom block edit forms. | |
11 */ | |
12 class BlockContentForm extends ContentEntityForm { | |
13 | |
14 /** | |
15 * The block content entity. | |
16 * | |
17 * @var \Drupal\block_content\BlockContentInterface | |
18 */ | |
19 protected $entity; | |
20 | |
21 /** | |
22 * {@inheritdoc} | |
23 */ | |
24 public function form(array $form, FormStateInterface $form_state) { | |
25 $block = $this->entity; | |
26 | |
27 $form = parent::form($form, $form_state); | |
28 | |
29 if ($this->operation == 'edit') { | |
30 $form['#title'] = $this->t('Edit custom block %label', ['%label' => $block->label()]); | |
31 } | |
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 | |
34 // names. | |
35 $form['#attributes']['class'][0] = 'block-' . Html::getClass($block->bundle()) . '-form'; | |
36 | |
37 return $form; | |
38 } | |
39 | |
40 /** | |
41 * {@inheritdoc} | |
42 */ | |
43 public function save(array $form, FormStateInterface $form_state) { | |
44 $block = $this->entity; | |
45 | |
46 $insert = $block->isNew(); | |
47 $block->save(); | |
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()]; | |
52 | |
53 if ($insert) { | |
54 $logger->notice('@type: added %info.', $context); | |
55 drupal_set_message($this->t('@type %info has been created.', $t_args)); | |
56 } | |
57 else { | |
58 $logger->notice('@type: updated %info.', $context); | |
59 drupal_set_message($this->t('@type %info has been updated.', $t_args)); | |
60 } | |
61 | |
62 if ($block->id()) { | |
63 $form_state->setValue('id', $block->id()); | |
64 $form_state->set('id', $block->id()); | |
65 if ($insert) { | |
66 if (!$theme = $block->getTheme()) { | |
67 $theme = $this->config('system.theme')->get('default'); | |
68 } | |
69 $form_state->setRedirect( | |
70 'block.admin_add', | |
71 [ | |
72 'plugin_id' => 'block_content:' . $block->uuid(), | |
73 'theme' => $theme, | |
74 ] | |
75 ); | |
76 } | |
77 else { | |
78 $form_state->setRedirectUrl($block->urlInfo('collection')); | |
79 } | |
80 } | |
81 else { | |
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(); | |
86 } | |
87 } | |
88 | |
89 } |