Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Views Datasource

Jeremy edited this page Jun 22, 2021 · 3 revisions

Do you want to visualise data from Drupal content, this can be achieved by using Views to create your datasets, outputting as JSON, then providing the JSON URL to DVF. This page will show an example of how that might be done.

Before you start, read the JSON Datasource page to understand how DVF reads JSON documents. Then setup a DVF field as per its instructions, the only difference being, make sure you use the field type Visualisation URL and not Visualisation File.

Make a view to provide your data

In this example, we will make a view that shows a Bar Chart that shows a count of different content types in Drupal. The goal is to get views outputting something like below, when you visit the path /content-types.json

[
    {
        "type": "Blog article",
        "count": "1"
    },
    {
        "type": "Event",
        "count": "4"
    },
    {
        "type": "FOI",
        "count": "1"
    },
    {
        "type": "News and Media",
        "count": "1"
    },
    {
        "type": "Standard page",
        "count": "11"
    },
]

Required modules

There a few ways to output JSON in views, most require additional modules, for this example, we will just use Drupal core REST module. Enable the following modules:

  • RESTful Web Services
  • REST UI
  • Serialization

Create your view

  • Add a new view, give it a name and check Provide REST export, set the path to be content-types.json then Save and edit
  • Make the following changes
    • Format > Settings - check "JSON"
    • Format > Show - Fields
    • Advanced > Use aggregation - yes
    • Remove all fields, filters, sorts
    • Add Fields
      • Content type (Aggregation type = Group results together, Uncheck link label to referenced entity)
      • ID (Aggregation type = Count)
    • Format > Show > Field Settings, add an alias for each, eg type = name, nid = count

Save and visit content-types.json, you should see your JSON view

Use your view data with DVF

  • Create new content which has a Visualisation URL JSON File field on it.
  • Add the path /content-types.json - DVF will turn this into an absolute URL internally
  • Select Bar Chart under Visualisation style
  • Select count under Fields (as per your alias in views)
  • Axis settings > X Axis > Tick values field select name (as per your alias in views)

Save and view, it should look something like this

Example chart