public function Progress::render

Same name in this branch
  1. 8.x-1.x src/Plugin/views/field/Progress.php \Drupal\tmgmt\Plugin\views\field\Progress::render()
  2. 8.x-1.x translators/tmgmt_local/src/Plugin/views/field/Progress.php \Drupal\tmgmt_local\Plugin\views\field\Progress::render()

File

src/Plugin/views/field/Progress.php, line 19

Class

Progress
Field handler which shows the progress of a job or job item.

Namespace

Drupal\tmgmt\Plugin\views\field

Code

public function render(ResultRow $values) {
  $entity = $this
    ->getEntity($values);

  // If job is continuous we don't show anything.
  if ($entity instanceof JobInterface && $entity
    ->isContinuous()) {
    return;
  }
  if ($entity instanceof JobInterface) {
    switch ($entity
      ->getState()) {
      case JobInterface::STATE_UNPROCESSED:
        return t('Unprocessed');
        break;
      case JobInterface::STATE_REJECTED:
        return t('Rejected');
        break;
      case JobInterface::STATE_ABORTED:
        return t('Aborted');
        break;
      case JobInterface::STATE_FINISHED:
        return t('Finished');
        break;
    }
  }
  elseif ($entity instanceof JobItemInterface) {
    switch ($entity
      ->getState()) {
      case JobItemInterface::STATE_INACTIVE:
        return t('Inactive');
        break;
      case JobItemInterface::STATE_ACCEPTED:
        return t('Accepted');
        break;
      case JobItemInterface::STATE_ABORTED:
        return t('Aborted');
        break;
    }
  }
  $counts = array(
    '@pending' => $entity
      ->getCountPending(),
    '@translated' => $entity
      ->getCountTranslated(),
    '@reviewed' => $entity
      ->getCountReviewed(),
    '@accepted' => $entity
      ->getCountAccepted(),
  );
  $title = t('Pending: @pending, translated: @translated, reviewed: @reviewed, accepted: @accepted.', $counts);
  $sum = array_sum($counts);
  if ($sum == 0) {
    return [];
  }
  $output = [
    '#theme' => 'tmgmt_progress_bar',
    '#attached' => [
      'library' => 'tmgmt/admin',
    ],
    '#title' => $title,
    '#entity' => $entity,
    '#total' => $sum,
    '#parts' => [
      'pending' => [
        'count' => $counts['@pending'],
        'width' => $counts['@pending'] / $sum * 100,
      ],
      'translated' => [
        'count' => $counts['@translated'],
        'width' => $counts['@translated'] / $sum * 100,
      ],
      'reviewed' => [
        'count' => $counts['@reviewed'],
        'width' => $counts['@reviewed'] / $sum * 100,
      ],
      'accepted' => [
        'count' => $counts['@accepted'],
        'width' => $counts['@accepted'] / $sum * 100,
      ],
    ],
  ];
  return $output;
}