/**
  * {@inheritdoc}
  */
 public function build(FacetInterface $facet, array $results)
 {
     $processors = $facet->getProcessors();
     $config = $processors[$this->getPluginId()];
     // This should load the facet's config to find the ordering direction.
     return $this->sortResults($results, $config->getConfiguration()['sort']);
 }
 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet)
 {
     $processors = $facet->getProcessors();
     $config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : NULL;
     $build['exclude'] = ['#title' => $this->t('Exclude items'), '#type' => 'textfield', '#default_value' => !is_null($config) ? $config->getConfiguration()['exclude'] : $this->defaultConfiguration()['exclude'], '#description' => $this->t("Comma separated list of titles or values that should be excluded, matching either an item's title or value.")];
     $build['regex'] = ['#title' => $this->t('Regular expressions used'), '#type' => 'checkbox', '#default_value' => !is_null($config) ? $config->getConfiguration()['regex'] : $this->defaultConfiguration()['regex'], '#description' => $this->t('Interpret each exclude list item as a regular expression pattern.<br /><small>(Slashes are escaped automatically, patterns using a comma can be wrapped in "double quotes", and if such a pattern uses double quotes itself, just make them double-double-quotes (""))</small>.')];
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet)
 {
     $processors = $facet->getProcessors();
     $config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : NULL;
     $build['minimum_items'] = array('#title' => $this->t('Minimum items'), '#type' => 'number', '#min' => 1, '#default_value' => !is_null($config) ? $config->getConfiguration()['minimum_items'] : $this->defaultConfiguration()['minimum_items'], '#description' => $this->t('Hide block if the facet contains less than this number of results.'));
     $max_default_value = !is_null($config) ? $config->getConfiguration()['maximum_items'] : $this->defaultConfiguration()['maximum_items'];
     $build['maximum_items'] = array('#title' => $this->t('Maximum items'), '#type' => 'number', '#min' => 1, '#default_value' => $max_default_value ? $max_default_value : '', '#description' => $this->t('Hide block if the facet contains more than this number of results.'));
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function build(FacetInterface $facet, array $results)
 {
     $processors = $facet->getProcessors();
     $config = $processors[$this->getPluginId()];
     $char = $config->getConfiguration()['character'];
     /** @var \Drupal\facets\Result\ResultInterface $result */
     foreach ($results as $id => $result) {
         if (strpos(strtolower($result->getDisplayValue()), $char) === 0) {
             unset($results[$id]);
         }
     }
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function build(FacetInterface $facet, array $results)
 {
     $facet_results = $facet->getResults();
     $result_count = 0;
     foreach ($facet_results as $result) {
         if ($result->isActive()) {
             $result_count += $result->getCount();
         }
     }
     /** @var \Drupal\facets\Result\ResultInterface $result */
     foreach ($results as $id => $result) {
         if ($result->getCount() == $result_count && !$result->isActive()) {
             unset($results[$id]);
         }
     }
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function getQueryTypesForFacet(FacetInterface $facet)
 {
     // Get our Facets Field Identifier, which is equal to the Search API Field
     // identifier.
     $field_id = $facet->getFieldIdentifier();
     // Get the Search API Server.
     $server = $this->index->getServerInstance();
     // Get the Search API Backend.
     $backend = $server->getBackend();
     $fields = $this->index->getFields();
     foreach ($fields as $field) {
         if ($field->getFieldIdentifier() == $field_id) {
             return $this->getQueryTypesForDataType($backend, $field->getType());
         }
     }
     throw new InvalidQueryTypeException($this->t("No available query types were found for facet @facet", ['@facet' => $facet->getName()]));
 }
Beispiel #7
0
 /**
  * {@inheritdoc}
  */
 public function build(FacetInterface $facet)
 {
     $this->facet = $facet;
     /** @var \Drupal\facets\Result\Result[] $results */
     $results = $facet->getResults();
     $items = [];
     $configuration = $facet->getWidgetConfigs();
     $this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
     $this->defaultOptionLabel = isset($configuration['default_option_label']) ? $configuration['default_option_label'] : '';
     foreach ($results as $result) {
         if (is_null($result->getUrl())) {
             $text = $this->extractText($result);
             $items[] = ['#markup' => $text];
         } else {
             $items[] = $this->buildListItems($result);
         }
     }
     $build = ['#theme' => 'item_list', '#items' => $items, '#attributes' => ['class' => ['js-facets-dropdown-links'], 'data-facet-default-option-label' => $this->defaultOptionLabel], '#cache' => ['contexts' => ['url.path', 'url.query_args']]];
     $build['#attached']['library'][] = 'facets/drupal.facets.dropdown-widget';
     return $build;
 }
 /**
  * {@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());
     }
     // 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);
     }
     if ($field) {
         $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;
 }
Beispiel #9
0
 /**
  * {@inheritdoc}
  */
 public function build(FacetInterface $facet)
 {
     /** @var \Drupal\facets\Result\Result[] $results */
     $results = $facet->getResults();
     $items = [];
     $configuration = $facet->getWidgetConfigs();
     $this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
     foreach ($results as $result) {
         if (is_null($result->getUrl())) {
             $text = $this->extractText($result);
             $items[] = ['#markup' => $text];
         } else {
             $items[] = $this->buildListItems($result);
         }
     }
     $build = ['#theme' => 'item_list', '#items' => $items, '#attributes' => ['data-drupal-facet-id' => $facet->id()], '#cache' => ['contexts' => ['url.path', 'url.query_args']]];
     if (!empty($configuration['soft_limit'])) {
         $build['#attached']['library'][] = 'facets/soft-limit';
         $build['#attached']['drupalSettings']['facets']['softLimit'][$facet->id()] = (int) $configuration['soft_limit'];
     }
     return $build;
 }
Beispiel #10
0
 /**
  * {@inheritdoc}
  */
 public function setActiveItems(FacetInterface $facet)
 {
     // Set the url alias from the the facet object.
     $this->urlAlias = $facet->getUrlAlias();
     // Get the filter key of the facet.
     if (isset($this->activeFilters[$this->urlAlias])) {
         foreach ($this->activeFilters[$this->urlAlias] as $value) {
             $facet->setActiveItem(trim($value, '"'));
         }
     }
 }
 /**
  * Builds the form for editing and creating a facet.
  *
  * @param \Drupal\facets\FacetInterface $facet
  *   The facets facet entity that is being created or edited.
  */
 public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet)
 {
     $facet_sources = [];
     foreach ($this->getFacetSourcePluginManager()->getDefinitions() as $facet_source_id => $definition) {
         $facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
     }
     if (count($facet_sources) == 0) {
         $form['#markup'] = $this->t('You currently have no facet sources defined. You should start by adding a facet source before creating facets.');
         return;
     }
     $form['facet_source_id'] = ['#type' => 'select', '#title' => $this->t('Facet source'), '#description' => $this->t('The source where this facet can find its fields.'), '#options' => $facet_sources, '#default_value' => $facet->getFacetSourceId(), '#required' => TRUE, '#ajax' => ['trigger_as' => ['name' => 'facet_source_configure'], 'callback' => '::buildAjaxFacetSourceConfigForm', 'wrapper' => 'facets-facet-sources-config-form', 'method' => 'replace', 'effect' => 'fade']];
     $form['facet_source_configs'] = ['#type' => 'container', '#attributes' => ['id' => 'facets-facet-sources-config-form'], '#tree' => TRUE];
     $form['facet_source_configure_button'] = ['#type' => 'submit', '#name' => 'facet_source_configure', '#value' => $this->t('Configure facet source'), '#limit_validation_errors' => [['facet_source_id']], '#submit' => ['::submitAjaxFacetSourceConfigForm'], '#ajax' => ['callback' => '::buildAjaxFacetSourceConfigForm', 'wrapper' => 'facets-facet-sources-config-form'], '#attributes' => ['class' => ['js-hide']]];
     $this->buildFacetSourceConfigForm($form, $form_state);
     $form['name'] = ['#type' => 'textfield', '#title' => $this->t('Name'), '#description' => $this->t('The administrative name used for this facet.'), '#default_value' => $facet->label(), '#required' => TRUE];
     $form['id'] = ['#type' => 'machine_name', '#default_value' => $facet->id(), '#maxlength' => 50, '#required' => TRUE, '#machine_name' => ['exists' => [$this->getFacetStorage(), 'load'], 'source' => ['name']]];
     $form['status'] = ['#type' => 'checkbox', '#title' => $this->t('Enabled'), '#description' => $this->t('Only enabled facets can be displayed.'), '#default_value' => $facet->status()];
 }
Beispiel #12
0
 /**
  * Returns the page title for an facets's "View" tab.
  *
  * @param \Drupal\facets\FacetInterface $facet
  *   The facet that is displayed.
  *
  * @return string
  *   The page title.
  */
 public function pageTitle(FacetInterface $facet)
 {
     return new FormattableMarkup('@title', array('@title' => $facet->label()));
 }
 /**
  * {@inheritdoc}
  */
 public function getQueryInfo(FacetInterface $facet)
 {
     $query_info = [];
     $field_name = $facet->getFieldIdentifier();
     $default_fields = $this->getDefaultFields();
     if (array_key_exists($facet->getFieldIdentifier(), $default_fields)) {
         // We add the language code of the indexed item to the result of the
         // query. So in this case we need to use the search_index table alias (i)
         // for the langcode field. Otherwise we will have same nid for multiple
         // languages as result. For more details see NodeSearch::findResults().
         // @TODO review if I can refactor this.
         $table_alias = $facet->getFieldIdentifier() == 'langcode' ? 'i' : 'n';
         $query_info = ['fields' => [$table_alias . '.' . $facet->getFieldIdentifier() => ['table_alias' => $table_alias, 'field' => $facet->getFieldIdentifier()]]];
     } else {
         // Gets field info, finds table name and field name.
         $table = "node__{$field_name}";
         // The column name will be different depending on the field type, it's
         // always the fields machine name, suffixed with '_value'. Entity
         // reference fields change that suffix into '_target_id'.
         $field_config = FieldStorageConfig::loadByName('node', $facet->getFieldIdentifier());
         $field_type = $field_config->getType();
         if ($field_type == 'entity_reference') {
             $column = $facet->getFieldIdentifier() . '_target_id';
         } else {
             $column = $facet->getFieldIdentifier() . '_value';
         }
         $query_info['fields'][$field_name . '.' . $column] = array('table_alias' => $table, 'field' => $column);
         // Adds the join on the node table.
         $query_info['joins'] = array($table => array('table' => $table, 'alias' => $table, 'condition' => "n.vid = {$table}.revision_id AND i.langcode = {$table}.langcode"));
     }
     // Returns query info, makes sure all keys are present.
     return $query_info + ['joins' => [], 'fields' => []];
 }
Beispiel #14
0
 /**
  * {@inheritdoc}
  */
 protected function buildListItems(ResultInterface $result)
 {
     $items = parent::buildListItems($result);
     $items['#attributes']['data-drupal-facet-item-id'] = $this->facet->getUrlAlias() . '-' . $result->getRawValue();
     return $items;
 }
 /**
  * Returns one of the processed facets.
  *
  * Returns one of the processed facets, this is a facet with filled results.
  * Keep in mind that if you want to have the facet's build processor executed,
  * there needs to be an extra call to the FacetManager::build with the facet
  * returned here as argument.
  *
  * @param \Drupal\facets\FacetInterface $facet
  *   The facet to process.
  *
  * @return \Drupal\facets\FacetInterface|NULL
  *   The updated facet if it exists, NULL otherwise.
  */
 public function returnProcessedFacet(FacetInterface $facet)
 {
     $this->processFacets($facet->getFacetSourceId());
     return !empty($this->facets[$facet->id()]) ? $this->facets[$facet->id()] : NULL;
 }
 /**
  * {@inheritdoc}
  */
 public function submitConfigurationForm(array &$form, FormStateInterface $form_state)
 {
     $facet_source_id = $this->facet->getFacetSourceId();
     $field_identifier = $form_state->getValue('facet_source_configs')[$facet_source_id]['field_identifier'];
     $this->facet->setFieldIdentifier($field_identifier);
 }