diff --git a/src/Plugin/better_exposed_filters/filter/Facets.php b/src/Plugin/better_exposed_filters/filter/Facets.php index 0f059e685070efdf6aad6e79ad48f7304f962840..d642956e05a4360a0039b89982df789abab5a977 100644 --- a/src/Plugin/better_exposed_filters/filter/Facets.php +++ b/src/Plugin/better_exposed_filters/filter/Facets.php @@ -20,7 +20,7 @@ class Facets extends FilterWidgetBase { * {@inheritdoc} */ public function exposedFormAlter(array &$form, FormStateInterface $form_state) { - if ($form_state->facets_not_built ?? FALSE) { + if ($form_state->get('facets_not_built') ?? FALSE) { return; } diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index c1485806d5b69b7f7e640799b46be4229b83b82b..f92f46c2e9e9a5d020898497c184c3a117e9973e 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -68,18 +68,18 @@ class DisplayValueWidgetOrderProcessor extends SortProcessorPluginBase implement */ public function sortResults(Result $a, Result $b) { // Get the transliterate values only once. - if (!isset($a->transliterateDisplayValue)) { - $a->transliterateDisplayValue = $this->transliteration->removeDiacritics($a->getDisplayValue()); + if (!$a->get('transliterateDisplayValue')) { + $a->set('transliterateDisplayValue', $this->transliteration->removeDiacritics($a->getDisplayValue())); } - if (!isset($b->transliterateDisplayValue)) { - $b->transliterateDisplayValue = $this->transliteration->removeDiacritics($b->getDisplayValue()); + if (!$b->get('transliterateDisplayValue')) { + $b->set('transliterateDisplayValue', $this->transliteration->removeDiacritics($b->getDisplayValue())); } // Return the sort value. - if ($a->transliterateDisplayValue == $b->transliterateDisplayValue) { + if ($a->get('transliterateDisplayValue') == $b->get('transliterateDisplayValue')) { return 0; } - return strnatcasecmp($a->transliterateDisplayValue, $b->transliterateDisplayValue); + return strnatcasecmp($a->get('transliterateDisplayValue'), $b->get('transliterateDisplayValue')); } } diff --git a/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php b/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php index bedfb2ba26add593c25f3ebe06614ad6a2d11d87..3b56512a1bb30e70742938dd50315e4896703910 100644 --- a/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php @@ -71,38 +71,38 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements */ public function sortResults(Result $a, Result $b) { // Get the term weight once. - if (!isset($a->termWeight) || !isset($b->termWeight)) { + if ($a->get('termWeight') === NULL || $b->get('termWeight') === NULL) { $ids = []; - if (!isset($a->termWeight)) { + if ($a->get('termWeight') === NULL) { $a_raw = $a->getRawValue(); $ids[] = $a_raw; } - if (!isset($b->termWeight)) { + if ($b->get('termWeight') === NULL) { $b_raw = $b->getRawValue(); $ids[] = $b_raw; } $entities = $this->entityTypeManager ->getStorage('taxonomy_term') ->loadMultiple($ids); - if (!isset($a->termWeight)) { + if ($a->get('termWeight') === NULL) { if (empty($entities[$a_raw])) { return 0; } - $a->termWeight = $entities[$a_raw]->getWeight(); + $a->set('termWeight', $entities[$a_raw]->getWeight()); } - if (!isset($b->termWeight)) { + if ($b->get('termWeight') === NULL) { if (empty($entities[$b_raw])) { return 0; } - $b->termWeight = $entities[$b_raw]->getWeight(); + $b->set('termWeight', $entities[$b_raw]->getWeight()); } } // Return the sort value. - if ($a->termWeight === $b->termWeight) { + if ($a->get('termWeight') === $b->get('termWeight')) { return 0; } - return ($a->termWeight < $b->termWeight) ? -1 : 1; + return ($a->get('termWeight') < $b->get('termWeight')) ? -1 : 1; } /** diff --git a/src/Plugin/views/filter/FacetsFilter.php b/src/Plugin/views/filter/FacetsFilter.php index bd44301da27ce845172c0022a3abb08dc4b112a2..186626df364ead46ad4e06cf08e472ed1de932c3 100644 --- a/src/Plugin/views/filter/FacetsFilter.php +++ b/src/Plugin/views/filter/FacetsFilter.php @@ -111,7 +111,7 @@ class FacetsFilter extends FilterPluginBase { static $is_processing = NULL; if ($is_processing) { - $form_state->facets_not_built = TRUE; + $form_state->set('facets_not_built', TRUE); $form['value'] = []; return; } diff --git a/src/Result/Result.php b/src/Result/Result.php index 334dc68559ba310b8743a188a66a2f1075e059b1..40a0c2e578afd9984fea7c47de9efbb42b07ca18 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -2,6 +2,7 @@ namespace Drupal\facets\Result; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Url; use Drupal\facets\FacetInterface; @@ -10,6 +11,13 @@ use Drupal\facets\FacetInterface; */ class Result implements ResultInterface { + /** + * The facet transliterate display value. + * + * @var string + */ + public $transliterateDisplayValue; + /** * The facet related to the result. * @@ -73,6 +81,13 @@ class Result implements ResultInterface { */ protected $children = []; + /** + * Storage for implementation-specific data. + * + * @var array + */ + protected $storage = []; + /** * Constructs a new result value object. * @@ -221,4 +236,34 @@ class Result implements ResultInterface { return $this->facet; } + /** + * {@inheritdoc} + */ + public function getStorage() { + return $this->storage; + } + + /** + * {@inheritdoc} + */ + public function setStorage(array $storage) { + $this->storage = $storage; + return $this; + } + + /** + * {@inheritdoc} + */ + public function get($property) { + return NestedArray::getValue($this->storage, (array) $property); + } + + /** + * {@inheritdoc} + */ + public function set($property, $value) { + NestedArray::setValue($this->storage, (array) $property, $value, TRUE); + return $this; + } + } diff --git a/src/Result/ResultInterface.php b/src/Result/ResultInterface.php index 3e23b638e0cafa88fa36d443109f164f15043b97..36fbf18374833a90dd423dbbbfb9539f07c4ea41 100644 --- a/src/Result/ResultInterface.php +++ b/src/Result/ResultInterface.php @@ -145,4 +145,52 @@ interface ResultInterface { */ public function getChildren(); + /** + * Returns the entire set of arbitrary data. + * + * @return array + * The entire set of arbitrary data storage for this result. + */ + public function getStorage(); + + /** + * Sets the entire set of arbitrary data. + * + * @param array $storage + * The entire set of arbitrary data to store for this result. + * + * @return $this + */ + public function setStorage(array $storage); + + /** + * Gets any arbitrary property. + * + * @param string|array $property + * Properties are often stored as multi-dimensional associative arrays. If + * $property is a string, it will return $storage[$property]. If $property + * is an array, each element of the array will be used as a nested key. If + * $property = ['foo', 'bar'] it will return $storage['foo']['bar']. + * + * @return mixed + * The property, or the default if the property does not exist. + */ + public function get($property); + + /** + * Sets a value to an arbitrary property. + * + * @param string|array $property + * Properties are often stored as multi-dimensional associative arrays. If + * $property is a string, it will use $storage[$property] = $value. If + * $property is an array, each element of the array will be used as a nested + * key. If $property = ['foo', 'bar'] it will use + * $storage['foo']['bar'] = $value. + * @param mixed $value + * The value to set. + * + * @return $this + */ + public function set($property, $value); + } diff --git a/tests/src/Unit/Result/ResultTest.php b/tests/src/Unit/Result/ResultTest.php index b70c8449b1ffa6701a2b56f41bdaed50307ed630..e52c955263a659b8694c86de436ee8a393d30ae3 100644 --- a/tests/src/Unit/Result/ResultTest.php +++ b/tests/src/Unit/Result/ResultTest.php @@ -43,6 +43,15 @@ class ResultTest extends UnitTestCase { $url = new Url('foo'); $result->setUrl($url); $this->assertSame($url, $result->getUrl()); + + $property = ['foo', 'bar']; + $value = 'baz'; + $result->set($property, $value); + $this->assertSame($value, $result->get($property)); + + $storage = ['foo' => ['bar']]; + $result->setStorage($storage); + $this->assertSame($storage, $result->getStorage()); } }