You are here

public function ChecklistapiCommands::listCommand in Checklist API 8

Get an overview of your installed checklists with progress details.

@command checklistapi:list @aliases capi-list,capil,checklistapi-list

File

src/Commands/ChecklistapiCommands.php, line 44

Class

ChecklistapiCommands
Checklist API Drush command fileA Drush commandfile.

Namespace

Drupal\checklistapi\Commands

Code

public function listCommand() {
  $definitions = checklistapi_get_checklist_info();
  if (empty($definitions)) {
    return $this->logger
      ->alert(dt('No checklists available.'));
  }

  // Build table rows.
  $rows = [];

  // The first row is the table header.
  $rows[] = [
    dt('Checklist'),
    dt('Progress'),
    dt('Last updated'),
    dt('Last updated by'),
  ];
  foreach ($definitions as $id => $definition) {
    $checklist = checklistapi_checklist_load($id);
    $row = [];
    $row[] = dt('!title (@id)', [
      '!title' => strip_tags($checklist->title),
      '@id' => $id,
    ]);
    $row[] = dt('@completed of @total (@percent%)', [
      '@completed' => $checklist
        ->getNumberCompleted(),
      '@total' => $checklist
        ->getNumberOfItems(),
      '@percent' => round($checklist
        ->getPercentComplete()),
    ]);
    $row[] = $checklist
      ->getLastUpdatedDate();
    $row[] = $checklist
      ->getLastUpdatedUser();
    $rows[] = $row;
  }
  $formatter_manager = new FormatterManager();
  $opts = [
    FormatterOptions::INCLUDE_FIELD_LABELS => FALSE,
    FormatterOptions::TABLE_STYLE => 'compact',
    FormatterOptions::TERMINAL_WIDTH => ListCommands::getTerminalWidth(),
  ];
  $formatter_options = new FormatterOptions([], $opts);
  $formatter_manager
    ->write($this
    ->output(), 'table', new RowsOfFields($rows), $formatter_options);
}