View source
<?php
namespace Drupal\search_autocomplete\Form;
use Drupal;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class AutocompletionConfigurationEditForm extends AutocompletionConfigurationFormBase {
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Update');
return $actions;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['search_autocomplete_how'] = [
'#type' => 'details',
'#title' => $this
->t('HOW - How to display Search Autocomplete suggestions?'),
'#collapsible' => TRUE,
'#open' => FALSE,
];
$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,
];
$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,
];
$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(),
];
$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(),
];
$form['search_autocomplete_how']['moreResultsSuggestion'] = [
'#type' => 'details',
'#title' => $this
->t('Custom behaviour when some suggestions are available'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$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,
];
$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,
];
$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,
];
$form['search_autocomplete_how']['noResultSuggestion'] = [
'#type' => 'details',
'#title' => $this
->t('Custom behaviour when no suggestions are found'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$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,
];
$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,
];
$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,
];
$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,
];
$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' => '/#?',
],
];
$themes = [];
$files = \Drupal::service('file_system')
->scanDirectory(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.'),
];
$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 $form;
}
public function validateUriElement($element, FormStateInterface $form_state) {
$source = $form_state
->getValue('source');
$entity_ids = NULL;
$input_source = explode('::', $source);
if (count($input_source) == 2) {
$entity_ids = \Drupal::entityTypeManager()
->getStorage('view')
->getQuery()
->condition('status', TRUE)
->condition('id', $input_source[0])
->condition("display.*.id", $input_source[1])
->execute();
}
if (!empty($entity_ids)) {
return;
}
elseif (UrlHelper::isExternal($source)) {
return;
}
else {
if (!Drupal::service('path.validator')
->isValid($source)) {
$form_state
->setErrorByName('source', $this
->t('The input source is not valid. Please enter a Drupal valid path, a View display (using completion) or a valid external URL.'));
}
}
}
public function viewAutocomplete(Request $request) {
$matches = [];
$displays = Views::getApplicableViews('autocompletion_callback_display');
$options = [];
foreach ($displays as $data) {
list($view_id, $display_id) = $data;
$view = Views::getView($view_id);
$display = $view
->getDisplay();
$suggestion_value = $view->storage
->get('id') . '::' . $display_id;
$suggestion_label = $view->storage
->get('label') . '::' . $display->display['display_title'];
if (stristr($suggestion_label, $request->query
->get('q')) !== FALSE) {
$matches[] = [
'value' => $suggestion_value,
'label' => $suggestion_label,
];
}
}
return new JsonResponse($matches);
}
}