Backup of db before drupal security update
[yaffs-website] / web / core / modules / block_content / src / BlockContentTypeForm.php
1 <?php
2
3 namespace Drupal\block_content;
4
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\language\Entity\ContentLanguageSettings;
9
10 /**
11  * Base form for category edit forms.
12  */
13 class BlockContentTypeForm extends BundleEntityFormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function form(array $form, FormStateInterface $form_state) {
19     $form = parent::form($form, $form_state);
20
21     /* @var \Drupal\block_content\BlockContentTypeInterface $block_type */
22     $block_type = $this->entity;
23
24     if ($this->operation == 'add') {
25       $form['#title'] = $this->t('Add custom block type');
26     }
27     else {
28       $form['#title'] = $this->t('Edit %label custom block type', ['%label' => $block_type->label()]);
29     }
30
31     $form['label'] = [
32       '#type' => 'textfield',
33       '#title' => t('Label'),
34       '#maxlength' => 255,
35       '#default_value' => $block_type->label(),
36       '#description' => t("Provide a label for this block type to help identify it in the administration pages."),
37       '#required' => TRUE,
38     ];
39     $form['id'] = [
40       '#type' => 'machine_name',
41       '#default_value' => $block_type->id(),
42       '#machine_name' => [
43         'exists' => '\Drupal\block_content\Entity\BlockContentType::load',
44       ],
45       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
46     ];
47
48     $form['description'] = [
49       '#type' => 'textarea',
50       '#default_value' => $block_type->getDescription(),
51       '#description' => t('Enter a description for this block type.'),
52       '#title' => t('Description'),
53     ];
54
55     $form['revision'] = [
56       '#type' => 'checkbox',
57       '#title' => t('Create new revision'),
58       '#default_value' => $block_type->shouldCreateNewRevision(),
59       '#description' => t('Create a new revision by default for this block type.'),
60     ];
61
62     if ($this->moduleHandler->moduleExists('language')) {
63       $form['language'] = [
64         '#type' => 'details',
65         '#title' => t('Language settings'),
66         '#group' => 'additional_settings',
67       ];
68
69       $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('block_content', $block_type->id());
70       $form['language']['language_configuration'] = [
71         '#type' => 'language_configuration',
72         '#entity_information' => [
73           'entity_type' => 'block_content',
74           'bundle' => $block_type->id(),
75         ],
76         '#default_value' => $language_configuration,
77       ];
78
79       $form['#submit'][] = 'language_configuration_element_submit';
80     }
81
82     $form['actions'] = ['#type' => 'actions'];
83     $form['actions']['submit'] = [
84       '#type' => 'submit',
85       '#value' => t('Save'),
86     ];
87
88     return $this->protectBundleIdElement($form);
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function save(array $form, FormStateInterface $form_state) {
95     $block_type = $this->entity;
96     $status = $block_type->save();
97
98     $edit_link = $this->entity->link($this->t('Edit'));
99     $logger = $this->logger('block_content');
100     if ($status == SAVED_UPDATED) {
101       drupal_set_message(t('Custom block type %label has been updated.', ['%label' => $block_type->label()]));
102       $logger->notice('Custom block type %label has been updated.', ['%label' => $block_type->label(), 'link' => $edit_link]);
103     }
104     else {
105       block_content_add_body_field($block_type->id());
106       drupal_set_message(t('Custom block type %label has been added.', ['%label' => $block_type->label()]));
107       $logger->notice('Custom block type %label has been added.', ['%label' => $block_type->label(), 'link' => $edit_link]);
108     }
109
110     $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
111   }
112
113 }