Same name and namespace in other branches
  1. 8.9.x core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::form()
  2. 9 core/modules/node/src/NodeTypeForm.php \Drupal\node\NodeTypeForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/node/src/NodeTypeForm.php, line 48

Class

NodeTypeForm
Form handler for node type forms.

Namespace

Drupal\node

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $type = $this->entity;
  if ($this->operation == 'add') {
    $form['#title'] = $this
      ->t('Add content type');
    $fields = $this->entityFieldManager
      ->getBaseFieldDefinitions('node');

    // Create a node with a fake bundle using the type's UUID so that we can
    // get the default values for workflow settings.
    // @todo Make it possible to get default values without an entity.
    //   https://www.drupal.org/node/2318187
    $node = $this->entityTypeManager
      ->getStorage('node')
      ->create([
      'type' => $type
        ->uuid(),
    ]);
  }
  else {
    $form['#title'] = $this
      ->t('Edit %label content type', [
      '%label' => $type
        ->label(),
    ]);
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('node', $type
      ->id());

    // Create a node to get the current values for workflow settings fields.
    $node = $this->entityTypeManager
      ->getStorage('node')
      ->create([
      'type' => $type
        ->id(),
    ]);
  }
  $form['name'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $type
      ->label(),
    '#description' => $this
      ->t('The human-readable name for this content type, displayed on the <em>Content types</em> page.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['type'] = [
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => $type
      ->isLocked(),
    '#machine_name' => [
      'exists' => [
        'Drupal\\node\\Entity\\NodeType',
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
    '#description' => $this
      ->t('Unique machine-readable name: lowercase letters, numbers, and underscores only.', [
      '%node-add' => $this
        ->t('Add content'),
    ]),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type
      ->getDescription(),
    '#description' => $this
      ->t('Displays on the <em>Content types</em> page.'),
  ];
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
    '#attached' => [
      'library' => [
        'node/drupal.content_types',
      ],
    ],
  ];
  $form['submission'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Submission form settings'),
    '#group' => 'additional_settings',
    '#open' => TRUE,
  ];
  $form['submission']['title_label'] = [
    '#title' => $this
      ->t('Title field label'),
    '#type' => 'textfield',
    '#default_value' => $fields['title']
      ->getLabel(),
    '#required' => TRUE,
  ];
  $form['submission']['preview_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Preview before submitting'),
    '#default_value' => $type
      ->getPreviewMode(),
    '#options' => [
      DRUPAL_DISABLED => $this
        ->t('Disabled'),
      DRUPAL_OPTIONAL => $this
        ->t('Optional'),
      DRUPAL_REQUIRED => $this
        ->t('Required'),
    ],
  ];
  $form['submission']['help'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Explanation or submission guidelines'),
    '#default_value' => $type
      ->getHelp(),
    '#description' => $this
      ->t('This text will be displayed at the top of the page when creating or editing content of this type.'),
  ];
  $form['workflow'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Publishing options'),
    '#group' => 'additional_settings',
  ];
  $workflow_options = [
    'status' => $node->status->value,
    'promote' => $node->promote->value,
    'sticky' => $node->sticky->value,
    'revision' => $type
      ->shouldCreateNewRevision(),
  ];

  // Prepare workflow options to be used for 'checkboxes' form element.
  $keys = array_keys(array_filter($workflow_options));
  $workflow_options = array_combine($keys, $keys);
  $form['workflow']['options'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Default options'),
    '#default_value' => $workflow_options,
    '#options' => [
      'status' => $this
        ->t('Published'),
      'promote' => $this
        ->t('Promoted to front page'),
      'sticky' => $this
        ->t('Sticky at top of lists'),
      'revision' => $this
        ->t('Create new revision'),
    ],
    '#description' => $this
      ->t('Users with sufficient access rights will be able to override these options.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('language')) {
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language settings'),
      '#group' => 'additional_settings',
    ];
    $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', $type
      ->id());
    $form['language']['language_configuration'] = [
      '#type' => 'language_configuration',
      '#entity_information' => [
        'entity_type' => 'node',
        'bundle' => $type
          ->id(),
      ],
      '#default_value' => $language_configuration,
    ];
  }
  $form['display'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Display settings'),
    '#group' => 'additional_settings',
  ];
  $form['display']['display_submitted'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display author and date information'),
    '#default_value' => $type
      ->displaySubmitted(),
    '#description' => $this
      ->t('Author username and publish date will be displayed.'),
  ];
  return $this
    ->protectBundleIdElement($form);
}