Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently a lot of functionalities of devel that use tables with on-the-fly filtering have a lot of boilerplate code repeated for handle the table filtering.
public function parameterList() {
$headers = [..];
$rows = [];
// .... populate rows
$output['#attached']['library'][] = 'system/drupal.system.modules';
$output['filters'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['table-filter', 'js-show'],
],
];
$output['filters']['text'] = [
'#type' => 'search',
'#title' => $this->t('Search'),
'#size' => 30,
'#placeholder' => $this->t('Enter parameter name'),
'#attributes' => [
'class' => ['table-filter-text'],
'data-table' => '.devel-filter-text',
'autocomplete' => 'off',
'title' => $this->t('Enter a part of the parameter name to filter by.'),
],
];
$output['parameters'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this->t('No parameters found.'),
'#sticky' => TRUE,
'#attributes' => [
'class' => ['devel-parameter-list', 'devel-filter-text'],
],
];
return $output;
}
We can create a custom element that handle this aspect and reduce the boilerplate code, centralize the behavior and reduce errors.
The result could be something like
public function parameterList() {
$headers = [..];
$rows = [];
// .... populate rows
$output['parameters'] = [
'#type' => 'devel_table_filter',
'#filter_label' => $this->t('Search'),
'#filter_placeholder' => $this->t('Enter parameter name'),
'#filter_description' => $this->t('Enter a part of the parameter name to filter by.'),
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this->t('No parameters found.'),
'#sticky' => TRUE,
'#attributes' => [
'class' => ['devel-parameter-list'],
],
];
}
Comment | File | Size | Author |
---|---|---|---|
#4 | interdiff.txt | 8.06 KB | willzyx |
#4 | create-custom-element-3048060-4.patch | 25.56 KB | willzyx |
| |||
#3 | create-custom-element-3048060-2.patch | 19.69 KB | willzyx |
| |||
#2 | create-custom-element-3048060-1.patch | 22.47 KB | willzyx |
|
Comments
Comment #2
willzyx CreditAttribution: willzyx commentedComment #3
willzyx CreditAttribution: willzyx commentedoops removed some unrelated changes
Comment #4
willzyx CreditAttribution: willzyx commentedAdd some tests
Comment #5
fgmActually this is not really devel-specific. I had to do pretty much the same a few days ago on custom code.
Maybe it would be best to think this from the start as a potential core target, with devel usage being just a specialization?
Comment #7
moshe weitzman CreditAttribution: moshe weitzman commentedIts working, and code is more modular. Core is welcome to borrow it.
Comment #8
willzyx CreditAttribution: willzyx commented@fgm I think that this is a great idea.
Let's open a feature request in drupal core for this
Comment #9
fgm@willzyx I can't find the followup issue, did you create it ?