Commit 521df791 authored by Jur de Vries's avatar Jur de Vries

Issue #2349459 by JeroenT: Create facetapi_sort plugin.

parent 663a70a6
......@@ -2,3 +2,6 @@ services:
facetapi.adapter:
class: Drupal\facetapi\Adapter
arguments: []
plugin.manager.facetapi.sort:
class: Drupal\facetapi\FacetApiSortManager
parent: default_plugin_manager
<?php
/**
* @file
* Contains \Drupal\facetapi\Annotation\FacetApiSort.
*/
namespace Drupal\facetapi\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Facet API Sorter annotation object.
*
* @see \Drupal\facetapi\FacetApiSortManager
* @see plugin_api
*
* @Annotation
*/
class FacetApiSort extends Plugin {
/**
* The sorter plugin id
*
* @var string
*/
public $id;
/**
* The human-readable name of the sorter plugin
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* The sorter description.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
}
<?php
/**
* @file
* Provides Drupal/Sort/FacetApiSortBase.
*/
namespace Drupal\facetapi\Sort;
use Drupal\Component\Plugin\PluginBase;
class FacetApiSortBase extends PluginBase implements FacetApiSortInterface {
private $weight;
/**
* Returns the id of the facetapi sort.
*
* @return mixed
*/
public function getId() {
return $this->pluginDefinition['id'];
}
/**
* Returns the label of the facetapi sort.
*
* @return string
*/
public function getLabel() {
return $this->pluginDefinition['label'];
}
/**
* Returns the description of the facetapi sort.
*
* @return string
*/
public function getDescription() {
return $this->pluginDefinition['description'];
}
/**
* Gets the weight of the facetapi sort.
*
* @return int|string
*/
public function getWeight() {
return $this->weight;
}
/**
* Sets the weight of the facetapi sort.
*
* @param int $weight
* @return $this
*/
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
public function sort(array $a, array $b) {
//Nothing to do here...
}
}
<?php
/**
* @file
* Contains \Drupal\image\ImageEffectInterface.
*/
namespace Drupal\facetapi\Sort;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
/**
* Defines the interface for image effects.
*
* @see plugin_api
*/
interface FacetApiSortInterface extends PluginInspectionInterface {
/**
* Returns the id of the facetapi sort.
*
* @return mixed
*/
public function getId();
/**
* Returns the label of the facetapi sort.
*
* @return string
*/
public function getLabel();
/**
* Returns the description of the facetapi sort.
*
* @return string
*/
public function getDescription();
/**
* Returns the weight of the facetapi sort.
*
* @return int|string
* Either the integer weight of the facetapi sort, or an empty string.
*/
public function getWeight();
/**
* Sets the weight for this facetapi sort.
*
* @param int $weight
* The weight for this facetapi sort.
*
* @return $this
*/
public function setWeight($weight);
/**
* Function that actually sorts the facetapi results.
*
* @param array $a
* @param array $b
* @return mixed
*/
public function sort(array $a, array $b);
}
<?php
/**
* @file
* Contains \Drupal\facetapi\FacetapiSortManager.
*/
namespace Drupal\facetapi\Sort;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Manages facetapi sort plugins.
*
* @see plugin_api
*/
class FacetApiSortManager extends DefaultPluginManager {
/**
* Constructs a new FacetapiSortManager.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Sort', $namespaces, $module_handler, 'Drupal\facetapi\FacetApiSortInterface', 'Drupal\facetapi\Annotation\FacetApiSort');
$this->alterInfo('facetapi_sort_info');
$this->setCacheBackend($cache_backend, 'facetapi_sort_plugins');
}
}
<?php
namespace Drupal\facetapi\Plugin\FacetApi\Sort;
use Drupal\Core\Annotation\Translation;
use Drupal\facetapi\Annotation\FacetApiSort;
use Drupal\facetapi\Sort\FacetApiSortBase;
/**
* @FacetApiSort(
* id = "facetapi_sort_active",
* label = @Translation("Facet Active"),
* description = @Translation("Sort by whether the facet is active or not.")
* )
*/
class SortActive extends FacetApiSortBase {
public function sort(array $a, array $b) {
$a_active = (isset($a['#active'])) ? $a['#active'] : 0;
$b_active = (isset($b['#active'])) ? $b['#active'] : 0;
if ($a_active == $b_active) {
return 0;
}
return ($a_active < $b_active) ? -1 : 1;
}
}
<?php
namespace Drupal\facetapi\Plugin\FacetApi\Sort;
use Drupal\Core\Annotation\Translation;
use Drupal\facetapi\Annotation\FacetApiSort;
use Drupal\facetapi\Sort\FacetApiSortBase;
/**
* @FacetApiSort(
* id = "facetapi_sort_count",
* label = @Translation("Count"),
* description = @Translation("Sort by the facet count.")
* )
*/
class SortCount extends FacetApiSortBase {
public function sort(array $a, array $b) {
$a_count = (isset($a['#count'])) ? $a['#count'] : 0;
$b_count = (isset($b['#count'])) ? $b['#count'] : 0;
if ($a_count == $b_count) {
return 0;
}
return ($a_count < $b_count) ? -1 : 1;
}
}
<?php
namespace Drupal\facetapi\Plugin\FacetApi\Sort;
use Drupal\Core\Annotation\Translation;
use Drupal\facetapi\Annotation\FacetApiSort;
use Drupal\facetapi\Sort\FacetApiSortBase;
/**
* @FacetApiSort(
* id = "facetapi_sort_display",
* label = @Translation("Display value"),
* description = @Translation("Sort by the value displayed to the user.")
* )
*/
class SortDisplay extends FacetApiSortBase {
public function sort(array $a, array $b) {
return strcasecmp($a['#markup'], $b['#markup']);
}
}
<?php
namespace Drupal\facetapi\Plugin\FacetApi\Sort;
use Drupal\Core\Annotation\Translation;
use Drupal\facetapi\Annotation\FacetApiSort;
use Drupal\facetapi\Sort\FacetApiSortBase;
/**
* @FacetApiSort(
* id = "facetapi_sort_indexed",
* label = @Translation("Indexed value"),
* description = @Translation("Sort by the raw value stored in the index.")
* )
*/
class SortIndexed extends FacetApiSortBase {
public function sort(array $a, array $b) {
$a_value = (isset($a['#indexed_value'])) ? $a['#indexed_value'] : '';
$b_value = (isset($b['#indexed_value'])) ? $b['#indexed_value'] : '';
if ($a_value == $b_value) {
return 0;
}
return ($a_value < $b_value) ? -1 : 1;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment