Version 1
[yaffs-website] / web / core / modules / node / src / NodeTypeForm.php
1 <?php
2
3 namespace Drupal\node;
4
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\language\Entity\ContentLanguageSettings;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Form handler for node type forms.
14  */
15 class NodeTypeForm extends BundleEntityFormBase {
16
17   /**
18    * The entity manager.
19    *
20    * @var \Drupal\Core\Entity\EntityManagerInterface
21    */
22   protected $entityManager;
23
24   /**
25    * Constructs the NodeTypeForm object.
26    *
27    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
28    *   The entity manager.
29    */
30   public function __construct(EntityManagerInterface $entity_manager) {
31     $this->entityManager = $entity_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('entity.manager')
40     );
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function form(array $form, FormStateInterface $form_state) {
47     $form = parent::form($form, $form_state);
48
49     $type = $this->entity;
50     if ($this->operation == 'add') {
51       $form['#title'] = $this->t('Add content type');
52       $fields = $this->entityManager->getBaseFieldDefinitions('node');
53       // Create a node with a fake bundle using the type's UUID so that we can
54       // get the default values for workflow settings.
55       // @todo Make it possible to get default values without an entity.
56       //   https://www.drupal.org/node/2318187
57       $node = $this->entityManager->getStorage('node')->create(['type' => $type->uuid()]);
58     }
59     else {
60       $form['#title'] = $this->t('Edit %label content type', ['%label' => $type->label()]);
61       $fields = $this->entityManager->getFieldDefinitions('node', $type->id());
62       // Create a node to get the current values for workflow settings fields.
63       $node = $this->entityManager->getStorage('node')->create(['type' => $type->id()]);
64     }
65
66     $form['name'] = [
67       '#title' => t('Name'),
68       '#type' => 'textfield',
69       '#default_value' => $type->label(),
70       '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the <em>Add content</em> page. This name must be unique.'),
71       '#required' => TRUE,
72       '#size' => 30,
73     ];
74
75     $form['type'] = [
76       '#type' => 'machine_name',
77       '#default_value' => $type->id(),
78       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
79       '#disabled' => $type->isLocked(),
80       '#machine_name' => [
81         'exists' => ['Drupal\node\Entity\NodeType', 'load'],
82         'source' => ['name'],
83       ],
84       '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %node-add page, in which underscores will be converted into hyphens.', [
85         '%node-add' => t('Add content'),
86       ]),
87     ];
88
89     $form['description'] = [
90       '#title' => t('Description'),
91       '#type' => 'textarea',
92       '#default_value' => $type->getDescription(),
93       '#description' => t('This text will be displayed on the <em>Add new content</em> page.'),
94     ];
95
96     $form['additional_settings'] = [
97       '#type' => 'vertical_tabs',
98       '#attached' => [
99         'library' => ['node/drupal.content_types'],
100       ],
101     ];
102
103     $form['submission'] = [
104       '#type' => 'details',
105       '#title' => t('Submission form settings'),
106       '#group' => 'additional_settings',
107       '#open' => TRUE,
108     ];
109     $form['submission']['title_label'] = [
110       '#title' => t('Title field label'),
111       '#type' => 'textfield',
112       '#default_value' => $fields['title']->getLabel(),
113       '#required' => TRUE,
114     ];
115     $form['submission']['preview_mode'] = [
116       '#type' => 'radios',
117       '#title' => t('Preview before submitting'),
118       '#default_value' => $type->getPreviewMode(),
119       '#options' => [
120         DRUPAL_DISABLED => t('Disabled'),
121         DRUPAL_OPTIONAL => t('Optional'),
122         DRUPAL_REQUIRED => t('Required'),
123       ],
124     ];
125     $form['submission']['help']  = [
126       '#type' => 'textarea',
127       '#title' => t('Explanation or submission guidelines'),
128       '#default_value' => $type->getHelp(),
129       '#description' => t('This text will be displayed at the top of the page when creating or editing content of this type.'),
130     ];
131     $form['workflow'] = [
132       '#type' => 'details',
133       '#title' => t('Publishing options'),
134       '#group' => 'additional_settings',
135     ];
136     $workflow_options = [
137       'status' => $node->status->value,
138       'promote' => $node->promote->value,
139       'sticky' => $node->sticky->value,
140       'revision' => $type->isNewRevision(),
141     ];
142     // Prepare workflow options to be used for 'checkboxes' form element.
143     $keys = array_keys(array_filter($workflow_options));
144     $workflow_options = array_combine($keys, $keys);
145     $form['workflow']['options'] = [
146       '#type' => 'checkboxes',
147       '#title' => t('Default options'),
148       '#default_value' => $workflow_options,
149       '#options' => [
150         'status' => t('Published'),
151         'promote' => t('Promoted to front page'),
152         'sticky' => t('Sticky at top of lists'),
153         'revision' => t('Create new revision'),
154       ],
155       '#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.'),
156     ];
157     if ($this->moduleHandler->moduleExists('language')) {
158       $form['language'] = [
159         '#type' => 'details',
160         '#title' => t('Language settings'),
161         '#group' => 'additional_settings',
162       ];
163
164       $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $type->id());
165       $form['language']['language_configuration'] = [
166         '#type' => 'language_configuration',
167         '#entity_information' => [
168           'entity_type' => 'node',
169           'bundle' => $type->id(),
170         ],
171         '#default_value' => $language_configuration,
172       ];
173     }
174     $form['display'] = [
175       '#type' => 'details',
176       '#title' => t('Display settings'),
177       '#group' => 'additional_settings',
178     ];
179     $form['display']['display_submitted'] = [
180       '#type' => 'checkbox',
181       '#title' => t('Display author and date information'),
182       '#default_value' => $type->displaySubmitted(),
183       '#description' => t('Author username and publish date will be displayed.'),
184     ];
185
186     return $this->protectBundleIdElement($form);
187   }
188
189   /**
190    * {@inheritdoc}
191    */
192   protected function actions(array $form, FormStateInterface $form_state) {
193     $actions = parent::actions($form, $form_state);
194     $actions['submit']['#value'] = t('Save content type');
195     $actions['delete']['#value'] = t('Delete content type');
196     return $actions;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function validateForm(array &$form, FormStateInterface $form_state) {
203     parent::validateForm($form, $form_state);
204
205     $id = trim($form_state->getValue('type'));
206     // '0' is invalid, since elsewhere we check it using empty().
207     if ($id == '0') {
208       $form_state->setErrorByName('type', $this->t("Invalid machine-readable name. Enter a name other than %invalid.", ['%invalid' => $id]));
209     }
210   }
211
212   /**
213    * {@inheritdoc}
214    */
215   public function save(array $form, FormStateInterface $form_state) {
216     $type = $this->entity;
217     $type->setNewRevision($form_state->getValue(['options', 'revision']));
218     $type->set('type', trim($type->id()));
219     $type->set('name', trim($type->label()));
220
221     $status = $type->save();
222
223     $t_args = ['%name' => $type->label()];
224
225     if ($status == SAVED_UPDATED) {
226       drupal_set_message(t('The content type %name has been updated.', $t_args));
227     }
228     elseif ($status == SAVED_NEW) {
229       node_add_body_field($type);
230       drupal_set_message(t('The content type %name has been added.', $t_args));
231       $context = array_merge($t_args, ['link' => $type->link($this->t('View'), 'collection')]);
232       $this->logger('node')->notice('Added content type %name.', $context);
233     }
234
235     $fields = $this->entityManager->getFieldDefinitions('node', $type->id());
236     // Update title field definition.
237     $title_field = $fields['title'];
238     $title_label = $form_state->getValue('title_label');
239     if ($title_field->getLabel() != $title_label) {
240       $title_field->getConfig($type->id())->setLabel($title_label)->save();
241     }
242     // Update workflow options.
243     // @todo Make it possible to get default values without an entity.
244     //   https://www.drupal.org/node/2318187
245     $node = $this->entityManager->getStorage('node')->create(['type' => $type->id()]);
246     foreach (['status', 'promote', 'sticky'] as $field_name) {
247       $value = (bool) $form_state->getValue(['options', $field_name]);
248       if ($node->$field_name->value != $value) {
249         $fields[$field_name]->getConfig($type->id())->setDefaultValue($value)->save();
250       }
251     }
252
253     $this->entityManager->clearCachedFieldDefinitions();
254     $form_state->setRedirectUrl($type->urlInfo('collection'));
255   }
256
257 }