SearchApiFederatedSolrSiteName.php in Search API Federated Solr 7.3
File
src/SearchApiFederatedSolrSiteName.php
View source
<?php
class SearchApiFederatedSolrSiteName extends SearchApiAbstractAlterCallback {
public function supportsIndex(SearchApiIndex $index) {
return $index
->getEntityType() == 'node';
}
public function propertyInfo() {
return array(
'site_name' => array(
'label' => t('Site Name'),
'description' => t('Adds the site name to the indexed data.'),
'type' => 'list<string>',
),
);
}
public function alterItems(array &$items) {
if ($this
->useDomainAccess()) {
$this
->addDomainName($items);
}
else {
$this
->addSiteName($items);
}
}
protected function addSiteName(array &$items) {
$site_name = !empty($this->options['site_name']) ? $this->options['site_name'] : variable_get('site_name');
foreach ($items as &$item) {
$item->site_name = [
$site_name,
];
}
}
protected function addDomainName(array &$items) {
$type = $this->index
->getEntityType();
foreach ($items as &$item) {
$nid = entity_id($type, $item);
$entity = current(entity_load($type, [
$nid,
]));
$domains = domain_get_content_urls($entity);
$ds = [];
foreach ($domains as $domain_id => $url) {
$domain = domain_lookup($domain_id);
$ds[] = !empty($this->options['domain'][$domain['machine_name']]) ? $this->options['domain'][$domain['machine_name']] : $domain['sitename'];
}
$item->site_name = $ds;
}
}
public function configurationForm() {
if ($this
->useDomainAccess()) {
$form['domain'] = [
'#type' => 'container',
];
foreach (domain_list_by_machine_name() as $machine_name => $domain) {
$form['domain'][$machine_name] = [
'#type' => 'textfield',
'#title' => t('%domain Domain Label', [
'%domain' => $domain['sitename'],
]),
'#description' => t('Map the Domain to a custom label for search.'),
'#default_value' => !empty($this->options['domain'][$machine_name]) ? $this->options['domain'][$machine_name] : $domain['sitename'],
'#required' => TRUE,
];
}
}
else {
$form['site_name'] = [
'#type' => 'textfield',
'#title' => t('Site Name'),
'#description' => t('The name of the site from which this content originated. This can be useful if indexing multiple sites with a single search index.'),
];
}
return $form;
}
protected function useDomainAccess() {
return function_exists('domain_list_by_machine_name');
}
}
Classes
Name |
Description |
SearchApiFederatedSolrSiteName |
Class SearchApiFederatedSolrSiteName
Provides a Search API index data alteration that adds a "Site Name" property to each indexed item. |