Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / output-formatters / README.md
index e50958149184ad4638d5f3bf5b2e6a04cc6cf8e7..6fbe718a065ead2f6fa7daf03bda0ab282d73611 100644 (file)
@@ -53,7 +53,9 @@ Formatters may also implement different interfaces to alter the behavior of the
 Most formatters will operate on any array or ArrayObject data. Some formatters require that specific data types be used. The following data types, all of which are subclasses of ArrayObject, are available for use:
 
 - `RowsOfFields`: Each row contains an associative array of field:value pairs. It is also assumed that the fields of each row are the same for every row. This format is ideal for displaying in a table, with labels in the top row.
+- `RowsOfFieldsWithMetadata`: Equivalent to `RowsOfFields`, but allows the data to be nested inside of an element, with other elements being used as metadata, or, alternately, allows the metadata to be nested inside of an element, with all other elements being used as data.
 - `PropertyList`: Each row contains a field:value pair. Each field is unique. This format is ideal for displaying in a table, with labels in the first column and values in the second common.
+- `ListDataFromKeys`: The result may be structured or unstructured data. When formatted with the --format=list formatter, the result will come from the array keys instead of the array values.
 - `DOMDocument`: The standard PHP DOM document class may be used by functions that need to be able to presicely specify the exact attributes and children when the XML output format is used.
 
 Commands that return table structured data with fields can be filtered and/or re-ordered by using the --fields option. These structured data types can also be formatted into a more generic type such as yaml or json, even after being filtered. This capabilities are not available if the data is returned in a bare php array.
@@ -90,6 +92,26 @@ public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
 ```
 Note that if your data structure is printed with a formatter other than one such as the table formatter, it will still be reordered per the selected fields, but cell rendering will **not** be done.
 
+The RowsOfFields and PropertyList data types also allow objects that implement RenderCellInterface, as well as anonymous functions to be added directly to the data structure object itself. If this is done, then the renderer will be called for each cell in the table. An example of an attached renderer implemented as an anonymous function is shown below.
+```php
+    return (new RowsOfFields($data))->addRendererFunction(
+        function ($key, $cellData, FormatterOptions $options, $rowData) {
+            if ($key == 'my-field') {
+                return implode(',', $cellData);
+            }
+            return $cellData;
+        }
+    );
+```
+This project also provides a built-in cell renderer, NumericCellRenderer, that adds commas at the thousands place and right-justifies columns identified as numeric. An example of a numeric renderer attached to two columns of a data set is shown below.
+```php
+use Consolidation\OutputFormatters\StructuredData\NumericCellRenderer;
+...
+    return (new RowsOfFields($data))->addRenderer(
+         new NumericCellRenderer($data, ['population','cats-per-capita'])
+    );
+```
+
 ## API Usage
 
 It is recommended to use [Consolidation/AnnotationCommand](https://github.com/consolidation/annotation-command) to manage commands and formatters.  See the [AnnotationCommand API Usage](https://github.com/consolidation/annotation-command#api-usage) for details.