Add entity to the Chart element when a Chart field is used to enable retrieval of entity values in charts hooks

Created on 18 October 2024, about 1 month ago

Problem/Motivation

For users who are using the Chart Field and want to incorporate information from the entity into the chart (rather than needing to alter the Chart form), we should pass the entity and the field name to the Chart element.

API changes

The API doesn't change, but now you can access the entity (i.e. node) like this:

function MYMODULE_chart_definition_alter(array &$definition, array $element, $chart_id) {
  if ($element['#chart_library'] === 'highcharts') {
    // Add a caption based on a field on your node containing a Chart field.
    $node = $element['#entity'];
    if ($node instanceof \Drupal\node\NodeInterface && $node->hasField('field_caption')) {
      $definition['caption']['text'] = $node->field_caption->value ?? '';
    }
  }
}
✨ Feature request
Status

Active

Version

5.1

Component

Chart API

Created by

πŸ‡ΊπŸ‡ΈUnited States andileco

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024