View source
<?php
namespace Drupal\search_api_glossary\Plugin\facets\processor;
use Drupal\facets\Processor\SortProcessorPluginBase;
use Drupal\facets\Processor\SortProcessorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\Result;
class GlossaryAZWidgetOrderProcessor extends SortProcessorPluginBase implements SortProcessorInterface {
public function sortResults(Result $a, Result $b) {
$group_a = $this
->getResultGroup($a);
$group_b = $this
->getResultGroup($b);
if ($group_a == $group_b) {
return strnatcasecmp($a
->getRawValue(), $b
->getRawValue());
}
else {
$sort_options_by_weight = $this
->sortConfigurationWeight($this
->getConfiguration()['sort']);
return $sort_options_by_weight[$group_a] < $sort_options_by_weight[$group_b] ? -1 : 1;
}
}
private function getResultGroup(Result $result) {
if ($result
->getRawValue() == '0-9' || ctype_digit($result
->getRawValue()) || is_int($result
->getRawValue())) {
$group = 'glossaryaz_sort_09';
}
elseif ($result
->getRawValue() == 'All') {
$group = 'glossaryaz_sort_all';
}
elseif (ctype_alpha($result
->getRawValue())) {
$group = 'glossaryaz_sort_az';
}
else {
$group = 'glossaryaz_sort_other';
}
return $group;
}
public function build(FacetInterface $facet, array $results) {
$sort_options_by_weight = $this
->sortConfigurationWeight($this
->getConfiguration()['sort']);
$glossary_results = array();
foreach ($sort_options_by_weight as $sort_option_by_weight_id => $sort_option_by_weight_weight) {
$glossary_results[$sort_option_by_weight_id] = array();
}
foreach ($results as $result) {
if ($result
->getRawValue() == '0-9' || ctype_digit($result
->getRawValue()) || is_int($result
->getRawValue())) {
$glossary_results['glossaryaz_sort_09'][$result
->getRawValue()] = $result;
}
elseif ($result
->getRawValue() == 'All') {
$glossary_results['glossaryaz_sort_all'][$result
->getRawValue()] = $result;
}
elseif (ctype_alpha($result
->getRawValue())) {
$glossary_results['glossaryaz_sort_az'][$result
->getRawValue()] = $result;
}
else {
$glossary_results['glossaryaz_sort_other'][$result
->getRawValue()] = $result;
}
}
ksort($glossary_results['glossaryaz_sort_az']);
ksort($glossary_results['glossaryaz_sort_09']);
ksort($glossary_results['glossaryaz_sort_other']);
$glossary_results_sorted = array();
foreach ($glossary_results as $glossary_result) {
if ($glossary_result) {
$glossary_results_sorted = array_merge($glossary_results_sorted, $glossary_result);
}
}
return $glossary_results_sorted;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$processors = $facet
->getProcessors();
$config = isset($processors[$this
->getPluginId()]) ? $processors[$this
->getPluginId()] : NULL;
$sort_options = !is_null($config) ? $config
->getConfiguration()['sort'] : $this
->defaultConfiguration();
$sort_options_by_weight = $this
->sortConfigurationWeight($sort_options);
$build['sort'] = array(
'#tree' => TRUE,
'#type' => 'table',
'#attributes' => array(
'id' => 'glossaryaz-sort-widget',
),
'#header' => array(
$this
->t('Sort By'),
$this
->t('Weight'),
),
'#tabledrag' => array(
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'glossaryaz-sort-weight',
),
),
);
foreach ($sort_options_by_weight as $sort_option_key => $sort_option_weight) {
$build['sort'][$sort_option_key]['#attributes']['class'][] = 'draggable';
$build['sort'][$sort_option_key]['#attributes']['class'][] = 'glossaryaz-sort-weight--' . $sort_option_key;
$build['sort'][$sort_option_key]['#weight'] = $sort_option_weight;
$build['sort'][$sort_option_key]['sort_by']['#plain_text'] = $this
->defaultConfiguration()[$sort_option_key]['name'];
$build['sort'][$sort_option_key]['weight'] = array(
'#type' => 'weight',
'#delta' => count($this
->defaultConfiguration()),
'#default_value' => $sort_option_weight,
'#attributes' => array(
'class' => array(
'glossaryaz-sort-weight',
),
),
);
}
return $build;
}
public function defaultConfiguration() {
$sort_options_deafult = array(
'glossaryaz_sort_az' => array(
'weight' => 1,
'name' => $this
->t('Alpha (A-Z)'),
),
'glossaryaz_sort_09' => array(
'weight' => 2,
'name' => $this
->t('Numeric (0-9)'),
),
'glossaryaz_sort_other' => array(
'weight' => 3,
'name' => $this
->t('Other (#)'),
),
'glossaryaz_sort_all' => array(
'weight' => -1,
'name' => $this
->t('All'),
),
);
return $sort_options_deafult;
}
public function sortConfigurationWeight($sort_options) {
foreach ($sort_options as $sort_option_id => $sort_option) {
$sort_options_by_weight[$sort_option_id] = $sort_option['weight'];
}
asort($sort_options_by_weight);
return $sort_options_by_weight;
}
}