You are here

public function FileMime::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/FileMime.php, line 76

Class

FileMime
Adds customized sort priority by File mime.

Namespace

Drupal\search_api_sort_priority\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {

  // Get default weight.
  $weight = $this->configuration['weight'];

  // We are only working with files for now.
  if ($item
    ->getDatasource()
    ->getEntityTypeId() == 'file') {
    $mimeType = $item
      ->getOriginalObject()
      ->getValue()
      ->getMimeType();

    // Get the weight assigned to content type.
    if ($this->configuration['sorttable'][$mimeType]['weight']) {
      $weight = $this->configuration['sorttable'][$mimeType]['weight'];
    }

    // Set the weight on all the configured fields.
    $fields = $this
      ->getFieldsHelper()
      ->filterForPropertyPath($item
      ->getFields(), NULL, $this->targetFieldId);
    foreach ($fields as $field) {
      $field
        ->addValue($weight);
    }
  }
}