/** * Extracts value and original type from a single piece of data. * * @param \Drupal\Core\TypedData\TypedDataInterface $data * The piece of data from which to extract information. * @param \Drupal\search_api\Item\FieldInterface $field * The field into which to put the extracted data. */ public static function extractField(TypedDataInterface $data, FieldInterface $field) { $values = static::extractFieldValues($data); // If the data type of the field is a custom one, then the value can be // altered by the data type plugin. $data_type_manager = \Drupal::service('plugin.manager.search_api.data_type'); if ($data_type_manager->hasDefinition($field->getType())) { /** @var \Drupal\search_api\DataType\DataTypeInterface $data_type_plugin */ $data_type_plugin = $data_type_manager->createInstance($field->getType()); foreach ($values as $i => $value) { $values[$i] = $data_type_plugin->getValue($value); } } $field->setValues($values); $field->setOriginalType($data->getDataDefinition()->getDataType()); }
/** * Processes a single field's value. * * Calls process() either for each value, or each token, depending on the * type. Also takes care of extracting list values and of fusing returned * tokens back into a one-dimensional array. * * @param \Drupal\search_api\Item\FieldInterface $field * The field to process. */ protected function processField(FieldInterface $field) { $values = $field->getValues(); $type = $field->getType(); foreach ($values as $i => &$value) { // We restore the field's type for each run of the loop since we need the // unchanged one as long as the current field value hasn't been updated. $type = $field->getType(); if ($type == 'tokenized_text') { foreach ($value as &$tokenized_value) { $this->processFieldValue($tokenized_value['value'], $type); } } else { $this->processFieldValue($value, $type); } if ($type == 'tokenized_text') { $value = $this->normalizeTokens($value); } elseif ($value === '') { unset($values[$i]); } } // We're also setting the type here as it could have changed. $field->setType($type); $field->setValues($values); }