3 namespace Drupal\block_content;
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\language\Entity\ContentLanguageSettings;
11 * The block content type entity form.
15 class BlockContentTypeForm extends BundleEntityFormBase {
20 public function form(array $form, FormStateInterface $form_state) {
21 $form = parent::form($form, $form_state);
23 /* @var \Drupal\block_content\BlockContentTypeInterface $block_type */
24 $block_type = $this->entity;
26 if ($this->operation == 'add') {
27 $form['#title'] = $this->t('Add custom block type');
30 $form['#title'] = $this->t('Edit %label custom block type', ['%label' => $block_type->label()]);
34 '#type' => 'textfield',
35 '#title' => t('Label'),
37 '#default_value' => $block_type->label(),
38 '#description' => t("Provide a label for this block type to help identify it in the administration pages."),
42 '#type' => 'machine_name',
43 '#default_value' => $block_type->id(),
45 'exists' => '\Drupal\block_content\Entity\BlockContentType::load',
47 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
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'),
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.'),
64 if ($this->moduleHandler->moduleExists('language')) {
67 '#title' => t('Language settings'),
68 '#group' => 'additional_settings',
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(),
78 '#default_value' => $language_configuration,
81 $form['#submit'][] = 'language_configuration_element_submit';
84 $form['actions'] = ['#type' => 'actions'];
85 $form['actions']['submit'] = [
87 '#value' => t('Save'),
90 return $this->protectBundleIdElement($form);
96 public function save(array $form, FormStateInterface $form_state) {
97 $block_type = $this->entity;
98 $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 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]);
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]);
112 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));