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;
13 * Form handler for node type forms.
17 class NodeTypeForm extends BundleEntityFormBase {
22 * @var \Drupal\Core\Entity\EntityManagerInterface
24 protected $entityManager;
27 * Constructs the NodeTypeForm object.
29 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
32 public function __construct(EntityManagerInterface $entity_manager) {
33 $this->entityManager = $entity_manager;
39 public static function create(ContainerInterface $container) {
41 $container->get('entity.manager')
48 public function form(array $form, FormStateInterface $form_state) {
49 $form = parent::form($form, $form_state);
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()]);
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()]);
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.'),
78 '#type' => 'machine_name',
79 '#default_value' => $type->id(),
80 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
81 '#disabled' => $type->isLocked(),
83 'exists' => ['Drupal\node\Entity\NodeType', 'load'],
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'),
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.'),
98 $form['additional_settings'] = [
99 '#type' => 'vertical_tabs',
101 'library' => ['node/drupal.content_types'],
105 $form['submission'] = [
106 '#type' => 'details',
107 '#title' => t('Submission form settings'),
108 '#group' => 'additional_settings',
111 $form['submission']['title_label'] = [
112 '#title' => t('Title field label'),
113 '#type' => 'textfield',
114 '#default_value' => $fields['title']->getLabel(),
117 $form['submission']['preview_mode'] = [
119 '#title' => t('Preview before submitting'),
120 '#default_value' => $type->getPreviewMode(),
122 DRUPAL_DISABLED => t('Disabled'),
123 DRUPAL_OPTIONAL => t('Optional'),
124 DRUPAL_REQUIRED => t('Required'),
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.'),
133 $form['workflow'] = [
134 '#type' => 'details',
135 '#title' => t('Publishing options'),
136 '#group' => 'additional_settings',
138 $workflow_options = [
139 'status' => $node->status->value,
140 'promote' => $node->promote->value,
141 'sticky' => $node->sticky->value,
142 'revision' => $type->isNewRevision(),
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,
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'),
157 '#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.'),
159 if ($this->moduleHandler->moduleExists('language')) {
160 $form['language'] = [
161 '#type' => 'details',
162 '#title' => t('Language settings'),
163 '#group' => 'additional_settings',
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(),
173 '#default_value' => $language_configuration,
177 '#type' => 'details',
178 '#title' => t('Display settings'),
179 '#group' => 'additional_settings',
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.'),
188 return $this->protectBundleIdElement($form);
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');
204 public function validateForm(array &$form, FormStateInterface $form_state) {
205 parent::validateForm($form, $form_state);
207 $id = trim($form_state->getValue('type'));
208 // '0' is invalid, since elsewhere we check it using empty().
210 $form_state->setErrorByName('type', $this->t("Invalid machine-readable name. Enter a name other than %invalid.", ['%invalid' => $id]));
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()));
223 $status = $type->save();
225 $t_args = ['%name' => $type->label()];
227 if ($status == SAVED_UPDATED) {
228 drupal_set_message(t('The content type %name has been updated.', $t_args));
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);
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();
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();
255 $this->entityManager->clearCachedFieldDefinitions();
256 $form_state->setRedirectUrl($type->urlInfo('collection'));