Skip to content

Commit

Permalink
EWPP-2838: Date status processor specific sort alter field.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Jan 11, 2023
1 parent f9f42d5 commit 2e932bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/Plugin/facets/processor/DateStatusProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public function build(FacetInterface $facet, array $results) {
return $facet_results;
}

/**
* {@inheritdoc}
*/
public function preQuery(FacetInterface $facet) {
parent::preQuery($facet);

$sort_alter_field_identifier = $this->getConfiguration()['sort_alter_field_identifier'];
if ($sort_alter_field_identifier) {
// Store the value onto the facet so that we can use it at the query
// stage.
$facet->set('default_status_sort_alter_field_identifier', $sort_alter_field_identifier);
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -88,6 +102,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#default_value' => $this->getConfiguration()['past_label'],
];

$build['sort_alter_field_identifier'] = [
'#type' => 'textfield',
'#title' => $this->t('Sort alter field identifier'),
'#description' => $this->t('The field to be used for altering the sort. This is if the default sort for the query is different than the one used in this facet. Leave empty to use the one in the facet.'),
'#default_value' => $this->getConfiguration()['sort_alter_field_identifier'],
];

return $build;
}

Expand All @@ -98,6 +119,7 @@ public function defaultConfiguration() {
return [
'upcoming_label' => $this->t('Upcoming'),
'past_label' => $this->t('Past'),
'sort_alter_field_identifier' => NULL,
] + parent::defaultConfiguration();
}

Expand Down
14 changes: 10 additions & 4 deletions src/Plugin/facets/query_type/DateStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,23 @@ public function execute() {
$filter = $query->createConditionGroup('OR', ['facet:' . $field_identifier]);
foreach ($active_items as $value) {
if ($value === self::PAST) {
$filter->addCondition($this->facet->getFieldIdentifier(), $now->getTimestamp(), "<=");
$filter->addCondition($field_identifier, $now->getTimestamp(), "<=");
}
elseif ($value === self::UPCOMING) {
$condition_group = $query->createConditionGroup('OR');
$condition_group->addCondition($this->facet->getFieldIdentifier(), $now->getTimestamp(), ">");
$condition_group->addCondition($this->facet->getFieldIdentifier(), NULL);
$condition_group->addCondition($field_identifier, $now->getTimestamp(), ">");
$condition_group->addCondition($field_identifier, NULL);
$filter->addConditionGroup($condition_group);
}
}
$query->addConditionGroup($filter);
$this->applySort($query, $active_items, $this->facet->getFieldIdentifier());
// Apply the sort using the field identifier of the current facet or the
// overridden one that was configured on the corresponding processor.
$sort_field_identifier = $field_identifier;
if ($this->facet->get('default_status_sort_alter_field_identifier')) {
$sort_field_identifier = $this->facet->get('default_status_sort_alter_field_identifier');
}
$this->applySort($query, $active_items, $sort_field_identifier);
}
}

Expand Down

0 comments on commit 2e932bb

Please sign in to comment.