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'],
      ],
    ];
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

willzyx created an issue. See original summary.

willzyx’s picture

Status: Active » Needs review
FileSize
22.47 KB
willzyx’s picture

oops removed some unrelated changes

willzyx’s picture

fgm’s picture

Actually 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?

moshe weitzman’s picture

Status: Needs review » Fixed

Its working, and code is more modular. Core is welcome to borrow it.

willzyx’s picture

@fgm I think that this is a great idea.
Let's open a feature request in drupal core for this

fgm’s picture

@willzyx I can't find the followup issue, did you create it ?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.