/** * {@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; }
/** * 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()]; }
/** * Returns a form to edit a facet on a Search API index. * * @param \Drupal\facets\FacetInterface $facets_facet * Facet currently being edited. * * @return array * The facet edit form. */ public function editForm(FacetInterface $facets_facet) { $facet = \Drupal::service('entity_type.manager')->getStorage('facets_facet')->load($facets_facet->id()); return $this->entityFormBuilder()->getForm($facet, 'default'); }
/** * 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; }