diff --git a/README.md b/README.md index 4dbcfaaf8..b7ca20902 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/search_api_federated_solr.info b/search_api_federated_solr.info index 292e3bbf2..80484e028 100644 --- a/search_api_federated_solr.info +++ b/search_api_federated_solr.info @@ -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 diff --git a/search_api_federated_solr.module b/search_api_federated_solr.module index 264f8ca9b..47be51e69 100644 --- a/search_api_federated_solr.module +++ b/search_api_federated_solr.module @@ -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.'), diff --git a/src/SearchApiFederatedSolrCanonicalUrl.php b/src/SearchApiFederatedSolrCanonicalUrl.php new file mode 100644 index 000000000..5ca20fd4c --- /dev/null +++ b/src/SearchApiFederatedSolrCanonicalUrl.php @@ -0,0 +1,94 @@ + [ + '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'); + } + +} diff --git a/src/SearchApiFederatedSolrUrls.php b/src/SearchApiFederatedSolrUrls.php index 7e6057b8e..5eb663647 100644 --- a/src/SearchApiFederatedSolrUrls.php +++ b/src/SearchApiFederatedSolrUrls.php @@ -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); + } } }