You are here

Urls.php in Search API Field Map 8.2

File

src/Plugin/search_api/processor/Urls.php
View source
<?php

namespace Drupal\search_api_field_map\Plugin\search_api\processor;

use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;
use Drupal\search_api_field_map\Plugin\search_api\processor\Property\UrlsProperty;

/**
 * Adds the Urls to the indexed data.
 *
 * @SearchApiProcessor(
 *   id = "search_api_urls",
 *   label = @Translation("Urls"),
 *   description = @Translation("Adds the Urls to the indexed data."),
 *   stages = {
 *     "add_properties" = 0,
 *   },
 *   locked = true,
 *   hidden = true,
 * )
 */
class Urls extends ProcessorPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
    $properties = [];
    if (!$datasource) {
      $definition = [
        'label' => $this
          ->t('Urls'),
        'description' => $this
          ->t('URLs pointing to this node on all sites containing.'),
        'type' => 'string',
        'processor_id' => $this
          ->getPluginId(),
      ];
      $properties['search_api_urls'] = new UrlsProperty($definition);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function addFieldValues(ItemInterface $item) {
    $url = $item
      ->getDatasource()
      ->getItemUrl($item
      ->getOriginalObject());
    if ($url) {
      $fields = $this
        ->getFieldsHelper()
        ->filterForPropertyPath($item
        ->getFields(), NULL, 'search_api_urls');
      foreach ($fields as $field) {
        $url = $url
          ->setAbsolute()
          ->toString();
        $field
          ->addValue($url);
      }
    }
  }

}

Classes

Namesort descending Description
Urls Adds the Urls to the indexed data.