You are here

public function AutocompletionConfigurationFormBase::buildForm in Search Autocomplete 2.x

Same name and namespace in other branches
  1. 8 src/Form/AutocompletionConfigurationFormBase.php \Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::buildForm()

Overrides Drupal\Core\Entity\EntityFormController::form().

Builds the entity add/edit form.

Parameters

array $form: An associative array containing the structure of the form.

array $form_state: An associative array containing the current state of the form.

Return value

array An associative array containing the autocompletion_configuration add/edit form.

Overrides EntityForm::buildForm

2 calls to AutocompletionConfigurationFormBase::buildForm()
AutocompletionConfigurationAddForm::buildForm in src/Form/AutocompletionConfigurationAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::form() and Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::form()
AutocompletionConfigurationEditForm::buildForm in src/Form/AutocompletionConfigurationEditForm.php
Overrides Drupal\Core\Entity\EntityFormController::form() and Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::form()
2 methods override AutocompletionConfigurationFormBase::buildForm()
AutocompletionConfigurationAddForm::buildForm in src/Form/AutocompletionConfigurationAddForm.php
Overrides Drupal\Core\Entity\EntityFormController::form() and Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::form()
AutocompletionConfigurationEditForm::buildForm in src/Form/AutocompletionConfigurationEditForm.php
Overrides Drupal\Core\Entity\EntityFormController::form() and Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::form()

File

src/Form/AutocompletionConfigurationFormBase.php, line 79

Class

AutocompletionConfigurationFormBase
Class AutocompletionConfigurationFormBase.

Namespace

Drupal\search_autocomplete\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get anything we need from the base class.
  $form = parent::buildForm($form, $form_state);

  // Drupal provides the entity to us as a class variable. If this is an
  // existing entity, it will be populated with existing values as class
  // variables. If this is a new entity, it will be a new object with the
  // class of our entity. Drupal knows which class to call from the
  // annotation on our AutocompletionConfiguration class.
  $autocompletion_configuration = $this->entity;

  // Get default label from URL if available.
  $label = '';
  if (isset($_REQUEST['label'])) {
    $label = urldecode($_REQUEST['label']);
  }

  // Label.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Human readable name'),
    '#maxlength' => 255,
    '#description' => 'Please enter a label for this autocompletion configuration.',
    '#default_value' => $label ? $label : $autocompletion_configuration
      ->label(),
    '#required' => TRUE,
  ];

  // ID.
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $autocompletion_configuration
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this->entityStorage,
        'load',
      ],
      'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
      'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
    ],
    '#disabled' => !$autocompletion_configuration
      ->isNew(),
  ];

  // Return the form.
  return $form;
}