You are here

function attach_configuration_to_element in Search Autocomplete 8

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

This helper method will analyse an autocomplete_configuration to build the element properties and add necessary libraries, configurations and data necessary for autocompletion to happen.

Parameters

array $element: An element of a form.

string $config_id: The ID of the autocomplete_configuration to use.

Return value

array The element once filled with data.

2 calls to attach_configuration_to_element()
process_search_autocomplete in ./search_autocomplete.module
Adds autocomplete functionality to elements.
search_autocomplete_page_attachments in ./search_autocomplete.module
Implements hook_page_attachments().

File

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

Code

function attach_configuration_to_element(&$element, $config_id) {

  // Load the configuration.
  $config = Drupal::entityTypeManager()
    ->getStorage('autocompletion_configuration')
    ->load($config_id);

  // No need to continue if the $config is empty.
  if ($config == NULL || !$config
    ->getStatus()) {
    return;
  }
  $source = $config
    ->getSource();
  $exposed_filters = [];

  // Build the callback URL.
  $input_source = explode('::', $source);

  // If from View, build view URL.
  if (count($input_source) == 2) {

    // Load the view.
    $view = Views::getView($input_source[0]);

    // Load the required display.
    if ($view != NULL && $view
      ->setDisplay($input_source[1])) {

      // Retrieve the display URL.
      $display = $view
        ->getDisplay();
      $source = '/' . $display
        ->getPath();

      // Build filters
      $filters = $view
        ->getHandlers('filter');
      foreach ($filters as $filter) {
        if (isset($filter['exposed']) && $filter['exposed'] && isset($filter['expose'])) {
          $exposed_filters[] = $filter['expose']['identifier'];
        }
      }
    }
  }

  // In case of internal URL, convert it to absolute.
  if (!UrlHelper::isExternal($source)) {
    $prefix = 'internal:';
    if (strpos($source, '/') !== 0) {
      $prefix .= '/';
    }
    $source = Url::fromUri($prefix . $source, [
      'absolute' => FALSE,
    ])
      ->toString();
  }
  $settings = [
    'source' => $source,
    'selector' => $config
      ->getSelector(),
    'minChars' => $config
      ->getMinChar(),
    'maxSuggestions' => $config
      ->getMaxSuggestions(),
    'autoSubmit' => $config
      ->getAutoSubmit(),
    'autoRedirect' => $config
      ->getAutoRedirect(),
    'theme' => basename($config
      ->getTheme(), '.css'),
    // theme without extension
    'filters' => $exposed_filters,
    'noResult' => [
      'group' => [
        'group_id' => 'no_results',
      ],
      'label' => $config
        ->getNoResultLabel(),
      'value' => $config
        ->getNoResultValue(),
      'link' => $config
        ->getNoResultLink(),
    ],
    'moreResults' => [
      'group' => [
        'group_id' => 'more_results',
      ],
      'label' => $config
        ->getMoreResultsLabel(),
      'value' => $config
        ->getMoreResultsValue(),
      'link' => $config
        ->getMoreResultsLink(),
    ],
  ];
  $element['#attached']['drupalSettings']['search_autocomplete'][$config
    ->id()] = $settings;

  // Add the theme library if available
  if (Drupal::service('library.discovery')
    ->getLibraryByName('search_autocomplete', 'theme.' . $config
    ->getTheme())) {
    $element['#attached']['library'][] = 'search_autocomplete/theme.' . $config
      ->getTheme();
  }
  return $element;
}