You are here

function search_autocomplete_page_attachments in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x search_autocomplete.module \search_autocomplete_page_attachments()

Implements hook_page_attachments().

Adds the settings from user defined autocomplete_configurations to the page. Also loads the necessary JS library if needed.

File

./search_autocomplete.module, line 220
Provides autocompletion in any field from GUI.

Code

function search_autocomplete_page_attachments(array &$attachments) {

  // Check user permissions.
  if (!Drupal::currentUser()
    ->hasPermission('use search autocomplete')) {
    return;
  }

  // Load module settings.
  $module_settings = Drupal::config('search_autocomplete.settings');

  // Check if user have authorization to use admin tools and if enabled.
  if (Drupal::currentUser()
    ->hasPermission('administer Search Autocomplete') && $module_settings
    ->get('admin_helper')) {
    $attachments['#attached']['library'][] = 'search_autocomplete/search_autocomplete.admin.helper';
  }

  // Load all configurations with selectors
  $autocomplete_configuration_entities = Drupal::entityQuery('autocompletion_configuration')
    ->condition('status', TRUE)
    ->condition('selector', '', '<>')
    ->execute();

  // End-up here if no config to set.
  if (empty($autocomplete_configuration_entities)) {
    return;
  }

  // Add the settings to be pass to JS:
  foreach ($autocomplete_configuration_entities as $config_id) {
    attach_configuration_to_element($attachments, $config_id);
  }
  $attachments['#attached']['library'][] = 'core/drupal.autocomplete';
}