You are here

class SearchApiFederatedSolrSiteName in Search API Federated Solr 7.3

Same name and namespace in other branches
  1. 7 src/SearchApiFederatedSolrSiteName.php \SearchApiFederatedSolrSiteName
  2. 7.2 src/SearchApiFederatedSolrSiteName.php \SearchApiFederatedSolrSiteName

Class SearchApiFederatedSolrSiteName Provides a Search API index data alteration that adds a "Site Name" property to each indexed item.

Hierarchy

Expanded class hierarchy of SearchApiFederatedSolrSiteName

1 string reference to 'SearchApiFederatedSolrSiteName'
search_api_federated_solr_search_api_alter_callback_info in ./search_api_federated_solr.module
Implements hook_search_api_alter_callback_info().

File

src/SearchApiFederatedSolrSiteName.php, line 11

View source
class SearchApiFederatedSolrSiteName extends SearchApiAbstractAlterCallback {

  /**
   * {@inheritdoc}
   */
  public function supportsIndex(SearchApiIndex $index) {

    // Code in this class assumes that it is working with nodes.
    return $index
      ->getEntityType() == 'node';
  }

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

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

    // Map the Domain of each node to its configured label.
    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;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function configurationForm() {
    if ($this
      ->useDomainAccess()) {
      $form['domain'] = [
        '#type' => 'container',
      ];

      // Provide a configuration field to map each Domain to a different label for indexing.
      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;
  }

  /**
   * Whether to use the site name from Domain Access.
   *
   * @return bool
   */
  protected function useDomainAccess() {
    return function_exists('domain_list_by_machine_name');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractAlterCallback::$index protected property The index whose items will be altered.
SearchApiAbstractAlterCallback::$options protected property The configuration options for this callback, if it has any.
SearchApiAbstractAlterCallback::configurationFormSubmit public function Implements SearchApiAlterCallbackInterface::configurationFormSubmit(). Overrides SearchApiAlterCallbackInterface::configurationFormSubmit 4
SearchApiAbstractAlterCallback::configurationFormValidate public function Implements SearchApiAlterCallbackInterface::configurationFormValidate(). Overrides SearchApiAlterCallbackInterface::configurationFormValidate 1
SearchApiAbstractAlterCallback::isMultiEntityIndex protected function Determines whether the given index contains multiple types of entities.
SearchApiAbstractAlterCallback::__construct public function Implements SearchApiAlterCallbackInterface::__construct(). Overrides SearchApiAlterCallbackInterface::__construct 1
SearchApiFederatedSolrSiteName::addDomainName protected function
SearchApiFederatedSolrSiteName::addSiteName protected function
SearchApiFederatedSolrSiteName::alterItems public function Alter items before indexing. Overrides SearchApiAlterCallbackInterface::alterItems
SearchApiFederatedSolrSiteName::configurationForm public function Implements SearchApiAlterCallbackInterface::configurationForm(). Overrides SearchApiAbstractAlterCallback::configurationForm
SearchApiFederatedSolrSiteName::propertyInfo public function Implements SearchApiAlterCallbackInterface::propertyInfo(). Overrides SearchApiAbstractAlterCallback::propertyInfo
SearchApiFederatedSolrSiteName::supportsIndex public function Implements SearchApiAlterCallbackInterface::supportsIndex(). Overrides SearchApiAbstractAlterCallback::supportsIndex
SearchApiFederatedSolrSiteName::useDomainAccess protected function Whether to use the site name from Domain Access.