You are here

public function SuggestionSearchForm::buildForm in Autocomplete Search Suggestions 8

Same name and namespace in other branches
  1. 8.2 src/Form/SuggestionSearchForm.php \Drupal\suggestion\Form\SuggestionSearchForm::buildForm()
  2. 3.0.x src/Form/SuggestionSearchForm.php \Drupal\suggestion\Form\SuggestionSearchForm::buildForm()

The suggestion search form.

Parameters

array $form: A drupal form array.

Drupal\Core\Form\FormStateInterface $form_state: A Drupal form state object.

string $ngram: The search string.

Return value

array A Drupal form array.

Overrides FormInterface::buildForm

File

src/Form/SuggestionSearchForm.php, line 50

Class

SuggestionSearchForm
Ngram search form.

Namespace

Drupal\suggestion\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $ngram = '') {
  $ngram = trim($ngram);
  $opts = [
    'query' => $this->redirect
      ->getAsArray(),
  ];
  $rows = [];
  $rpp = SuggestionHelper::getConfig('rpp');
  $header = [
    $this
      ->t('N-Gram'),
    $this
      ->t('Source'),
    $this
      ->t('Atoms'),
    $this
      ->t('Quantity'),
    $this
      ->t('Density'),
    $this
      ->t('Edit'),
  ];
  if ($ngram) {
    $pattern = '%' . db_like($ngram) . '%';
    $page = pager_default_initialize(SuggestionStorage::getCount($pattern), $rpp);
    $suggestions = SuggestionStorage::search($pattern, $page * $rpp, $rpp);
  }
  else {
    $page = pager_default_initialize(SuggestionStorage::getCount(), $rpp);
    $suggestions = SuggestionStorage::getAllSuggestions($page * $rpp, $rpp);
  }
  foreach ($suggestions as $obj) {
    $rows[$obj->ngram] = [
      $obj->ngram,
      $obj->src,
      $obj->atoms,
      $obj->qty,
      $obj->density,
      Link::fromTextAndUrl($this
        ->t('Edit'), Url::fromUri("internal:/admin/config/suggestion/edit/{$obj->ngram}", $opts)),
    ];
  }
  $form['ngram'] = [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'suggestion.autocomplete',
    '#default_value' => $ngram,
    '#weight' => 10,
  ];
  $form['search'] = [
    '#type' => 'submit',
    '#name' => 'search',
    '#value' => $this
      ->t('Search'),
    '#submit' => [
      '::submitForm',
    ],
    '#weight' => 20,
  ];
  $form['list'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => $this
      ->t('Nothing found.'),
    '#weight' => 60,
  ];
  if (count($rows)) {
    $form['src'] = [
      '#title' => $this
        ->t('Source'),
      '#type' => 'select',
      '#options' => SuggestionStorage::getSrcOptions(),
      '#multiple' => TRUE,
      '#weight' => 30,
    ];
    $form['update'] = [
      '#type' => 'submit',
      '#name' => 'update',
      '#value' => $this
        ->t('Update'),
      '#submit' => [
        '::submitUpdateForm',
        '::submitForm',
      ],
      '#validate' => [
        '::validateUpdateForm',
      ],
      '#weight' => 40,
    ];
    $form['pager_head'] = [
      '#type' => 'pager',
      '#weight' => 50,
    ];
    $form['pager_foot'] = [
      '#type' => 'pager',
      '#weight' => 70,
    ];
  }
  return $form;
}