/** * 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; }
/** * 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()]; }
/** * {@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); }