You are here

protected function FieldsProcessorPluginBase::processField in Search API 8

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.

Parameters

\Drupal\search_api\Item\FieldInterface $field: The field to process.

5 calls to FieldsProcessorPluginBase::processField()
FieldsProcessorPluginBase::preprocessIndexItems in src/Processor/FieldsProcessorPluginBase.php
Preprocesses search items for indexing.
HtmlFilter::processField in src/Plugin/search_api/processor/HtmlFilter.php
Processes a single field's value.
IgnoreCase::processField in src/Plugin/search_api/processor/IgnoreCase.php
Processes a single field's value.
Stemmer::preprocessIndexItems in src/Plugin/search_api/processor/Stemmer.php
Preprocesses search items for indexing.
Tokenizer::processField in src/Plugin/search_api/processor/Tokenizer.php
Processes a single field's value.
3 methods override FieldsProcessorPluginBase::processField()
HtmlFilter::processField in src/Plugin/search_api/processor/HtmlFilter.php
Processes a single field's value.
IgnoreCase::processField in src/Plugin/search_api/processor/IgnoreCase.php
Processes a single field's value.
Tokenizer::processField in src/Plugin/search_api/processor/Tokenizer.php
Processes a single field's value.

File

src/Processor/FieldsProcessorPluginBase.php, line 324

Class

FieldsProcessorPluginBase
Provides a base class for processors that work on individual fields.

Namespace

Drupal\search_api\Processor

Code

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.
    if ($value instanceof TextValueInterface) {
      $tokens = $value
        ->getTokens();
      if ($tokens !== NULL) {
        $new_tokens = [];
        foreach ($tokens as $token) {
          $token_text = $token
            ->getText();
          $this
            ->processFieldValue($token_text, $type);
          if (is_scalar($token_text)) {
            if ($token_text !== '') {
              $token
                ->setText($token_text);
              $new_tokens[] = $token;
            }
          }
          else {
            $base_boost = $token
              ->getBoost();

            /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextTokenInterface $new_token */
            foreach ($token_text as $new_token) {
              if ($new_token
                ->getText() !== '') {
                $new_token
                  ->setBoost($new_token
                  ->getBoost() * $base_boost);
                $new_tokens[] = $new_token;
              }
            }
          }
        }
        $value
          ->setTokens($new_tokens);
      }
      else {
        $text = $value
          ->getText();
        if ($text !== '') {
          $this
            ->processFieldValue($text, $type);
          if ($text === '') {
            unset($values[$i]);
          }
          elseif (is_scalar($text)) {
            $value
              ->setText($text);
          }
          else {
            $value
              ->setTokens($text);
          }
        }
      }
    }
    elseif ($value !== '') {
      $this
        ->processFieldValue($value, $type);
      if ($value === '') {
        unset($values[$i]);
      }
    }
  }
  $field
    ->setValues(array_values($values));
}