You are here

public function FederatedSearchPageFormBlock::build in Search API Federated Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Block/FederatedSearchPageFormBlock.php \Drupal\search_api_federated_solr\Plugin\Block\FederatedSearchPageFormBlock::build()
  2. 8 src/Plugin/Block/FederatedSearchPageFormBlock.php \Drupal\search_api_federated_solr\Plugin\Block\FederatedSearchPageFormBlock::build()
  3. 4.x src/Plugin/Block/FederatedSearchPageFormBlock.php \Drupal\search_api_federated_solr\Plugin\Block\FederatedSearchPageFormBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/FederatedSearchPageFormBlock.php, line 27

Class

FederatedSearchPageFormBlock
Provides a "Federated Search Page Form" block.

Namespace

Drupal\search_api_federated_solr\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $app_config = \Drupal::config('search_api_federated_solr.search_app.settings');
  $build = [
    '#theme' => 'search_api_federated_solr_block',
    '#search_form' => \Drupal::formBuilder()
      ->getForm('Drupal\\search_api_federated_solr\\Form\\FederatedSearchPageBlockForm'),
  ];

  // If autocomplete is enabled for this block, attach the js library.
  if (array_key_exists('autocomplete', $config) && array_key_exists('isEnabled', $config['autocomplete']) && $config['autocomplete']['isEnabled'] === 1) {

    // Attach autocomplete JS library.
    $build['#attached']['library'][] = 'search_api_federated_solr/search_form_autocomplete';

    // Cache autocomplete config.
    $autocomplete = $config['autocomplete'];
    $proxy_is_disabled = array_key_exists('proxyIsDisabled', $autocomplete) ? $autocomplete['proxyIsDisabled'] : 0;
    $direct_url = array_key_exists('directUrl', $autocomplete) ? $autocomplete['directUrl'] : '';

    // Determine the url that should be used for autocomplete.
    $autocomplete['url'] = Helpers::getEndpointUrl($proxy_is_disabled, $direct_url, '?q=[val]&wt=json');

    // Set the connstraints for allowed sites.
    if ($allowed_sites = $app_config
      ->get('facet.site_name.allowed_sites')) {
      $list = [];
      $sites = array_keys(array_filter($allowed_sites));
      foreach ($sites as $site) {
        $list[] = '"' . $site . '"';
      }
      $autocomplete['sm_site_name'] = '(' . implode(' OR ', $list) . ')';
    }

    // Write the block autocomplete config to Drupal settings.
    $build['#attached']['drupalSettings']['searchApiFederatedSolr'] = [
      'block' => [
        'autocomplete' => $autocomplete,
      ],
    ];

    // Add the js trigger class to the block.
    $build['#attributes']['class'][] = 'js-search-api-federated-solr-block-form-autocomplete';
  }
  return $build;
}