SearchApiFederatedSolrCanonicalUrl.php in Search API Federated Solr 7.2
File
src/SearchApiFederatedSolrCanonicalUrl.php
View source
<?php
class SearchApiFederatedSolrCanonicalUrl extends SearchApiAbstractAlterCallback {
protected $index;
protected $options;
public function propertyInfo() {
return [
'canonical_url' => [
'label' => t('Canonical URL'),
'description' => t('Preferred URL for this content'),
'type' => 'uri',
'cardinality' => -1,
],
];
}
public function alterItems(array &$items) {
if ($this
->useDomainAccess()) {
$this
->addDomainUrl($items);
}
else {
$this
->addUrl($items);
}
}
protected function addUrl(array &$items) {
foreach ($items as &$item) {
$url = $this->index
->datasource()
->getItemUrl($item);
if (!$url) {
$item->canonical_url = NULL;
continue;
}
$item->canonical_url = url($url['path'], array(
'absolute' => TRUE,
) + $url['options']);
}
}
protected function addDomainUrl(array &$items) {
$entity_type = $this->index
->getEntityType();
$entity_info = entity_get_info($entity_type);
foreach ($items as $item) {
$id = entity_id($entity_type, $item);
$entity = current(entity_load($entity_type, [
$id,
]));
if (!$entity) {
return;
}
if (isset($entity->domain_source) && $entity->domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
$this->canonical_url = '';
}
else {
$list = [
$item,
];
$this
->addUrl($list);
}
}
}
protected function useDomainAccess() {
return defined('DOMAIN_SOURCE_USE_ACTIVE');
}
}
Classes
Name |
Description |
SearchApiFederatedSolrCanonicalUrl |
Class SearchApiFederatedSolrCanonicalUrl
Provides a Search API index data alteration that indicates the preferred
URL content is available on to each indexed item. |