-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExampleStatusFieldHook.php
68 lines (61 loc) · 2.45 KB
/
ExampleStatusFieldHook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Drush\Commands\example_drush_extension;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
/**
* This hook file demonstrates how to add a new line to the drush
* core:status command.
*
* Note that this hook requires Drush 9; it does not work in Drush 8.
*/
class ExampleStatusFieldHook extends DrushCommands
{
/**
* Add a new default field, 'example-status' to the core:status command.
*
* Without this hook, the 'example-status' field will be added to the
* output result, but it will not be displayed unless requested, e.g.
* via `--fields=*` or `--field=example-status`.
*
* Note that an alter hook is needed to actually add the field to the
* core:status command output. Without the alter hook, this hook will
* cause an error.
* @see addCoreStatusField()
*
* @hook command-event core:status
*/
public function addCoreStatusDefaultField(ConsoleCommandEvent $event) {
// gets the command to be executed
$command = $event->getCommand();
$definition = $command->getDefinition();
$options = $definition->getOptions();
$default_fields = $options['fields']->getDefault();
$default_fields .= ',example-status';
$options['fields']->setDefault($default_fields);
}
/**
* Add an 'example-status' field to the core:status command.
*
* Note that a command-event hook is necessary to make this field be
* displayed by default in the `drush core:status` command.
* @see addCoreStatusDefaultField()
*
* @hook alter core:status
*/
public function addCoreStatusField($result, CommandData $commandData)
{
$formatter_options = $commandData->formatterOptions();
// Add a field label for our new 'whoami' field. This is required
// in order for this field to be selectable; otherwise, our structured
// data formatters will remove it.
$field_labels = $formatter_options->get(FormatterOptions::FIELD_LABELS);
$field_labels['example-status'] = 'Example Status';
$formatter_options->setFieldLabels($field_labels);
// Add our data to the new example field.
$result['example-status'] = 'Added by ' . __METHOD__;
return $result;
}
}