You are here

public function Glossary::addFieldValues in Search API AZ Glossary 8.4

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/Glossary.php \Drupal\search_api_glossary\Plugin\search_api\processor\Glossary::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/Glossary.php, line 85

Class

Glossary
Adds the item's AZ to the indexed data.

Namespace

Drupal\search_api_glossary\Plugin\search_api\processor

Code

public function addFieldValues(ItemInterface $item) {

  // TODO Dependency Inject.
  $glossary_helper = \Drupal::service('search_api_glossary.helper');
  $item_fields = $item
    ->getFields();

  // Get glossary fields.
  $glossary_fields_conf = $this
    ->getConfig();

  // Loop through all fields.
  foreach ($item_fields as $name => $field) {

    // Filter out hidden fields
    // and fields that do not match
    // our required criteria.
    // Finally check if this field has glossary enabled.
    if ($field
      ->isHidden() == FALSE && $this
      ->testType($field
      ->getType()) && $this
      ->checkFieldName($name) == FALSE && isset($glossary_fields_conf[$name])) {
      $glossary_field_conf = $glossary_fields_conf[$name]['glossary'];

      // Check if source field exists
      // and if glossary is enabled on this field.
      if (isset($glossary_field_conf) && $glossary_field_conf == 1 && !empty($field
        ->getValues())) {

        // Get the Parent field value.
        $source_field_value = $field
          ->getValues()[0];

        // Get target field name.
        $glossary_field_name = $this
          ->makeFieldName($name);

        // Calculated Glossary value.
        $glossary_value = $glossary_helper
          ->glossaryGetter($source_field_value, $glossary_fields_conf[$name]['grouping']);

        // Get target field.
        $glossary_field = $item_fields[$glossary_field_name];

        // Set the Target Glossary value.
        if (empty($glossary_field
          ->getValues())) {
          $glossary_field
            ->addValue($glossary_value);
        }
      }
    }
  }
}