Skip to content

Commit

Permalink
Add caching contexts to list builder
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcube committed Dec 3, 2024
1 parent 70c91d3 commit 007d167
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 3 additions & 5 deletions web/modules/mof/src/Form/ModelSearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ public function getFormId() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, string $collection_name = NULL) {
public function buildForm(array $form, FormStateInterface $form_state) {
$req = $this->request;

$form['collection'] = $collection_name;

$form['search'] = [
'#type' => 'details',
'#title' => $this->t('Search filters'),
Expand Down Expand Up @@ -95,14 +93,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
unset($query['org']);
}

$form_state->setRedirect($form['collection'], $query);
$form_state->setRedirect($this->request->attributes->get('_route'), $query);
}

/**
* Reset the form.
*/
public function resetForm(array $form, FormStateInterface $form_state) {
$form_state->setRedirect($form['collection']);
$form_state->setRedirect($this->request->attributes->get('_route'));
}

}
Expand Down
13 changes: 12 additions & 1 deletion web/modules/mof/src/ModelAdminListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,23 @@ public function render(): array|RedirectResponse {

$build = parent::render();
$build['#attached']['library'][] = 'mof/model-list';
$build['search'] = $this->formBuilder->getForm('\Drupal\mof\Form\ModelSearchForm', 'entity.model.admin_collection');
$build['search'] = $this->formBuilder->getForm('\Drupal\mof\Form\ModelSearchForm');
$build['search']['#weight'] = -100;
$build['table']['#attributes']['class'][] = 'tablesaw';
$build['table']['#attributes']['class'][] = 'tablesaw-stack';
$build['table']['#attributes']['data-tablesaw-mode'] = 'stack';

$build['#cache'] = [
'contexts' => [
'url.query_args:label',
'url.query_args:org',
'url.query_args:page',
'url.query_args:limit',
'url.query_args:sort',
'url.query_args:order',
],
];

return $build;
}

Expand Down
13 changes: 12 additions & 1 deletion web/modules/mof/src/ModelListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,23 @@ public function render(): array|RedirectResponse {

$build = parent::render();
$build['#attached']['library'][] = 'mof/model-list';
$build['search'] = $this->formBuilder->getForm('\Drupal\mof\Form\ModelSearchForm', 'entity.model.collection');
$build['search'] = $this->formBuilder->getForm('\Drupal\mof\Form\ModelSearchForm');
$build['search']['#weight'] = -100;
$build['table']['#attributes']['class'][] = 'tablesaw';
$build['table']['#attributes']['class'][] = 'tablesaw-stack';
$build['table']['#attributes']['data-tablesaw-mode'] = 'stack';

$build['#cache'] = [
'contexts' => [
'url.query_args:label',
'url.query_args:org',
'url.query_args:page',
'url.query_args:limit',
'url.query_args:sort',
'url.query_args:order',
],
];

return $build;
}

Expand Down

0 comments on commit 007d167

Please sign in to comment.