function CommentTypeForm::form

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

Gets the actual form array to be built.

Overrides EntityForm::form

File

core/modules/comment/src/CommentTypeForm.php, line 72

Class

CommentTypeForm
Base form handler for comment type edit forms.

Namespace

Drupal\comment

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $comment_type = $this->entity;
  if ($this->operation === 'edit') {
    $form['#title'] = $this->t('Edit %label comment type', [
      '%label' => $comment_type->label(),
    ]);
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $comment_type->label(),
    '#description' => $this->t('The human-readable name for this comment type, displayed on the <em>Comment types</em> page.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $comment_type->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\comment\\Entity\\CommentType::load',
    ],
    '#description' => $this->t('Unique machine-readable name: lowercase letters, numbers, and underscores only.'),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => !$comment_type->isNew(),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#default_value' => $comment_type->getDescription(),
    '#description' => $this->t('Displays on the <em>Comment types</em> page.'),
    '#title' => $this->t('Description'),
  ];
  if ($comment_type->isNew()) {
    $options = [];
    // Only expose entities that have field UI enabled, only those can
    // get comment fields added in the UI. Also, ensure to include only
    // entities that have integer id.
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type) {
      if ($this->entityTypeSupportsComments($entity_type)) {
        if ($entity_type->get('field_ui_base_route')) {
          $options[$entity_type->id()] = $entity_type->getLabel();
        }
      }
    }
    $form['target_entity_type_id'] = [
      '#type' => 'select',
      '#default_value' => $comment_type->getTargetEntityTypeId(),
      '#title' => $this->t('Target entity type'),
      '#required' => TRUE,
      '#empty_value' => '_none',
      '#options' => $options,
      '#description' => $this->t('The target entity type can not be changed after the comment type has been created.'),
    ];
  }
  else {
    $form['target_entity_type_id_display'] = [
      '#type' => 'item',
      '#markup' => $this->entityTypeManager
        ->getDefinition($comment_type->getTargetEntityTypeId())
        ->getLabel(),
      '#title' => $this->t('Target entity type'),
    ];
  }
  if ($this->moduleHandler
    ->moduleExists('content_translation')) {
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this->t('Language settings'),
      '#group' => 'additional_settings',
    ];
    $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id());
    $form['language']['language_configuration'] = [
      '#type' => 'language_configuration',
      '#entity_information' => [
        'entity_type' => 'comment',
        'bundle' => $comment_type->id(),
      ],
      '#default_value' => $language_configuration,
    ];
    $form['#submit'][] = 'language_configuration_element_submit';
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Save'),
  ];
  return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.