Skip to content

Commit

Permalink
add #39 to 7.x-2.x umhs-28: use canonical URL for results (#94)
Browse files Browse the repository at this point in the history
add #39 to 7.x-2.x umhs-28: use canonical URL for results
  • Loading branch information
jesconstantine authored May 29, 2019
2 parents 90b279f + f7fbf25 commit 963c40f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ When changes to [federated-search-react](https://github.com/palantirnet/federate

## More information

Full documentation for this module is in the [handbook on Drupal.org](https://www.drupal.org/docs/7/modules/search-api-federated-solr/search-api-federated-solr-module)
Full documentation for this module is available in the [handbook on Drupal.org](https://www.drupal.org/docs/7/modules/search-api-federated-solr/search-api-federated-solr-module)

* [How to use this module](https://www.drupal.org/docs/7/modules/search-api-federated-solr/search-api-federated-solr-module/intro-install-configure)
* [How to configure a Search API Index for federated search](https://www.drupal.org/docs/8/modules/search-api-federated-solr/federated-search-schema)
Expand Down
1 change: 1 addition & 0 deletions search_api_federated_solr.info
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies[] = search_api_solr
dependencies[] = token

files[] = search-api_federated_solr.module
files[] = src/SearchApiFederatedSolrCanonicalUrl.php
files[] = src/SearchApiFederatedSolrField.php
files[] = src/SearchApiFederatedSolrTerms.php
files[] = src/SearchApiFederatedSolrRemap.php
Expand Down
5 changes: 5 additions & 0 deletions search_api_federated_solr.module
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ function search_api_federated_solr_search_api_alter_callback_info() {
'description' => t('The links to the node on all available sites. This can be useful if indexing multiple sites with a single search index.'),
'class' => 'SearchApiFederatedSolrUrls',
);
$callbacks['canonical_url'] = array(
'name' => t('Canonical URL'),
'description' => t('Preferred URL for this content.'),
'class' => 'SearchApiFederatedSolrCanonicalUrl',
);
$callbacks['federated_field'] = array(
'name' => t('Federated Field'),
'description' => t('A token or free text field that can be customized per-bundle.'),
Expand Down
94 changes: 94 additions & 0 deletions src/SearchApiFederatedSolrCanonicalUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* Class SearchApiFederatedSolrCanonicalUrl
* Provides a Search API index data alteration that indicates the preferred
* URL content is available on to each indexed item.
*/
class SearchApiFederatedSolrCanonicalUrl extends SearchApiAbstractAlterCallback {

/**
* @var SearchApiIndex
*/
protected $index;

/**
* @var array
*/
protected $options;

/**
* {@inheritdoc}
*/
public function propertyInfo() {
return [
'canonical_url' => [
'label' => t('Canonical URL'),
'description' => t('Preferred URL for this content'),
'type' => 'uri',
'cardinality' => -1,
],
];
}

/**
* {@inheritdoc}
*/
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);

// Get the entity object for the item being indexed, exit if there's somehow not one.
$entity = current(entity_load($entity_type, [$id]));
if (!$entity) {
return;
}

// Determine if there is a canonical URL for the content.
// This only comes into play if domain source is used.
if (isset($entity->domain_source) && $entity->domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
$this->canonical_url = '';
}
else {
$list = [$item];
$this->addUrl($list);
}
}

}

/**
* Whether to use the canonical value from Domain Source.
*
* @return bool
*/
protected function useDomainAccess() {
return defined('DOMAIN_SOURCE_USE_ACTIVE');
}

}
9 changes: 7 additions & 2 deletions src/SearchApiFederatedSolrUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ protected function addDomainUrls(array &$items) {
}

$urls = domain_get_content_urls($entity);

$item->urls = $urls;
if (!empty($urls)) {
$item->urls = $urls;
}
else {
$list = [$item];
$this->addUrl($list);
}
}

}
Expand Down

0 comments on commit 963c40f

Please sign in to comment.