You are here

public function MappedFields::addFieldValues in Search API Field Map 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/MappedFields.php \Drupal\search_api_field_map\Plugin\search_api\processor\MappedFields::addFieldValues()
  2. 8 src/Plugin/search_api/processor/MappedFields.php \Drupal\search_api_field_map\Plugin\search_api\processor\MappedFields::addFieldValues()
  3. 4.x src/Plugin/search_api/processor/MappedFields.php \Drupal\search_api_field_map\Plugin\search_api\processor\MappedFields::addFieldValues()

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/MappedFields.php, line 50

Class

MappedFields
Normalize multiple content types into a single mapped field.

Namespace

Drupal\search_api_field_map\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {

  // Get all of the mapped fields on our item.
  $mapped_fields = $this
    ->getFieldsHelper()
    ->filterForPropertyPath($item
    ->getFields(), NULL, 'mapped_field');

  // Get the entity object, bail if there's somehow not one.
  $entity = $item
    ->getOriginalObject()
    ->getValue();
  if (!$entity) {

    // Apparently we were active for a wrong item.
    return;
  }

  // Set some helper vars for the entity and bundle type.
  $entity_type = $entity
    ->getEntityTypeId();
  $bundle_type = $entity
    ->bundle();

  // Process and set values for each mapped field on the item.
  foreach ($mapped_fields as $mapped_field) {

    // Get configuration for the field.
    $configuration = $mapped_field
      ->getConfiguration();

    // If there's a config item for the entity and bundle type we're in, set the value for the field.
    if (!empty($configuration['field_data'][$entity_type][$bundle_type])) {
      $token = \Drupal::token();

      // If the token replacement produces a value, add to this item.
      if ($value = $token
        ->replace($configuration['field_data'][$entity_type][$bundle_type], [
        $entity_type => $entity,
      ], [
        'clear' => true,
      ])) {

        // Do not use setValues(), since that doesn't preprocess the values according to their data type.
        $mapped_field
          ->addValue($value);
      }
    }
  }
}