You are here

public function AutocompletionConfigurationListBuilder::buildForm in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Controller/AutocompletionConfigurationListBuilder.php \Drupal\search_autocomplete\Controller\AutocompletionConfigurationListBuilder::buildForm()

Implements \Drupal\Core\Form\FormInterface::buildForm().

Form constructor for the main block administration form.

Overrides FormInterface::buildForm

File

src/Controller/AutocompletionConfigurationListBuilder.php, line 46

Class

AutocompletionConfigurationListBuilder
Provides a listing of autocompletion_configuration entities.

Namespace

Drupal\search_autocomplete\Controller

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $settings = Drupal::config('search_autocomplete.settings');

  // Define the table of configurations.
  $form['configs'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Autocompletion Configuration Name'),
      $this
        ->t('Enabled'),
      $this
        ->t('Selector'),
      $this
        ->t('Operations'),
    ],
  ];

  // Retrieve existing configurations.
  $entity_ids = $this
    ->getEntityIds();
  $entities = $this->storage
    ->loadMultiple($entity_ids);

  // Build blocks first for each region.
  $configs = [];
  foreach ($entities as $entity_id => $entity) {
    $editable = $entity
      ->getEditable() ? 'editable' : '';
    $deletable = $entity
      ->getEditable() ? 'deletable' : '';
    $form['configs'][$entity_id]['#attributes'] = [
      'id' => [
        $entity_id,
      ],
      'class' => [
        $editable,
        $deletable,
      ],
    ];
    $form['configs'][$entity_id]['label'] = [
      '#markup' => new HtmlEscapedText($entity
        ->label()),
    ];
    $form['configs'][$entity_id]['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => $entity
        ->getStatus(),
    ];
    $form['configs'][$entity_id]['selector'] = [
      '#markup' => new HtmlEscapedText($entity
        ->getSelector()),
    ];
    $form['configs'][$entity_id]['operations'] = $this
      ->buildOperations($entity);
  }

  // Use admin helper tool option settings.
  $form['admin_helper'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use autocompletion helper tool for Search Autocomplete administrators.'),
    '#description' => $this
      ->t('If enabled, user with "administer Search Autocomplete" permission will be able to use admin helper tool on input fields (recommended).'),
    '#default_value' => $settings
      ->get('admin_helper'),
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save changes'),
    '#button_type' => 'primary',
  ];
  return $form;
}