You are here

public function OptionsLimitWebformHandler::buildSummaryTable in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php \Drupal\webform_options_limit\Plugin\WebformHandler\OptionsLimitWebformHandler::buildSummaryTable()

Build summary table.

Return value

array A renderable containing the options limit summary table.

Overrides WebformOptionsLimitHandlerInterface::buildSummaryTable

File

modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php, line 941

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

public function buildSummaryTable() {
  $element = $this
    ->getElement();
  if (!$element) {
    return [];
  }
  if ($this->configuration['limit_user']) {
    return [];
  }
  $webform_element = $this
    ->getWebformElement();
  $rows = [];
  $limits = $this
    ->isOptionsElement() ? $this
    ->getOptionsLimits() : $this
    ->getBooleanLimits();
  foreach ($limits as $limit) {
    if ($limit['limit']) {
      $percentage = number_format($limit['total'] / $limit['limit'] * 100) . '% ';
      $progress = [
        '#type' => 'html_tag',
        '#tag' => 'progress',
        '#attributes' => [
          'max' => $limit['limit'],
          'value' => $limit['total'],
        ],
      ];
    }
    else {
      $percentage = '';
      $progress = [];
    }
    $rows[] = [
      [
        'data' => $limit['label'],
        'style' => 'font-weight: bold',
      ],
      [
        'data' => $limit['limit'] ?: '∞',
        'style' => 'text-align: right',
      ],
      [
        'data' => $limit['limit'] ? $limit['remaining'] : '∞',
        'style' => 'text-align: right',
      ],
      [
        'data' => $limit['total'],
        'style' => 'text-align: right',
      ],
      [
        'data' => $progress,
        'style' => 'text-align: center',
      ],
      [
        'data' => $percentage,
        'style' => 'text-align: right',
      ],
    ];
  }
  return [
    'title' => [
      '#markup' => $webform_element
        ->getLabel($element),
      '#prefix' => '<h2>',
      '#suffix' => '</h2>',
    ],
    'table' => [
      '#type' => 'table',
      '#header' => [
        '',
        [
          'data' => $this
            ->t('Limit'),
          'style' => 'text-align: right',
        ],
        [
          'data' => $this
            ->t('Remaining'),
          'style' => 'text-align: right',
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        [
          'data' => $this
            ->t('Total'),
          'style' => 'text-align: right',
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        [
          'data' => $this
            ->t('Progress'),
          'style' => 'text-align: center',
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        '',
      ],
      '#rows' => $rows,
    ],
  ];
}