Issue #3105591: Make drush integration Drush 9 + 10 compatible

parent b38bf47a
<?php
/**
* @file
* Drush commands for the Checklist API module.
*/
use Drupal\Core\Render\Element;
use Drupal\user\Entity\User;
/**
* Implements hook_drush_command().
*/
function checklistapi_drush_command() {
$items = [];
$items['checklistapi-list'] = [
'aliases' => ['capi-list', 'capil'],
'description' => 'Get an overview of your installed checklists with progress details.',
];
$items['checklistapi-info'] = [
'aliases' => ['capi-info', 'capii'],
'description' => 'Show detailed info for a given checklist.',
'arguments' => [
'checklist' => 'The checklist machine name, e.g., "example_checklist".',
],
'required-arguments' => TRUE,
];
return $items;
}
/**
* Implements hook_drush_help().
*/
function checklistapi_drush_help($section) {
switch ($section) {
case 'meta:checklistapi:title':
return dt('Checklist API commands');
}
}
/**
* Drush callback for checklist listing.
*/
function drush_checklistapi_list() {
$definitions = checklistapi_get_checklist_info();
if (empty($definitions)) {
return drush_print(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;
}
return drush_format_table($rows, TRUE);
}
/**
* Implements drush_hook_COMMAND_validate().
*
* @see drush_checklistapi_info()
*/
function drush_checklistapi_info_validate() {
$arguments = drush_get_arguments();
$id = $arguments[1];
$checklist = checklistapi_checklist_load($id);
// Make sure the given checklist exists.
if (!$checklist) {
drush_set_error('CHECKLISTAPI_INVALID_ARGUMENT', dt('No such checklist "@id".', [
'@id' => $id,
]));
}
drush_set_context('checklist', $checklist);
}
/**
* Drush callback for checklist info.
*/
function drush_checklistapi_info() {
$checklist = drush_get_context('checklist');
// Print the help.
if (!empty($checklist->help)) {
drush_print(strip_tags($checklist->help));
}
// Print last updated and progress details.
if ($checklist->hasSavedProgress()) {
drush_print('');
drush_print(dt('Last updated @date by @user', [
'@date' => $checklist->getLastUpdatedDate(),
'@user' => $checklist->getLastUpdatedUser(),
]));
drush_print(dt('@completed of @total (@percent%) complete', [
'@completed' => $checklist->getNumberCompleted(),
'@total' => $checklist->getNumberOfItems(),
'@percent' => round($checklist->getPercentComplete()),
]));
}
// Loop through groups.
$groups = $checklist->items;
foreach (Element::children($groups) as $group_key) {
$group = &$groups[$group_key];
// Print group title.
drush_print('');
drush_print(strip_tags($group['#title']) . ':');
// Loop through items.
foreach (Element::children($group) as $item_key) {
$item = &$group[$item_key];
$saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;
// Build title.
$title = strip_tags($item['#title']);
if ($saved_item) {
// Append completion details.
$user = User::load($saved_item['#uid']);
$title .= ' - ' . dt('Completed @time by @user', [
'@time' => \Drupal::service('date.formatter')->format($saved_item['#completed'], 'short'),
'@user' => $user->getAccountName(),
]);
}
// Print the list item.
drush_print(dt(' [@x] !title', [
'@x' => ($saved_item) ? 'x' : ' ',
'!title' => $title,
]));
}
}
}
......@@ -4,10 +4,23 @@
"description": "Provides an API for creating fillable, persistent checklists.",
"homepage": "http://drupal.org/project/checklistapi",
"license": "GPL-2.0-or-later",
"require": {},
"authors": [
{
"name": "Travis Carden",
"email": "travis.carden@gmail.com"
}
],
"require": {
"php": ">=5.6.0"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"drush": {
"services": {
"drush.services.yml": "^10"
}
}
},
"minimum-stability": "dev",
......
services:
checklistapi.commands:
class: \Drupal\checklistapi\Commands\ChecklistapiCommands
arguments: ['@logger.factory']
tags:
- { name: drush.command }
<?php
namespace Drupal\checklistapi\Commands;
use Consolidation\OutputFormatters\FormatterManager;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\Element;
use Drupal\user\Entity\User;
use Drush\Commands\DrushCommands;
use Drush\Commands\help\ListCommands;
use Psr\Log\LoggerAwareInterface;
/**
* Checklist API Drush command fileA Drush commandfile.
*/
class ChecklistapiCommands extends DrushCommands implements LoggerAwareInterface {
/**
* The logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs an instance.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_channel
* The logger channel factory.
*/
public function __construct(LoggerChannelFactoryInterface $logger_channel) {
$this->logger = $logger_channel->get('drush');
}
/**
* Get an overview of your installed checklists with progress details.
*
* @command checklistapi:list
* @aliases capi-list,capil,checklistapi-list
*/
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);
}
/**
* Show detailed info for a given checklist.
*
* @param string $checklist_id
* The checklist machine name, e.g., "example_checklist".
*
* @return string|void
* The command output.
*
* @command checklistapi:info
* @aliases capi-info,capii,checklistapi-info
*/
public function infoCommand($checklist_id) {
$checklist = checklistapi_checklist_load($checklist_id);
// Make sure the given checklist exists.
if (!$checklist) {
return $this->logger->error(dt('No such checklist "@id".', [
'@id' => $checklist_id,
]));
}
$output = [];
// Print the help.
if (!empty($checklist->help)) {
$output[] = strip_tags($checklist->help);
}
// Print last updated and progress details.
if ($checklist->hasSavedProgress()) {
$output[] = '';
$output[] = dt('Last updated @date by @user', [
'@date' => $checklist->getLastUpdatedDate(),
'@user' => $checklist->getLastUpdatedUser(),
]);
$output[] = dt('@completed of @total (@percent%) complete', [
'@completed' => $checklist->getNumberCompleted(),
'@total' => $checklist->getNumberOfItems(),
'@percent' => round($checklist->getPercentComplete()),
]);
}
// Loop through groups.
$groups = $checklist->items;
foreach (Element::children($groups) as $group_key) {
$group = &$groups[$group_key];
// Print group title.
$output[] = '';
$output[] = strip_tags($group['#title']) . ':';
// Loop through items.
foreach (Element::children($group) as $item_key) {
$item = &$group[$item_key];
$saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;
// Build title.
$title = strip_tags($item['#title']);
if ($saved_item) {
// Append completion details.
$user = User::load($saved_item['#uid']);
$title .= ' - ' . dt('Completed @time by @user', [
'@time' => \Drupal::service('date.formatter')->format($saved_item['#completed'], 'short'),
'@user' => $user->getAccountName(),
]);
}
// Print the list item.
$output[] = dt(' [@x] !title', [
'@x' => ($saved_item) ? 'x' : ' ',
'!title' => $title,
]);
}
}
$output[] = '';
return implode(PHP_EOL, $output);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment