key_value.inc

  1. 8.0.x commands/core/outputformat/key_value.inc
  2. 6.x commands/core/outputformat/key_value.inc
  3. 7.x commands/core/outputformat/key_value.inc
  4. master commands/core/outputformat/key_value.inc

Classes

Namesort descending Description
drush_outputformat_key_value Output formatter 'key_value'

File

commands/core/outputformat/key_value.inc
View source
  1. <?php
  2. /**
  3. * Output formatter 'key_value'
  4. *
  5. * @param $data
  6. * The $data parameter contains an array of key / value pairs which
  7. * are rendered as "key : value" in a formatted word-wrapped table
  8. * with aligned columns. 'value' is expected to always be a simple string;
  9. * if it is not, it is rendered with var_export.
  10. * @param $metadata
  11. * 'label' - If present, creates a section header "[label]" prior to the data
  12. * 'separator' - If present, used instead of ', ' when impoding data values
  13. * 'ini-item' - If present, selects a single item from any data value that is
  14. * an array and uses it instead of imploding all values together.
  15. *
  16. * Code:
  17. *
  18. * return array(
  19. * "b" => "Two B or ! Two B, that is the comparison",
  20. * "c" => "I see that C has gone to Sea"
  21. * );
  22. *
  23. * Output with --format=key-value:
  24. *
  25. * b : Two B or ! Two B,
  26. * that is the
  27. * comparison
  28. * c : I see that C has gone
  29. * to Sea
  30. *
  31. * Code:
  32. *
  33. * return array(
  34. * "a" => array(
  35. * "b" => "Two B or ! Two B, that is the comparison",
  36. * "c" => "I see that C has gone to Sea"
  37. * ),
  38. * "d" => array(
  39. * "e" => "Elephants and electron microscopes",
  40. * "f" => "My margin is too small"
  41. * )
  42. * );
  43. *
  44. * Output with --format=key-value-list:
  45. *
  46. * b : Two B or ! Two B,
  47. * that is the
  48. * comparison
  49. * c : I see that C has gone
  50. * to Sea
  51. *
  52. * e : Elephants and
  53. * electron microscopes
  54. * f : My margin is too
  55. * small
  56. */
  57. class drush_outputformat_key_value extends drush_outputformat {
  58. function format($input, $metadata) {
  59. if (!is_array($input)) {
  60. if (isset($metadata['label'])) {
  61. $input = array(dt($metadata['label']) => $input);
  62. }
  63. else {
  64. return $this->format_error(dt('No label provided.'));
  65. }
  66. }
  67. $kv_metadata = isset($metadata['table-metadata']) ? $metadata['table-metadata'] : array();
  68. if ((!isset($kv_metadata['key-value-item'])) && (isset($metadata['field-labels']))) {
  69. $input = drush_select_output_fields($input, $metadata['field-labels'], $metadata['field-mappings']);
  70. }
  71. if (isset($metadata['include-field-labels'])) {
  72. $kv_metadata['include-field-labels'] = $metadata['include-field-labels'];
  73. }
  74. $formatted_table = drush_key_value_to_array_table($input, $kv_metadata);
  75. if ($formatted_table === FALSE) {
  76. return FALSE;
  77. }
  78. return drush_format_table($formatted_table, FALSE, array());
  79. }
  80. }