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 ?? '';
    }
  }
}

Issue fork charts-3481760

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andileco created an issue. See original summary.

andileco’s picture

Status: Active » Needs review

  • andileco committed 8e28a99b on 5.1.x
    Issue #3481760 by andileco, nikathone: Add entity to the Chart element...
andileco’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

andileco’s picture

Version: 5.1.x-dev » 5.0.x-dev
Status: Closed (fixed) » Needs work

  • andileco committed 00fe196d on 5.0.x
    Issue #3481760 by andileco, nikathone: Add entity to the Chart element...
andileco’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

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