You are here

protected function DashboardController::getRenderLocals in Purge 8.3

Helper for dealing with render arrays more easily.

With complexer render arrays with many levels deep - especially in tables - it becomes much easier to read and write using this collection of template variables and closures that return common render array elements.

The variables can be imported like this:

extract($this
  ->getRenderLocals());

Return value

array The render array.

4 calls to DashboardController::getRenderLocals()
DashboardController::buildDiagnosticReport in modules/purge_ui/src/Controller/DashboardController.php
Add a visual report on the current state of the purge module.
DashboardController::buildLoggingSection in modules/purge_ui/src/Controller/DashboardController.php
Add a section devoted to log configuration.
DashboardController::buildPurgers in modules/purge_ui/src/Controller/DashboardController.php
Manage purgers and visualize the types they support.
DashboardController::buildQueuersQueueProcessors in modules/purge_ui/src/Controller/DashboardController.php
Manage queuers, the queue itself and processors.

File

modules/purge_ui/src/Controller/DashboardController.php, line 409

Class

DashboardController
Configuration dashboard for configuring the cache invalidation pipeline.

Namespace

Drupal\purge_ui\Controller

Code

protected function getRenderLocals() {

  // phpcs:disable DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable -- PHP's extract() isn't understood by this sniffer..
  $details = function ($title) {
    return [
      '#type' => 'details',
      '#title' => $title,
      '#open' => TRUE,
    ];
  };
  $fieldset = function ($title) {
    return [
      '#type' => 'fieldset',
      '#title' => $title,
      '#attributes' => [],
    ];
  };

  // Buttons and operation links.
  $url = function ($route) {
    if (is_array($route)) {
      $args = $route;
      $route = array_shift($args);
      return Url::fromRoute($this->routes[$route], $args);
    }
    else {
      return Url::fromRoute($this->routes[$route]);
    }
  };
  $button = function ($title, $route, $width = '60%') use ($url) {
    return [
      'title' => $title,
      'url' => $url($route),
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => $width,
        ]),
      ],
    ];
  };
  $buttonlink = function ($title, $route, $width = '60%') use ($url) {
    return [
      '#type' => 'link',
      '#title' => $title,
      '#url' => $url($route),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => $width,
        ]),
      ],
    ];
  };

  // Table management.
  $table = function ($header = []) {
    return [
      '#type' => 'table',
      '#responsive' => TRUE,
      '#header' => $header,
    ];
  };
  $cell = function ($cell = []) {
    return [
      'data' => $cell,
    ];
  };
  $cell_checked = function ($title = '') use ($cell) {
    return $cell([
      '#theme' => 'image',
      '#width' => 18,
      '#height' => 18,
      '#uri' => 'core/misc/icons/73b355/check.svg',
      '#alt' => $title,
      '#title' => $title,
    ]);
  };
  $cell_markup = function ($markup) use ($cell) {
    return $cell([
      '#markup' => $markup,
    ]);
  };
  $cell_spacer = $cell_markup(' ');
  $cell_ops = function ($links) use ($cell) {
    return $cell([
      '#type' => 'operations',
      '#links' => $links,
    ]);
  };
  $col_equalize = function (&$cols, $extrarows = 0) use ($cell_spacer) {
    $rowstotal = 1;
    foreach ($cols as $col => $rows) {
      if (($rowcount = count($rows)) > $rowstotal) {
        $rowstotal = $rowcount;
      }
    }
    $rowstotal = $rowstotal + $extrarows;
    foreach ($cols as $col => $rows) {
      if ($missing = $rowstotal - count($rows)) {
        for ($i = 0; $i < $missing; $i++) {
          $cols[$col][] = $cell_spacer;
        }
      }
    }
  };
  $row_isset = function ($table, $row) {
    return isset($table['#rows'][$row]);
  };
  $row_set = function (&$table, $row, $col, $value) {
    $table['#rows'][$row]['data'][$col] = $value;
  };
  $row_new = function (&$table, $row) use ($cell_spacer, $row_set) {
    foreach ($table['#header'] as $col => $definition) {
      $row_set($table, $row, $col, $cell_spacer);
    }
  };

  // Simple shorthand tag wrapping closures.
  $tag = function ($tag, $content) {
    return '<' . $tag . '>' . $content . '</' . $tag . '>';
  };
  $b = function ($content) use ($tag) {
    return $tag('b', $content);
  };
  $i = function ($content) use ($tag) {
    return $tag('i', $content);
  };
  $p = function ($content) use ($tag) {
    return $tag('p', $content);
  };

  // Return locally defined variables so extract() can easily unpack.
  // phpcs:enable DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable
  return get_defined_vars();
}