You are here

class SiteName in Search API Field Map 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/SiteName.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteName
  2. 8 src/Plugin/search_api/processor/SiteName.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteName
  3. 8.2 src/Plugin/search_api/processor/SiteName.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteName

Adds the site name to the indexed data.

Plugin annotation


@SearchApiProcessor(
  id = "site_name",
  label = @Translation("Site name"),
  description = @Translation("Adds the site name to the indexed data."),
  stages = {
    "add_properties" = 0,
  },
  locked = true,
  hidden = true,
)

Hierarchy

  • class \Drupal\search_api_field_map\Plugin\search_api\processor\SiteName extends \Drupal\search_api\Processor\ProcessorPluginBase

Expanded class hierarchy of SiteName

File

src/Plugin/search_api/processor/SiteName.php, line 27

Namespace

Drupal\search_api_field_map\Plugin\search_api\processor
View source
class SiteName extends ProcessorPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
    $properties = [];
    if (!$datasource) {
      $definition = [
        'label' => $this
          ->t('Site Name'),
        'description' => $this
          ->t('The name of the site from which this content originated.'),
        'type' => 'string',
        'processor_id' => $this
          ->getPluginId(),
        'is_list' => TRUE,
      ];
      $properties['site_name'] = new SiteNameProperty($definition);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function addFieldValues(ItemInterface $item) {
    $fields = $this
      ->getFieldsHelper()
      ->filterForPropertyPath($item
      ->getFields(), NULL, 'site_name');
    if ($this
      ->useDomain()) {
      $entity = $item
        ->getOriginalObject()
        ->getValue();
      if ($entity instanceof EntityInterface) {
        $this
          ->addDomainName($item, $entity, $fields);
      }
    }
    else {
      foreach ($fields as $field) {
        $site_name = $field
          ->getConfiguration()['site_name'];
        $field
          ->addValue($site_name);
      }
    }
  }

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

  /**
   * Process site names for Domains.
   *
   * @param Drupal\search_api\Item\ItemInterface $item
   *   The item being indexed.
   * @param Drupal\Core\Entity\EntityInterface $entity
   *   The original entity of the item.
   * @param array $fields
   *   The fields being processed for this item.
   *
   * @TODO: Allow this value to be configured.
   */
  protected function addDomainName(ItemInterface $item, EntityInterface $entity, array $fields) {
    $manager = \Drupal::service('domain_access.manager');
    $urls = $manager
      ->getContentUrls($entity);
    if (!empty($urls)) {
      $storage = \Drupal::service('entity_type.manager')
        ->getStorage('domain');
      $domains = $storage
        ->loadMultiple();
      foreach ($fields as $field) {
        foreach ($urls as $domain_id => $url) {
          if (isset($domains[$domain_id])) {
            $site_name = !empty($field
              ->getConfiguration()['domain'][$domains[$domain_id]
              ->id()]) ? $field
              ->getConfiguration()['domain'][$domains[$domain_id]
              ->id()] : $domains[$domain_id]
              ->label();
          }
          else {
            $site_name = $field
              ->getConfiguration()['site_name'];
          }
          if (empty($site_name)) {
            $site_name = \Drupal::config('system.site')
              ->get('name');
          }
          $field
            ->addValue($site_name);
        }
      }
    }
    else {
      foreach ($fields as $field) {
        $site_name = $field
          ->getConfiguration()['site_name'];
        if (empty($site_name)) {
          $site_name = \Drupal::config('system.site')
            ->get('name');
        }
        $field
          ->addValue($site_name);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SiteName::addDomainName protected function Process site names for Domains.
SiteName::addFieldValues public function
SiteName::getPropertyDefinitions public function
SiteName::useDomain protected function Whether to use the canonical value from Domain Source.