Skip to content

Instantly share code, notes, and snippets.

@nonom
Created July 19, 2022 13:17
Show Gist options
  • Save nonom/754abdab72f41df351b87512b2ee56c6 to your computer and use it in GitHub Desktop.
Save nonom/754abdab72f41df351b87512b2ee56c6 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types = 1);
namespace Drupal\upd_list_pages_autocomplete\Plugin\facets\widget;
use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\ResultInterface;
use Drupal\oe_list_pages\FacetManipulationTrait;
use Drupal\oe_list_pages\Plugin\facets\widget\ListPagesWidgetBase;
/**
* A widget that provides a textfield for autocomplete facet results.
*
* @FacetsWidget(
* id = "upd_list_pages_textfield_autocomplete",
* label = @Translation("List pages textfield autocomplete"),
* description= @Translation("An autocomplete textfield widget for oe_list_pages."),
* )
*/
class AutocompleteTextfieldWidget extends ListPagesWidgetBase {
use FacetManipulationTrait;
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function build(FacetInterface $facet) {
/** @var \Drupal\facets\Result\Result[] $results */
$results = $facet->getResults();
$items = $autocomplete_values = $automplete_urls = [];
$configuration = $facet->getWidget()['config'];
$this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
foreach ($results as $result) {
if (is_null($result->getUrl())) {
$text = $this->generateValues($result);
$items[$facet->getFieldIdentifier()][] = $text;
}
else {
$autocomplete_values[$result->getRawValue()] = $result->getDisplayValue() . ($this->showNumbers ? ' (' . $result->getCount() . ')' : '');
$url = $result->getUrl()->toString();
$automplete_urls[$result->getDisplayValue() . ($this->showNumbers ? ' (' . $result->getCount() . ')' : '')] = $url;
}
}
// Get reset URL, if option is checked.
$reset_url = NULL;
if ($configuration['show_reset_link']
&& (!$configuration['hide_reset_when_no_selection'] || $facet->getActiveItems())
) {
$urlProcessorManager = \Drupal::service('plugin.manager.facets.url_processor');
$url_processor = $urlProcessorManager->createInstance($facet->getFacetSourceConfig()
->getUrlProcessorName(), ['facet' => $facet]);
$request = \Drupal::request();
$reset_url = Url::createFromRequest($request);
$params = $request->query->all();
unset($params[$url_processor->getFilterKey()]);
$reset_url->setOption('query', $params);
$reset_url = $reset_url->toString();
}
$active = $facet->getActiveItems();
$default_value = '';
if (isset($active[0])) {
$default_value = $autocomplete_values[$active[0]] ?? '';
}
$default_value = $this->getValueFromActiveFilters($facet, '0');
// Add librarys and everything to js.
$build['#attached']['library'][] = 'upd_list_pages_autocomplete/drupal.upd_list_pages_autocomplete.autocomplete';
$build['#attached']['drupalSettings']['upd_list_pages_autocomplete']['autocomplete_widget'][$facet->id()] = [
'results' => $autocomplete_values,
'urls' => $automplete_urls,
'default_value' => $default_value,
'reset_url' => $reset_url,
];
$build[$facet->id()] = [
'#type' => 'textfield',
'#title' => $facet->label(),
'#attributes' => [
'class' => ['autocomplete-facet'],
'data-id' => [$facet->id()],
'id' => [$facet->id()],
],
'#default_value' => $default_value,
];
return $build;
}
/**
* Prepares the URL and values for the facet.
*
* @param \Drupal\facets\Result\ResultInterface $result
* A result item.
*
* @return array
* The results.
*/
protected function prepare(ResultInterface $result) {
$values = $this->generateValues($result);
if (is_null($result->getUrl())) {
$facet_values = $values;
}
else {
$facet_values['url'] = $result->getUrl()->setAbsolute()->toString();
$facet_values['values'] = $values;
}
return $facet_values;
}
/**
* Generates the value and the url.
*
* @param \Drupal\facets\Result\ResultInterface $result
* The result to extract the values.
*
* @return array
* The values.
*/
protected function generateValues(ResultInterface $result) {
$values['value'] = $result->getDisplayValue();
if ($this->getConfiguration()['show_numbers'] && $result->getCount() !== FALSE) {
$values['count'] = $result->getCount();
}
if ($result->isActive()) {
$values['active'] = 'TRUE';
}
return $values;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment