diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 6502bba..ff2220d 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -50,6 +50,7 @@ class CRUDComponent extends Component $optionFilters[] = "{$filter} !="; } $params = $this->Controller->ParamHandler->harvestParams($optionFilters); + $params = $this->fakeContextFilter($options, $params); $query = $this->Table->find(); if (!empty($options['filterFunction'])) { $query = $options['filterFunction']($query); @@ -1271,6 +1272,28 @@ class CRUDComponent extends Component $this->Controller->set('filteringContexts', $filteringContexts); } + /** + * Create a fake filtering label set to the filter to be used by default if the request does not supply one + * This fake filtering label will then be used to set approriate filters on the query + * + * @param array $options CRUD options + * @param array $params Collected params from the request + * @return array + */ + protected function fakeContextFilter($options, $params): array + { + if (empty($params['filteringLabel']) && !empty($options['contextFilters']['custom'])) { + foreach ($options['contextFilters']['custom'] as $contextFilter) { + if (!empty($contextFilter['default'])) { + $params['filteringLabel'] = $contextFilter['label']; + $this->Controller->set('fakeFilteringLabel', $contextFilter['label']); + break; + } + } + } + return $params; + } + public function setParentConditionsForMetaFields($query, array $metaConditions) { $metaTemplates = $this->MetaFields->MetaTemplates->find('list', [ diff --git a/src/Controller/MetaTemplatesController.php b/src/Controller/MetaTemplatesController.php index 10a014d..0a863e0 100644 --- a/src/Controller/MetaTemplatesController.php +++ b/src/Controller/MetaTemplatesController.php @@ -254,7 +254,7 @@ class MetaTemplatesController extends AppController 'custom' => [ [ 'default' => true, - 'label' => __('Newest Templates'), + 'label' => __('Latest Templates'), 'filterConditionFunction' => function ($query) { return $query->where([ 'id IN' => $this->MetaTemplates->genQueryForAllNewestVersionIDs() diff --git a/templates/element/genericElements/ListTopBar/group_context_filters.php b/templates/element/genericElements/ListTopBar/group_context_filters.php index c7cc3ed..e441515 100644 --- a/templates/element/genericElements/ListTopBar/group_context_filters.php +++ b/templates/element/genericElements/ListTopBar/group_context_filters.php @@ -9,6 +9,7 @@ ]; $currentQuery = $this->request->getQuery(); $filteringLabel = !empty($currentQuery['filteringLabel']) ? $currentQuery['filteringLabel'] : ''; + $fakeFilteringLabel = !empty($fakeFilteringLabel) ? $fakeFilteringLabel : false; unset($currentQuery['page'], $currentQuery['limit'], $currentQuery['sort'], $currentQuery['filteringLabel']); if (!empty($filteringContext['filterCondition'])) { // PHP replaces `.` by `_` when fetching the request parameter $currentFilteringContext = []; @@ -25,9 +26,11 @@ ( $currentQuery == $currentFilteringContext && // query conditions match !isset($filteringContext['filterConditionFunction']) && // not a custom filtering - empty($filteringLabel) // do not check `All` by default + empty($filteringLabel) && // do not check `All` by default + empty($fakeFilteringLabel) // no custom filter is a default filter ) || - $filteringContext['label'] == $filteringLabel // labels should not be duplicated + $filteringContext['label'] == $filteringLabel || // labels should not be duplicated + $filteringContext['label'] == $fakeFilteringLabel // use the default filter ), 'isFilter' => true, 'onClick' => 'changeIndexContext',