You are here

protected function SmsDevelMessageForm::verboseResults in SMS Framework 8

Render message results as a HTML table.

Parameters

\Drupal\sms\Message\SmsMessageResultInterface[] $results: Results.

Return value

array A render array.

1 call to SmsDevelMessageForm::verboseResults()
SmsDevelMessageForm::buildForm in modules/sms_devel/src/Form/SmsDevelMessageForm.php
Form constructor.

File

modules/sms_devel/src/Form/SmsDevelMessageForm.php, line 288

Class

SmsDevelMessageForm
Simulate a message being sent or received.

Namespace

Drupal\sms_devel\Form

Code

protected function verboseResults(array $results) {
  $render = [];

  // Renders plain text, or 'Undefined' message if falsey.
  $renderString = function ($value) {
    return !empty($value) ? [
      '#plain_text' => $value,
    ] : [
      '#markup' => $this
        ->t('<em>Undefined</em>'),
    ];
  };

  // Renders a date text, or 'Undefined' message if falsey.
  $renderDate = function ($timestamp) {
    if ($timestamp) {
      $date = DrupalDateTime::createFromTimestamp($timestamp);
      return [
        '#plain_text' => $date
          ->format('c'),
      ];
    }
    else {
      return [
        '#markup' => $this
          ->t('<em>Undefined</em>'),
      ];
    }
  };
  $render['results'] = [
    '#type' => 'table',
    '#caption' => [
      'heading' => [
        '#type' => 'inline_template',
        '#template' => '<h2>Results</h2>',
      ],
    ],
    '#header' => [
      $this
        ->t('Result'),
      $this
        ->t('Error'),
      $this
        ->t('Error Message'),
      $this
        ->t('Credits Used'),
      $this
        ->t('Credits Balance'),
    ],
  ];
  foreach ($results as $i => $result) {
    $row = [];
    $row[]['#plain_text'] = $this
      ->t("#@number", [
      '@number' => $i,
    ]);
    $error = $result
      ->getError();
    $row[] = $error ? [
      '#plain_text' => $error,
    ] : [
      '#markup' => $this
        ->t('<em>Success</em>'),
    ];
    $row[] = $renderString($result
      ->getErrorMessage());
    $row[] = $renderString($result
      ->getCreditsUsed());
    $row[] = $renderString($result
      ->getCreditsBalance());
    $render['results'][] = $row;
    $reports_cell = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Recipient'),
        $this
          ->t('Message ID'),
        $this
          ->t('Status'),
        $this
          ->t('Status Message'),
        $this
          ->t('Time Delivered'),
        $this
          ->t('Time Queued'),
      ],
    ];
    foreach ($result
      ->getReports() as $report) {
      $row = [];
      $row[] = $renderString($report
        ->getRecipient());
      $row[] = $renderString($report
        ->getMessageId());
      $row[] = $renderString($report
        ->getStatus());
      $row[] = $renderString($report
        ->getStatusMessage());
      $row[] = $renderDate($report
        ->getTimeDelivered());
      $row[] = $renderDate($report
        ->getTimeQueued());
      $reports_cell[] = $row;
    }
    $render['results'][][] = [
      '#wrapper_attributes' => [
        'colspan' => count($render['results']['#header']),
      ],
      'data' => $reports_cell,
    ];
  }
  return $render;
}