diff --git a/src/Plugin/facets/processor/TranslateEntityProcessor.php b/src/Plugin/facets/processor/TranslateEntityProcessor.php index 415404d..36d45a4 100644 --- a/src/Plugin/facets/processor/TranslateEntityProcessor.php +++ b/src/Plugin/facets/processor/TranslateEntityProcessor.php @@ -3,6 +3,7 @@ namespace Drupal\facets\Plugin\facets\processor; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\TypedData\TranslatableInterface; @@ -87,33 +88,9 @@ class TranslateEntityProcessor extends ProcessorPluginBase implements BuildProce $ids[$delta] = $result->getRawValue(); } - // Default to nodes. - $entity_type = 'node'; - $source = $facet->getFacetSource(); - - // Support multiple entity types when using Search API. - if ($source instanceof SearchApiFacetSourceInterface) { - - $field_id = $facet->getFieldIdentifier(); - - // Load the index from the source, load the definition from the - // datasource. - /** @var \Drupal\facets\FacetSource\SearchApiFacetSourceInterface $source */ - $index = $source->getIndex(); - $field = $index->getField($field_id); - $datasource = $field->getDatasource(); - - // Load the field from the entity manager and find the entity type trough - // that. - $entity_id = $datasource->getEntityTypeId() . '.' . $field_id; - $field_storage = $this->entityTypeManager->getStorage('field_storage_config'); - $field_config = $field_storage->load($entity_id); - $entity_type = $field_config->getSetting('target_type'); - } - // Load all indexed entities of this type. $entities = $this->entityTypeManager - ->getStorage($entity_type) + ->getStorage($this->getEntityTypeFromFacet($facet)) ->loadMultiple($ids); // Loop over all results. @@ -140,4 +117,100 @@ class TranslateEntityProcessor extends ProcessorPluginBase implements BuildProce return $results; } + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) { + // Extact entity type from the facet, if null we need to ask the enitty_type to the user. + $entity_type = $this->getFieldEntityTypeFromFacet($facet); + $build = null; + + if (is_null($entity_type)) { + $processors = $facet->getProcessors(); + $config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : NULL; + + $entity_types = array_keys(\Drupal::entityTypeManager()->getDefinitions()); + $options = array_combine($entity_types, $entity_types); + + $build['entity_type'] = array( + '#title' => $this->t('Entity type'), + '#type' => 'select', + '#required' => TRUE, + '#options' => $options, + '#default_value' => !is_null($config) ? $config->getConfiguration()['entity_type'] : $this->defaultConfiguration()['entity_type'], + '#description' => $this->t('Select the entity type to which you want to refer.'), + ); + } + + return $build; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array( + 'entity_type' => null, + ); + } + + /** + * Get the entity_type from the FacetInterface. + * + * @param \Drupal\facets\FacetInterface $facet + * + * @return string + */ + private function getEntityTypeFromFacet(FacetInterface $facet) { + // Try to load entity_type from the field info, otherwise fetch it from configuration. + if ($entity_type = $this->getFieldEntityTypeFromFacet($facet)) { + return $entity_type; + } else { + $processors = $facet->getProcessors(); + $config = $processors[$this->getPluginId()]->getConfiguration(); + $entity_type = $config['entity_type']; + } + + return $entity_type; + } + + /** + * Get the entity_type from the field definition. + * + * @param \Drupal\facets\FacetInterface $facet + * + * @return mixed + * Returns entity type if known otherwise null. + */ + private function getFieldEntityTypeFromFacet(FacetInterface $facet) { + $entity_type = null; + $source = $facet->getFacetSource(); + + // Support multiple entity types when using Search API. + if ($source instanceof SearchApiFacetSourceInterface) { + $field_id = $facet->getFieldIdentifier(); + + // Load the index from the source, load the definition from the + // datasource. + /** @var \Drupal\facets\FacetSource\SearchApiFacetSourceInterface $source */ + $index = $source->getIndex(); + $field = $index->getField($field_id); + $datasource = $field->getDatasource(); + + if ($datasource) { + // Load the field from the entity manager and find the entity type trough + // that. + $entity_id = $datasource->getEntityTypeId() . '.' . $field_id; + $field_storage = $this->entityTypeManager->getStorage('field_storage_config'); + $field_config = $field_storage->load($entity_id); + $entity_type = $field_config->getSetting('target_type'); + } + + } + + return $entity_type; + } + }