BlockContentTypeForm.php 3.65 KB
Newer Older
1
2
<?php

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
27
28
29
    if ($this->operation == 'add') {
      $form['#title'] = $this->t('Add custom block type');
    }
    else {
30
      $form['#title'] = $this->t('Edit %label custom block type', ['%label' => $block_type->label()]);
31
32
    }

33
    $form['label'] = [
34
35
36
37
38
39
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 255,
      '#default_value' => $block_type->label(),
      '#description' => t("Provide a label for this block type to help identify it in the administration pages."),
      '#required' => TRUE,
40
41
    ];
    $form['id'] = [
42
43
      '#type' => 'machine_name',
      '#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
54
      '#description' => t('Enter a description for this block type.'),
      '#title' => t('Description'),
55
    ];
56

57
    $form['revision'] = [
58
59
      '#type' => 'checkbox',
      '#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
67
68
        '#type' => 'details',
        '#title' => t('Language settings'),
        '#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
82
83

      $form['#submit'][] = 'language_configuration_element_submit';
    }

84
85
    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
86
87
      '#type' => 'submit',
      '#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
99
    $status = $block_type->save();

100
    $edit_link = $this->entity->link($this->t('Edit'));
101
    $logger = $this->logger('block_content');
102
    if ($status == SAVED_UPDATED) {
103
104
      drupal_set_message(t('Custom block type %label has been updated.', ['%label' => $block_type->label()]));
      $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
109
      drupal_set_message(t('Custom block type %label has been added.', ['%label' => $block_type->label()]));
      $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
  }

}