Skip to content

Extending DVF

Jeremy edited this page Jun 30, 2021 · 2 revisions

DVF is built to be extended, this means developers can easily add new data sources or visualisations using Drupals Plugin API. This approach is used in many core aspects of Drupal, eg Adding a block or views plugin. It means you can add functionality in your own modules which add more functionality to DVF.

Plugin types

Need a different type of data source or maybe a different way of visualising the data, these are done with plugins and guides are available for each plugin type.

Here is an example of implementing a new DVF source and style with a frontend library that is not included with core DVF.

Hooks

Is the functionality provided by DVF really close, but you just want to make some minor tweaks or overrides? Maybe you need to provide some additional logic (eg if entity has field value, change visualisation config). These are good uses cases for DVF hooks. The following hooks are available in DVF.

  • hook_dvf_source_configuration_alter() - Modify configurations for a source. Eg changing the delimiter for a CSV source.
  • hook_dvf_style_configuration_alter() - Modify configuration for a visualisation. Eg change the colour palette.
  • hook_dvf_visualisation_build_alter() - Modify a visualisation renderable array before it gets rendered. Eg adding an extra class to the visualisation wrapper element.
  • hook_dvf_visualisation_data_alter() - Modify the data before it gets rendered. Eg. Reformat a data value.

For more documentation on hooks see dvf.api.php

Example hook usage

The following example shows how you might override the default colour palette for charts on a site wide level.

use Drupal\dvf\Plugin\VisualisationInterface;

/**
 * Implements theme_dvf_style_configuration_alter().
 *
 * Set a custom colour palette for all charts in a custom theme.
 */
function mytheme_dvf_style_configuration_alter(array &$configuration, VisualisationInterface $visualisation) {
  $configuration['chart']['palette'] = '#000000,#aec7e8,#ff0000,#ffbb78,#fff000';
  return $configuration;
}