diff --git a/src/Plugin/facets/processor/TranslateEntityProcessor.php b/src/Plugin/facets/processor/TranslateEntityProcessor.php index 4b1591e..d043ed1 100644 --- a/src/Plugin/facets/processor/TranslateEntityProcessor.php +++ b/src/Plugin/facets/processor/TranslateEntityProcessor.php @@ -3,6 +3,8 @@ namespace Drupal\facets\Plugin\facets\processor; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\TypedData\TranslatableInterface; @@ -87,33 +89,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 SearchApiDisplay) { - - $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 +118,91 @@ 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', + '#states' => array( + 'required' => array( + array(':input[name="facet_settings[translate_entity][status]"]' => array('checked' => 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 SearchApiDisplay) { + $data_definition = $source->getIndex() + ->getField($facet->getFieldIdentifier())->getDataDefinition(); + $entity_type = $data_definition->getSetting('target_type'); + + if (empty($entity_type) && $data_definition instanceof FieldItemInterface) { + $entity_type = $data_definition->getFieldDefinition()->getTargetEntityTypeId(); + } + } + + return $entity_type; + } + }