Skip to content

Instantly share code, notes, and snippets.

@bnchdrff
Created April 18, 2016 18:32
Show Gist options
  • Select an option

  • Save bnchdrff/ef53d8ba8e708195ddae534b959c2cb8 to your computer and use it in GitHub Desktop.

Select an option

Save bnchdrff/ef53d8ba8e708195ddae534b959c2cb8 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\redactedmodulename\Plugin\facets\processor;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a processor that transforms the results to show the list item label for entities without bundles.
*
* Really, really similar to Drupal\facets\Plugin\facets\processor\ListItemProcessor.
*
* @FacetsProcessor(
* id = "list_item_prop",
* label = @Translation("List item label from bundle-less entity base property"),
* description = @Translation("Display the list item label instead of the key for an bundle-less entity base property"),
* stages = {
* "build" = 5
* }
* )
*/
class ListItemPropProcessor extends ProcessorPluginBase implements BuildProcessorInterface, ContainerFactoryPluginInterface {
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
private $entityFieldManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_field.manager')
);
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$field_identifier = $facet->getFieldIdentifier();
$entity = 'node';
// Support multiple entities when using Search API.
if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) {
$index = $facet->getFacetSource()->getIndex();
$field = $index->getField($field_identifier);
$entity = str_replace('entity:', '', $field->getDatasourceId());
}
$baseFields = $this->entityFieldManager->getFieldDefinitions($entity, '');
if ($field = $baseFields[$field_identifier]) {
$function = $field->getSetting('allowed_values_function');
if (empty($function)) {
$allowed_values = $field->getSetting('allowed_values');
}
else {
$allowed_values = ${$function}($field);
}
if (is_array($allowed_values)) {
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
if (isset($allowed_values[$result->getRawValue()])) {
$result->setDisplayValue($allowed_values[$result->getRawValue()]);
}
}
}
}
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment