diff --git a/src/Plugin/facets/widget/CheckboxWidget.php b/src/Plugin/facets/widget/CheckboxWidget.php index 685ffea..db1735e 100644 --- a/src/Plugin/facets/widget/CheckboxWidget.php +++ b/src/Plugin/facets/widget/CheckboxWidget.php @@ -7,8 +7,11 @@ namespace Drupal\facets\Plugin\facets\widget; +use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\facets\Exception\Exception; use Drupal\facets\FacetInterface; use Drupal\facets\Widget\WidgetInterface; @@ -21,7 +24,7 @@ use Drupal\facets\Widget\WidgetInterface; * description = @Translation("A configurable widget that shows a list of checkboxes"), * ) */ -class CheckboxWidget implements WidgetInterface { +class CheckboxWidget implements WidgetInterface, FormInterface { use StringTranslationTrait; @@ -34,6 +37,11 @@ class CheckboxWidget implements WidgetInterface { protected $linkGenerator; /** + * @var \Drupal\facets\FacetInterface + */ + protected $facet; + + /** * {@inheritdoc} */ public function execute() { @@ -44,33 +52,12 @@ class CheckboxWidget implements WidgetInterface { * {@inheritdoc} */ public function build(FacetInterface $facet) { - /** @var \Drupal\facets\Result\Result[] $results */ - $results = $facet->getResults(); - $items = []; - $configuration = $facet->getWidgetConfigs(); - $show_numbers = (bool) $configuration['show_numbers']; + $this->facet = $facet; - foreach ($results as $result) { - if ($result->getCount()) { - // Get the link. - $text = $result->getDisplayValue(); - if ($show_numbers) { - $text .= ' (' . $result->getCount() . ')'; - } - if ($result->isActive()) { - $text = '(-) ' . $text; - } - $link = $this->linkGenerator()->generate($text, $result->getUrl()); - $items[] = $link; - } - } - $build = [ - '#theme' => 'item_list', - '#items' => $items, - ]; + $formBuilder = \Drupal::getContainer()->get('form_builder'); + $build = $formBuilder->getForm(static::class, new FormState()); - $build['#prefix'] = $this->t('Checkboxes'); return $build; } @@ -114,4 +101,85 @@ class CheckboxWidget implements WidgetInterface { return $this->linkGenerator; } + /** + * Returns a unique string identifying the form. + * + * @return string + * The unique string identifying the form. + */ + public function getFormId() { + return '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. + */ + 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\Result\Result[] $results */ + $results = $facet->getResults(); + + $configuration = $facet->getWidgetConfigs(); + $show_numbers = (bool) $configuration['show_numbers']; + $form[$facet->getFieldAlias()] = [ + '#type' => 'checkboxes', + '#title' => $facet->getName(), + ]; + + $options = array(); + foreach ($results as $result) { + $text = $result->getDisplayValue(); + if ($show_numbers) { + $text .= ' (' . $result->getCount() . ')'; + } + + $options[$result->getRawValue()] = $text; + + if ($result->isActive()) { + $form[$facet->getFieldAlias()]['#default_value'] = $result->getRawValue( + ); + } + } + + $form[$facet->getFieldAlias()]['#options'] = $options; + + 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. + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + // TODO: Implement validateForm() method. + } + + /** + * 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. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // TODO: Implement submitForm() method. + } }