Skip to content

Commit

Permalink
Merge pull request #56 from lehors/search-admin-collection
Browse files Browse the repository at this point in the history
Add search form to Admin models page
  • Loading branch information
lehors authored Dec 3, 2024
2 parents 933bfa6 + 007d167 commit d7f3f97
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
4 changes: 2 additions & 2 deletions web/modules/mof/src/Form/ModelSearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
unset($query['org']);
}

$form_state->setRedirect('entity.model.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('entity.model.collection');
$form_state->setRedirect($this->request->attributes->get('_route'));
}

}
Expand Down
75 changes: 73 additions & 2 deletions web/modules/mof/src/ModelAdminListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,55 @@

namespace Drupal\mof;

use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Url;
use Drupal\mof\Entity\Model;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class ModelAdminListBuilder extends EntityListBuilder {

use PageLimitTrait;

private $formBuilder;

private $request;

/**
* {@inheritdoc}
*/
public function __construct(
EntityTypeInterface $entity_type,
EntityStorageInterface $storage,
FormBuilderInterface $form_builder,
Request $request
) {
parent::__construct($entity_type, $storage);
$this->formBuilder = $form_builder;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public static function createInstance(
ContainerInterface $container,
EntityTypeInterface $entity_type
) {
return new static(
$entity_type,
$container->get('entity_type.manager')->getStorage($entity_type->id()),
$container->get('form_builder'),
$container->get('request_stack')->getCurrentRequest()
);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -64,6 +102,16 @@ protected function getEntityListQuery(): QueryInterface {
$query = parent::getEntityListQuery();
$query->sort('id', 'DESC');

if ($label = $this->request->get('label')) {
$label = addcslashes($label, '\\%_');
$query->condition('label', "%{$label}%", 'LIKE');
}

if ($org = $this->request->get('org')) {
$org = addcslashes($org, '\\%_');
$query->condition('organization', "%{$org}%", 'LIKE');
}

if ($this->limit) {
$query->pager($this->limit);
}
Expand Down Expand Up @@ -99,7 +147,30 @@ public function buildRow(EntityInterface $entity): array {
* {@inheritdoc}
*/
public function render(): array|RedirectResponse {
return ($url = $this->getPageRedirectUrl()) != NULL ? $this->redirectPage($url) : parent::render();
if (($url = $this->getPageRedirectUrl()) !== NULL) {
return $this->redirectPage($url);
}

$build = parent::render();
$build['#attached']['library'][] = 'mof/model-list';
$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;
}

}
11 changes: 11 additions & 0 deletions web/modules/mof/src/ModelListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public function render(): array|RedirectResponse {
$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 d7f3f97

Please sign in to comment.