diff --git a/src/Plugin/facets/widget/CheckboxWidget.php b/src/Plugin/facets/widget/CheckboxWidget.php index db1735e..d45da2e 100644 --- a/src/Plugin/facets/widget/CheckboxWidget.php +++ b/src/Plugin/facets/widget/CheckboxWidget.php @@ -7,13 +7,14 @@ namespace Drupal\facets\Plugin\facets\widget; +use Drupal\Core\Cache\CacheableRedirectResponse; use Drupal\Core\Form\FormInterface; -use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Link; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\facets\Exception\Exception; use Drupal\facets\FacetInterface; use Drupal\facets\Widget\WidgetInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * The checkbox / radios widget. @@ -37,11 +38,6 @@ class CheckboxWidget implements WidgetInterface, FormInterface { protected $linkGenerator; /** - * @var \Drupal\facets\FacetInterface - */ - protected $facet; - - /** * {@inheritdoc} */ public function execute() { @@ -52,11 +48,13 @@ class CheckboxWidget implements WidgetInterface, FormInterface { * {@inheritdoc} */ public function build(FacetInterface $facet) { + $form_builder = \Drupal::getContainer()->get('form_builder'); - $this->facet = $facet; - - $formBuilder = \Drupal::getContainer()->get('form_builder'); - $build = $formBuilder->getForm(static::class, new FormState()); + // The form builder's getForm method accepts 1 argument in the interface, + // the form ID. Extra arguments get passed into the form states addBuildInfo + // method. This way we can pass the facet to the ::buildForm method, it uses + // FormState::getBuildInfo to get the facet out. + $build = $form_builder->getForm(static::class, $facet); return $build; } @@ -102,32 +100,20 @@ class CheckboxWidget implements WidgetInterface, FormInterface { } /** - * Returns a unique string identifying the form. - * - * @return string - * The unique string identifying the form. + * {@inheritdoc} */ public function getFormId() { - return 'checkbox_widget'; + return 'facets_checkbox_widget'; } /** - * Form constructor. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @return array - * The form structure. + * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $facet = $this->facet; - if (is_null($this->facet)) { - throw new Exception("The facet is not correctly passed on to the checkboxWidget::buildForm method so we can't build"); - } + /** @var \Drupal\facets\FacetInterface $facet */ + $build_info = $form_state->getBuildInfo(); + $facet = $build_info['args'][0]; /** @var \Drupal\facets\Result\Result[] $results */ $results = $facet->getResults(); @@ -149,37 +135,55 @@ class CheckboxWidget implements WidgetInterface, FormInterface { $options[$result->getRawValue()] = $text; if ($result->isActive()) { - $form[$facet->getFieldAlias()]['#default_value'] = $result->getRawValue( - ); + $form[$facet->getFieldAlias()]['#default_value'] = $result->getRawValue(); } } $form[$facet->getFieldAlias()]['#options'] = $options; + $form[$facet->id() . '_submit'] = [ + '#type' => 'submit', + '#value' => 'submit', + ]; + return $form; } /** - * Form validation handler. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. + * {@inheritdoc} */ - public function validateForm(array &$form, FormStateInterface $form_state) { - // TODO: Implement validateForm() method. - } + public function validateForm(array &$form, FormStateInterface $form_state) {} /** - * Form submission handler. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. + * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - // TODO: Implement submitForm() method. + $values = $form_state->getValues(); + + /** @var \Drupal\facets\FacetInterface $facet */ + $build_info = $form_state->getBuildInfo(); + $facet = $build_info['args'][0]; + $result_link = FALSE; + + foreach ($values[$facet->getFieldAlias()] as $key => $value) { + if ($value !== 0) { + $active_items[] = $value; + } + } + + foreach ($facet->getResults() as $result) { + if (in_array($result->getRawValue(), $active_items)) { + $result_link = $result->getUrl(); + } + } + + // We have an active item, so redirecting to that page. + if ($result_link instanceof Link) { + return new CacheableRedirectResponse($result_link->toUriString()); + } + + // Form was submitted, but nothing was selected, so we should submit to the + // facet. + return new CacheableRedirectResponse('/' . $facet->getFacetSource()->getPath()); } }