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