4 * Output formatter 'list'
7 * The $data parameter is expected to be an array of key / value pairs.
8 * Each key / value pair is passed to some other output formatter for
9 * rendering; the key becomes the label, $metadata['label'], and the
10 * value becomes the $data for the sub-formatter.
12 * 'matches' - Specifies the exact kind of list to be rendered in an
13 * array of two elements. $matches[0] is the full name of the
14 * list format (e.g. 'string-list'), and $matches[1] is the type
15 * of the sub-formatter (e.g. 'string'). If not specified, 'string'
20 * return array('a', 'b', 'c');
22 * Output with --format=list: (list of string):
28 class drush_outputformat_list extends drush_outputformat {
33 // Separate the 'list' and 'filter' metadata from everything
34 // else in $engine_config['engine-info']
35 $list_metadata = array();
36 foreach ($this->engine_config as $key => $value) {
37 if ((substr($key, 0, 4) == 'list') || (substr($key, -6) == 'filter') || (substr($key, 0, 5) == 'field') || ($key == 'options')) {
38 unset($this->engine_config[$key]);
39 $list_metadata[$key] = $value;
42 foreach ($this->engine_config['engine-info'] as $key => $value) {
43 if ((substr($key, 0, 4) == 'list') || (substr($key, -6) == 'filter')) {
44 unset($this->engine_config['engine-info'][$key]);
45 $list_metadata[$key] = $value;
48 $sub_formatter = isset($list_metadata['list-item-type']) ? $list_metadata['list-item-type'] : 'string';
49 $this->sub_engine = drush_load_engine('outputformat', $sub_formatter, $this->engine_config);
50 if (!is_object($this->sub_engine)) {
51 return drush_set_error('DRUSH_INVALID_SUBFORMATTER', dt("The list output formatter could not load its subformatter: !sub", array('!sub' => $sub_formatter)));
53 $engine_info = $this->engine_config['engine-info'];
54 $this->engine_config = array(
55 'engine-info' => array(
56 'machine-parsable' => $this->sub_engine->engine_config['engine-info']['machine-parsable'],
58 'metameta' => $this->sub_engine->engine_config,
63 function format($input, $metadata) {
65 if (is_array($input)) {
66 // If this list is processing output from a command that produces table
67 // @todo - need different example below?
68 // output, but our subformatter only supports 'single' items (e.g. csv),
69 // then we will convert our data such that the output will be the keys
71 if (($this->data_type($metadata) == 'format-table') && ($this->supports_single_only($metadata)) && !isset($metadata['list-item'])) {
72 // If the user specified exactly one field with --fields, then
73 // use it to select the data column to use instead of the array key.
74 if (isset($metadata['field-labels']) && (count($metadata['field-labels']) == 1)) {
75 $first_label = key($metadata['field-labels']);
76 $input = drush_output_get_selected_field($input, $first_label);
79 $input = array_keys($input);
83 $field_selection_control = isset($metadata['list-field-selection-control']) ? $metadata['list-field-selection-control'] : 0;
84 $selected_output_fields = false;
85 if (empty($metadata['metameta'])) {
86 $metameta = $metadata;
87 unset($metameta['list-item']);
88 unset($metameta['list-item-default-value']);
91 $metameta = $metadata['metameta'];
93 $list_separator_key = 'list-separator';
94 if ($this->sub_engine->is_list()) {
95 $list_separator_key = 'line-separator';
96 if (isset($metadata['list-separator'])) {
97 $metameta['list-separator'] = $metadata['list-separator'];
100 $separator = isset($metadata[$list_separator_key]) && !empty($metadata[$list_separator_key]) ? $metadata[$list_separator_key] : "\n";
101 // @todo - bug? we iterate over a hard coded, single item array?
102 foreach (array('field-labels') as $key) {
103 if (isset($metadata[$key])) {
104 $metameta[$key] = $metadata[$key];
108 // Include field labels, if specified
109 if (!isset($metadata['list-item']) && isset($metadata['labeled-list']) && is_array($input) && isset($metadata['field-labels'])) {
110 if (isset($metadata['include-field-labels']) && $metadata['include-field-labels']) {
111 array_unshift($input, $metadata['field-labels']);
114 foreach ($input as $label => $data) {
115 // If this output formatter is set to print a single item from each
116 // element, select that item here.
117 if (isset($metadata['list-item'])) {
118 $data = isset($data[$metadata['list-item']]) ? $data[$metadata['list-item']] : $metadata['list-item-default-value'];
120 // If this formatter supports the --fields option, then filter and
121 // order the fields the user wants here. Note that we need to be
122 // careful about when we call drush_select_output_fields(); sometimes,
123 // there will be nested formatters of type 'list', and it would not
124 // do to select the output fields more than once.
125 // 'list-field-selection-control can be set to a positive number to
126 // cause output fields to be selected at a later point in the call chain.
127 elseif (is_array($data) && isset($metadata['field-labels'])) {
128 if (!$field_selection_control) {
129 $data = drush_select_output_fields($data, $metadata['field-labels'], $metadata['field-mappings']);
130 $selected_output_fields = true;
133 $metameta['label'] = $label;
134 if ($selected_output_fields) {
135 $metameta['list-field-selection-control'] = -1;
137 elseif ($field_selection_control) {
138 $metameta['list-field-selection-control'] = $field_selection_control - 1;
140 $formatted_item = $this->sub_engine->format($data, $metameta);
141 if ($formatted_item === FALSE) {
145 $output .= $separator;
147 if (($separator != "\n") && !empty($separator) && (strpos($formatted_item, $separator) !== FALSE)) {
148 $formatted_item = drush_wrap_with_quotes($formatted_item);
150 $output .= $formatted_item;