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