You are here

public function AutocompletionConfigurationEditForm::buildForm in Search Autocomplete 8

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

Overrides Drupal\Core\Entity\EntityFormController::form() and Drupal\search_autocomplete\Form\AutocompletionConfigurationFormBase::form()

Builds the entity add 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 AutocompletionConfigurationFormBase::buildForm

File

src/Form/AutocompletionConfigurationEditForm.php, line 58

Class

AutocompletionConfigurationEditForm
Class AutocompletionConfigurationEditForm

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);

  // ------------------------------------------------------------------.
  // HOW - How to display Search Autocomplete suggestions.
  $form['search_autocomplete_how'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('HOW - How to display Search Autocomplete suggestions?'),
    '#collapsible' => TRUE,
    '#open' => FALSE,
  ];

  // Minimum characters to set autocompletion on.
  $form['search_autocomplete_how']['minChar'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Minimum keyword size that uncouple autocomplete search'),
    '#description' => $this
      ->t('Please enter the minimum number of character a user must input before autocompletion starts.'),
    '#default_value' => $this->entity
      ->getMinChar(),
    '#maxlength' => 2,
    '#required' => TRUE,
  ];

  // Number of suggestions to display.
  $form['search_autocomplete_how']['maxSuggestions'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Limit of the autocomplete search result'),
    '#description' => $this
      ->t('Please enter the maximum number of suggestion to display.'),
    '#default_value' => $this->entity
      ->getMaxSuggestions(),
    '#maxlength' => 2,
    '#required' => TRUE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['autoSubmit'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Auto Submit'),
    '#description' => $this
      ->t('If enabled, the form will be submitted automatically as soon as your user choose a suggestion in the popup list.'),
    '#default_value' => $this->entity
      ->getAutoSubmit(),
  ];

  // Check if form should be auto redirected.
  $form['search_autocomplete_how']['autoRedirect'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Auto Redirect'),
    '#description' => $this
      ->t('If enabled, the user will be directly routed to the suggestion he chooses instead of performing form validation process. Only works if "link" attribute is existing and if "Auto Submit" is enabled.'),
    '#default_value' => $this->entity
      ->getAutoRedirect(),
  ];

  // ###
  // "View all results" custom configurations.
  $form['search_autocomplete_how']['moreResultsSuggestion'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom behaviour when some suggestions are available'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['moreResultsSuggestion']['moreResultsLabel'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "view all results" message label'),
    '#description' => $this
      ->t('This message is going to be displayed at the end of suggestion list when suggestions are found.') . $this
      ->t('Leave empty to disable this functionality.') . '<br/>' . $this
      ->t('You can use HTML tags as well as the token [search-phrase] to replace user input or [search-count] to replace the number of matching results.'),
    '#default_value' => $this
      ->t($this->entity
      ->getMoreResultsLabel()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['moreResultsSuggestion']['moreResultsValue'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "view all results" message value'),
    '#description' => $this
      ->t('If a label is filled above, this is the value that will be picked when the message is selected.') . $this
      ->t('Leave empty if you don\'t want the message to be selectable.') . '<br/>' . $this
      ->t('You can use the token [search-phrase] to replace user input.'),
    '#default_value' => $this
      ->t($this->entity
      ->getMoreResultsValue()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['moreResultsSuggestion']['moreResultsLink'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "view all results" URL redirection'),
    '#description' => $this
      ->t('If "Auto redirect" is checked and an URL is given for this configuration, the user will be redirected to this URL when the message is clicked. Leave empty if you rather like a standard Drupal behavior.'),
    '#default_value' => $this
      ->t($this->entity
      ->getMoreResultsLink()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // ###
  // "No resuls" custom configurations.
  $form['search_autocomplete_how']['noResultSuggestion'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom behaviour when no suggestions are found'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['noResultSuggestion']['noResultLabel'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "no result" message label'),
    '#description' => $this
      ->t('This message is going to be displayed when no suggestions can be found.') . $this
      ->t('Leave empty to disable this functionality.') . '<br/>' . $this
      ->t('You can use HTML tags as well as the token [search-phrase] to replace user input.'),
    '#default_value' => $this
      ->t($this->entity
      ->getNoResultLabel()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['noResultSuggestion']['noResultValue'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "no result" message value'),
    '#description' => $this
      ->t('If a label is filled above, this is the value that will be picked when the message is selected.') . $this
      ->t('Leave empty if you don\'t want the message to be selectable.') . '<br/>' . $this
      ->t('You can use the token [search-phrase] to replace user input.'),
    '#default_value' => $this
      ->t($this->entity
      ->getNoResultValue()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // Check if form should be auto submitted.
  $form['search_autocomplete_how']['noResultSuggestion']['noResultLink'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom "no result" URL redirection'),
    '#description' => $this
      ->t('If "Auto redirect" is checked and an URL is given for this configuration, the user will be redirected to this URL when the message is clicked. Leave empty if you rather like a standard Drupal behavior.'),
    '#default_value' => $this
      ->t($this->entity
      ->getNoResultLink()),
    '#maxlength' => 255,
    '#required' => FALSE,
  ];

  // ------------------------------------------------------------------.
  // WHAT - What to display in Search Autocomplete suggestions.
  $form['search_autocomplete_what'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('WHAT - What to display in Search Autocomplete suggestions?'),
    '#description' => $this
      ->t('Choose which data should be added to autocompletion suggestions.'),
    '#collapsible' => TRUE,
    '#open' => TRUE,
  ];
  $source = $this->entity
    ->getSource();
  $form['search_autocomplete_what']['source'] = [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'search_autocomplete.view_autocomplete',
    '#description' => $this
      ->t('Enter the URL of your callback for suggestions.') . '<br/>' . $this
      ->t('You can enter an internal path such as %node-xx or an external URL such as %url. You can also use autocompletion to target a specific View display.', [
      '%node-xx' => '/node/xx',
      '%url' => 'http://example.com',
    ]),
    '#element_validate' => [
      [
        $this,
        'validateUriElement',
      ],
    ],
    '#default_value' => $this->entity
      ->getSource(),
    '#size' => 80,
    '#attributes' => [
      'data-autocomplete-first-character-blacklist' => '/#?',
    ],
  ];

  // Template to use.
  $themes = [];
  $files = file_scan_directory(drupal_get_path('module', 'search_autocomplete') . '/css/themes', '/.*\\.css\\z/', [
    'recurse' => FALSE,
  ]);
  foreach ($files as $file) {
    $themes[$file->filename] = $file->name;
  }
  $form['search_autocomplete_what']['theme'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select a theme for your suggestions'),
    '#options' => $themes,
    '#default_value' => $this->entity
      ->getTheme(),
    '#description' => $this
      ->t('Choose the theme to use for autocompletion dropdown popup. Read <a href="http://drupal-addict.com/nos-projets/search-autocomplete">documentation</a> to learn how to make your own.'),
  ];

  // ------------------------------------------------------------------.
  // ADVANCED - Advanced options.
  $form['search_autocomplete_advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('ADVANCED - Advanced options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['search_autocomplete_advanced']['selector'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('ID selector for this form'),
    '#description' => $this
      ->t('Please change only if you know what you do, read <a href="http://drupal-addict.com/nos-projets/search-autocomplete">documentation</a> first.'),
    '#default_value' => $this->entity
      ->getSelector(),
    '#maxlength' => 255,
    '#size' => 35,
  ];

  // Return the form.
  return $form;
}