You are here

public function ListItemProcessor::build in Facets 8

Runs before the renderable array is created.

Parameters

\Drupal\facets\FacetInterface $facet: The facet being changed.

\Drupal\facets\Result\ResultInterface[] $results: The results being changed.

Return value

\Drupal\facets\Result\ResultInterface[] The changed results.

Overrides BuildProcessorInterface::build

File

src/Plugin/facets/processor/ListItemProcessor.php, line 95

Class

ListItemProcessor
Provides a processor that transforms the results to show the list item label.

Namespace

Drupal\facets\Plugin\facets\processor

Code

public function build(FacetInterface $facet, array $results) {
  $field_identifier = $facet
    ->getFieldIdentifier();
  $entity = 'node';
  $field = FALSE;
  $allowed_values = [];

  // Support multiple entities when using Search API.
  if ($facet
    ->getFacetSource() instanceof SearchApiDisplay) {

    /** @var \Drupal\search_api\Entity\Index $index */
    $index = $facet
      ->getFacetSource()
      ->getIndex();

    /** @var \Drupal\search_api\Item\Field $field */
    $field = $index
      ->getField($field_identifier);
    if (!$field
      ->getDatasourceId()) {
      throw new InvalidProcessorException("This field has no datasource, there is no valid use for this processor with this facet");
    }
    $entity = $field
      ->getDatasource()
      ->getEntityTypeId();
  }

  // If it's an entity base field, we find it in the field definitions.
  // We don't have access to the bundle via SearchApiFacetSourceInterface, so
  // we check the entity's base fields only.
  $base_fields = $this->entityFieldManager
    ->getFieldDefinitions($entity, '');

  // This only works for configurable fields.
  $config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier);
  if (isset($base_fields[$field_identifier])) {
    $field = $base_fields[$field_identifier];
  }
  elseif ($this->configManager
    ->loadConfigEntityByName($config_entity_name) !== NULL) {
    $field = $this->configManager
      ->loadConfigEntityByName($config_entity_name);
  }
  elseif ($field
    ->getDataDefinition() instanceof FieldItemDataDefinition) {
    $fieldDefinition = $field
      ->getDataDefinition()
      ->getFieldDefinition();
    $referenced_entity_name = sprintf('field.storage.%s.%s', $fieldDefinition
      ->getTargetEntityTypeId(), $fieldDefinition
      ->getName());
    if ($fieldDefinition instanceof BaseFieldDefinition) {
      if (isset($base_fields[$field
        ->getPropertyPath()])) {
        $field = $base_fields[$field
          ->getPropertyPath()];
      }
    }
    elseif ($this->configManager
      ->loadConfigEntityByName($referenced_entity_name) !== NULL) {
      $field = $this->configManager
        ->loadConfigEntityByName($referenced_entity_name);
    }
  }
  if ($field instanceof FieldStorageDefinitionInterface) {
    if ($field
      ->getName() !== 'type') {
      $allowed_values = options_allowed_values($field);
      if (!empty($allowed_values)) {
        return $this
          ->overWriteDisplayValues($results, $allowed_values);
      }
    }
  }

  // If no values are found for the current field, try to see if this is a
  // bundle field.
  $list_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($entity);
  if (!empty($list_bundles)) {
    foreach ($list_bundles as $key => $bundle) {
      $allowed_values[$key] = $bundle['label'];
    }
    return $this
      ->overWriteDisplayValues($results, $allowed_values);
  }
  return $results;
}