Offer 'Raw Data' format besides 'Tabular View'. This is particularly useful for the rendering of a table as JSON for example with Views Datasource.

Screenshot

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lolandese created an issue. See original summary.

lolandese’s picture

FileSize
7.99 KB

Work in progress.

lolandese’s picture

Status: Active » Needs review
FileSize
10.62 KB

Attached patch offers besides Tabular view:

  • Raw data (JSON)
  • Raw data (JSON) with header values as keys (if not empty)

Example (in pretty print)

Tabular view

A B C
11 12 13
21 22 23

Raw data (JSON)

{
    "caption": "",
    "tabledata": {
        "row_0": {
            "col_0": "A",
            "col_1": "B",
            "col_2": "C"
        },
        "row_1": {
            "col_0": "11",
            "col_1": "12",
            "col_2": "13"
        },
        "row_2": {
            "col_0": "21",
            "col_1": "22",
            "col_2": "23"
        }
    }
}

Raw data (JSON) with header values as keys (if not empty)

{
    "caption": "",
    "tabledata": {
        "row_1": {
            "A": "11",
            "B": "12",
            "C": "13"
        },
        "row_2": {
            "A": "21",
            "B": "22",
            "C": "23"
        }
    }
}

 
If a header value is empty the original key is used. If for example in table above the column header value "C" would be empty then "col_2" would be used as key for that column.

{
    "caption": "",
    "tabledata": {
        "row_1": {
            "A": "11",
            "B": "12",
            "col_2": "13"
        },
        "row_2": {
            "A": "21",
            "B": "22",
            "col_2": "23"
        }
    }
}

Note

We only have pretty print output if on a node page thus in e.g. a View it will be without indentation and line breaks. Usually you would however only use the JSON display in a View capable of serving JSON of through the Services module and still use the Tabular view on the node page. That's why when in JSON mode options like 'Hide header' and 'CSV export' won't have effect.

Solving #2841782: Move display related settings from manage fields to manage display will become more relevant.

lolandese’s picture

lolandese’s picture

FileSize
12.54 KB

Added another display mode 'Raw data (JSON) with header and first column values as keys (if not empty)'. This gives us complete control over the used keys in the JSON on all levels.

Example (in pretty print

Tabular view

A

B

C

First

11

12

13

Second

21

22

23

31

32

33

Raw data (JSON) with header and first column values as keys (if not empty)

{
    "caption": "",
    "tabledata": {
        "First": {
            "A": "11",
            "B": "12",
            "C": "13"
        },
        "Second": {
            "A": "21",
            "B": "22",
            "C": "23"
        },
        "row_3": {
            "A": "31",
            "B": "32",
            "C": "33"
        }
    }
}
lolandese’s picture

FileSize
12.98 KB

Modified the last added display mode to 'Raw data (JSON) with field label, header and first column values as keys (if not empty)'. This gives us even control over the used top level key.

Furthermore is displays the caption only if not empty (for all Raw JSON displays).

{
    "Table": {
        "First": {
            "A": "11",
            "B": "12",
            "C": "13"
        },
        "Second": {
            "A": "21",
            "B": "22",
            "C": "23"
        },
        "row_3": {
            "A": "31",
            "B": "32",
            "C": "33"
        }
    }
}
SebasL’s picture

Status: Needs review » Needs work
FileSize
36.33 KB
39.18 KB

While testing a scenario was found that could be considered a bug, but it isn't: If the content of the first column is same in multiple rows, the JSON output only prints one. See below images.
To avoid confusion it is recommended to show a notification to the user informing the exception.

Same content in first column of rows
JSON output without some rows content

More reading:
The names within an object SHOULD be unique:
https://tools.ietf.org/html/rfc7159#section-4
http://stackoverflow.com/a/23195243/1606702

lolandese’s picture

Status: Needs work » Needs review
FileSize
91.57 KB
14.28 KB

To avoid confusion it is recommended to show a notification to the user informing the exception.

I agree.

Screenshot

lolandese’s picture

FileSize
14.27 KB

Avoid informal writing (use 'a table' instead of 'your table').

lolandese’s picture

Status: Needs review » Needs work

With an empty table:

Warning: Invalid argument supplied for foreach() in tablefield_field_formatter_view() (line 525 of /sites/all/modules/contrib/tablefield/tablefield.module).

To wrap in a conditional, also in the other view mode (line 480).

lolandese’s picture

Status: Needs work » Needs review
FileSize
14.97 KB

Patch additions:

  • Skip processing is the table field is empty.
  • Show the caption field in the JSON even if empty (improves consistency of the expected output).
  • Add a separating comma for multi-value table field to have valid JSON.
  • Put a multi-value table field in the same
     tags instead of each one separate.
lolandese’s picture

FileSize
15.56 KB

Against latest dev.

lolandese’s picture

Status: Needs review » Needs work
FileSize
18.25 KB

Instead of having 3 different view modes for raw JSON data we extended only one with different display options. The result is cleaner and shorter code while enhancing the user experience.

Furthermore we introduced the option to:

  • display only tabledata (no caption) so that the JSON is not unnecessary nested if not needed
  • have a vertical header (swap columns and rows).

The latter is especially handy when dealing with long lists of key-value pairs that would otherwise be forced to be orientated horizontally resulting in a horizontal scrollbar, worsening the user experience. The setting is there but the feature of swapping should still be built.

lolandese’s picture

Status: Needs work » Needs review
FileSize
19.08 KB

Added row-column swap option.

View mode:

Screenshot

Edit mode:

Screenshot

Display options:

Screenshot

lolandese’s picture

SebasL’s picture

Status: Needs review » Needs work
FileSize
22.02 KB
45.41 KB
185.47 KB

The patch fails when there are more than one header or first column with same value.

Settings in error scenario:
Settings in scenario

Content in table:
Table content

Errors:
Errors

lolandese’s picture

Status: Needs work » Needs review
FileSize
19.25 KB
lolandese’s picture

FileSize
1.18 KB
SebasL’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
65.3 KB
94.21 KB
83.48 KB
91.54 KB
86.99 KB

Test with equal and different column values, with all combination of settings.
The output is correct in all scenarios. Below screenshots of two scenarios:

Table with same value in multiple columns:
Table Content

Settings in field:
Table settings

Table output:
Table output

Different settings in field:
Different settings in field

New table output:
New table output

  • lolandese committed 8add31d on 7.x-2.x
    Issue #2847650 by lolandese, SebasL: Offer 'Raw Data' format besides '...
lolandese’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the thorough review.

  • lolandese committed 8add31d on 7.x-3.x
    Issue #2847650 by lolandese, SebasL: Offer 'Raw Data' format besides '...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

lolandese’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
Status: Closed (fixed) » Patch (to be ported)
Parent issue: » #2189203: D8 port (from the existing 7.x-3.x to a new 8.x-3.x)
FileSize
82.23 KB
112.82 KB

Compare D8 (with just one option) with D7 below:

Screenshot

Screenshot

lolandese’s picture

Related issues: +#2912409: D8 version

Needed would be probably also a D8 version of the Themeless module.