You are here

public function Statistics::addFieldValues in Search API Sort Priority 8

Adds the values of properties defined by this processor to the item.

Parameters

\Drupal\search_api\Item\ItemInterface $item: The item whose field values should be added.

Overrides ProcessorPluginBase::addFieldValues

File

src/Plugin/search_api/processor/Statistics.php, line 79

Class

Statistics
Adds customized sort priority by Statistics.

Namespace

Drupal\search_api_sort_priority\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {

  // Only run for node and comment items.
  $entity_type_id = $item
    ->getDatasource()
    ->getEntityTypeId();
  if (!in_array($entity_type_id, $this->configuration['allowed_entity_types'])) {
    return;
  }
  $fields = $this
    ->getFieldsHelper()
    ->filterForPropertyPath($item
    ->getFields(), NULL, $this->targetFieldId);
  switch ($entity_type_id) {
    case 'node':

      // Get the node object.
      $node = $this
        ->getNode($item
        ->getOriginalObject());

      // Get statistics for this node.
      $nodeStatistics = $this
        ->statisticsGet($node
        ->id());

      // Set the weight on all the configured fields.
      foreach ($fields as $field) {
        $field
          ->addValue($nodeStatistics['totalcount']);
      }
      break;
  }
}