View source
<?php
namespace Drupal\entity_reference_facet_link\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\facets\Result\Result;
use Drupal\facets\UrlProcessor\UrlProcessorPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityReferenceFacetFormatterBase extends EntityReferenceFormatterBase implements ContainerFactoryPluginInterface {
protected $facet;
protected $facetStorage;
protected $urlProcessorPluginManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $facet_storage, UrlProcessorPluginManager $url_processor_plugin_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->facetStorage = $facet_storage;
$this->urlProcessorPluginManager = $url_processor_plugin_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('entity_type.manager')
->getStorage('facets_facet'), $container
->get('plugin.manager.facets.url_processor'));
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
if ($facet = $this
->getFacet()) {
$dependencies[$facet
->getConfigDependencyKey()][] = $facet
->getConfigDependencyName();
}
return $dependencies;
}
public static function defaultSettings() {
return [
'facet' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$facets = $this->facetStorage
->loadMultiple();
$options = [];
foreach ($facets as $facet) {
if ($facet
->getFieldIdentifier() == $this->fieldDefinition
->getName()) {
$options[$facet
->id()] = $facet
->label();
}
}
$elements['facet'] = [
'#title' => $this
->t('Select the facet to which the labels should be linked.'),
'#type' => 'select',
'#default_value' => $this
->getSetting('facet'),
'#options' => $options,
];
return $elements;
}
public function settingsSummary() {
$summary = [];
if ($facet = $this
->getFacet()) {
$summary[] = $this
->t('Selected facet: @facet', [
'@facet' => $facet
->label(),
]);
}
else {
$summary[] = $this
->t('No facet selected');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$facet = $this
->getFacet();
if (empty($facet)) {
return [];
}
$url_processor_id = $facet
->getFacetSourceConfig()
->getUrlProcessorName();
$configuration = [
'facet' => $facet,
];
$url_processor = $this->urlProcessorPluginManager
->createInstance($url_processor_id, $configuration);
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
$result = new Result($facet, $entity
->id(), $entity
->label(), 0);
$result = $url_processor
->buildUrls($facet, [
$result,
])[0];
$cache_tags = Cache::mergeTags($entity
->getCacheTags(), $facet
->getFacetSourceConfig()
->getCacheTags());
$elements[$delta] = $this
->buildElement($result
->getUrl(), $entity) + [
'#cache' => [
'tags' => $cache_tags,
],
];
}
return $elements;
}
protected abstract function buildElement(Url $url, EntityInterface $entity);
protected function getFacet() {
if (!isset($this->facet)) {
$this->facet = $this->facetStorage
->load($this
->getSetting('facet'));
}
return $this->facet;
}
}