search_api_federated_solr.module in Search API Federated Solr 8
Contains hook implementations for the Federated Solr Search API Module.
File
search_api_federated_solr.moduleView source
<?php
/**
* @file
* Contains hook implementations for the Federated Solr Search API Module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\search_api\IndexInterface;
/**
* Implements hook_help().
*/
function search_api_federated_solr_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the search_api_federated_solr module.
case 'help.page.search_api_federated_solr':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module facilitates indexing data from multiple Drupal sites into a single Solr search index. It also provides a ReactJS front end to render search results from that Solr search index.') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<p>' . t('On each site included in the federated search, you will need to:') . '</p>';
$output .= '<ol>';
$output .= '<li>' . t('Install this module') . '</li>';
$output .= '<li>' . t('Configure a Search API server to connect to the shared Solr index') . '</li>';
$output .= '<li>' . t('Configure a Search API index according to the <a href="@recommended_schema">recommended schema</a>', [
'@recommended_schema' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/federated_schema.md',
]) . '</li>';
$output .= '</ol>';
$output .= '<p>' . t('In order to display results from the Solr index:') . '</p>';
$output .= '<ol>';
$output .= '<li>' . t('Configure the application route and settings at <code>@settings</code>', [
'@settings' => '/admin/config/search-api-federated-solr/search-app/settings',
]) . '</li>';
$output .= '<li>' . t('Set permissions for <code>Use Federated Search</code> and <code>Administer Federated Search</code> for the proper roles.') . '</li>';
$output .= '<li>' . t('Optional: <a href="@theme">Theme the ReactJS search app</a>', [
'@theme' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/theme.md',
]) . '</li>';
$output .= '<li>' . t('Optional: Add the federated search page form block to your site theme') . '</li>';
$output .= '</ol>';
$output .= '<h3>' . t('Updating the bundled React application') . '</h3>';
$output .= '<p>' . t('When changes to <a href="@react">federated-search-react</a> are made they\'ll need to be pulled into this module. To do so:', [
'@react' => 'https://github.com/palantirnet/federated-search-react/',
]) . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Update <code>package.json</code> with the new version.') . '</li>';
$output .= '<li>' . t('Run <code>yarn install</code>') . '</li>';
$output .= '<li>' . t('Delete the old versions of <code>js/main.*.js (and .map)</code> and <code>css/main.*.css (and .map)</code>') . '</li>';
$output .= '<li>' . t('Update <code>search_api_federated_solr.libraries.yml</code> to reference the new file.') . '</li>';
$output .= '</ul>';
$output .= '<h3>' . t('More information') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('<a href="@usage">How to use this module</a>', [
'@usage' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/usage.md',
]) . '</li>';
$output .= '<li>' . t('<a href="@federated_schema">How to configure a Search API Index for federated search</a>', [
'@federated_schema' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/federated_schema.md',
]) . '</li>';
$output .= '<li>' . t('<a href="@theme">How to theme the ReactJS search app</a>', [
'@theme' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/theme.md',
]) . '</li>';
$output .= '<li>' . t('<a href="@block">How to add the search form block</a>', [
'@block' => 'https://github.com/palantirnet/search_api_federated_solr/blob/master/docs/block.md',
]) . '</li>';
$output .= '</ul>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function search_api_federated_solr_theme($existing, $type, $theme, $path) {
return [
'search_app' => [
'variables' => [
'federated_search_app_config' => NULL,
],
],
];
}
/**
* Change the way the index's field names are mapped to Solr field names.
*
* @param \Drupal\search_api\IndexInterface $index
* The index whose field mappings are altered.
* @param array $fields
* An associative array containing the index field names mapped to their Solr
* counterparts. The special fields 'search_api_id' and 'search_api_relevance'
* are also included.
*/
function search_api_federated_solr_search_api_solr_field_mapping_alter(IndexInterface $index, array &$fields) {
$singleFieldsMap = [
// Field name => new single value for solr.
"url" => "ss_url",
];
// Iterate through the index fields and remap solr values where necessary.
foreach (array_keys($fields) as $key) {
// If this field is in our singleFieldsMap, remap it to its single value.
if (array_key_exists($key, $singleFieldsMap)) {
$fields[$key] = $singleFieldsMap[$key];
}
// Map all "mapped_field" property fields to their single value in solr.
$field = $index
->getField($key);
if (method_exists($field, 'getPropertyPath') && 'mapped_field' == $field
->getPropertyPath()) {
$fields[$key] = preg_replace("/^(\\w)m_/", "\$1s_", $fields[$key], 1);
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the federated_search_page_block_form form.
*
* Since the exposed form is a GET form, we don't want it to send the form
* tokens. However, you cannot make this happen in the form builder function
* itself, because the tokens are added to the form after the builder function
* is called. So, we have to do it in a form_alter.
*
* @see \Drupal\search_api_federated_solr\Form\FederatedSearchPageBlockForm
*/
function search_api_federated_solr_form_federated_search_page_block_form_alter(&$form, FormStateInterface $form_state) {
$form['form_build_id']['#access'] = FALSE;
$form['form_token']['#access'] = FALSE;
$form['form_id']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter() for search_api_federated_solr_search_settings_form.
*
* Validates whether or not the search app's chosen index has a site_name
* property and alters the search app settings form accordingly.
*
* @see \Drupal\search_api_federated_solr\Form\SearchApiFederatedSolrSearchAppSettingsForm
*/
function search_api_federated_solr_form_search_api_federated_solr_search_app_settings_alter(&$form, FormStateInterface $form_state) {
if ($search_index_id = $form['search_index']['#default_value']) {
$config = \Drupal::configFactory()
->getEditable('search_api_federated_solr.search_app.settings');
$index_config = \Drupal::config('search_api.index.' . $search_index_id);
// Determine if the index has a site name property, which could have been
// added / removed since last form load.
$site_name_property = $index_config
->get('field_settings.site_name.configuration.site_name');
$config
->set('index.has_site_name_property', $site_name_property ? TRUE : FALSE);
// If the index doesn't have a site name property.
if (!$site_name_property) {
// Reset the search app config options.
$form['site_name_property']['#value'] = '';
$form['set_search_site']['#default_value'] = 0;
$config
->set('facet.site_name.set_default', 0);
}
else {
// Ensure the hidden form field reflects lack of site name property.
$form['site_name_property']['#value'] = 'true';
}
$config
->save();
}
}
Functions
Name | Description |
---|---|
search_api_federated_solr_form_federated_search_page_block_form_alter | Implements hook_form_FORM_ID_alter() for the federated_search_page_block_form form. |
search_api_federated_solr_form_search_api_federated_solr_search_app_settings_alter | Implements hook_form_FORM_ID_alter() for search_api_federated_solr_search_settings_form. |
search_api_federated_solr_help | Implements hook_help(). |
search_api_federated_solr_search_api_solr_field_mapping_alter | Change the way the index's field names are mapped to Solr field names. |
search_api_federated_solr_theme | Implements hook_theme(). |