d3_sankey_table_group_pp.module 1.84 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Hooks and helper functions for the d3_sankey_table_group_pp module.
 */

8
9
use Drupal\d3_sankey_table_group_pp\TableGroupingPreprocessor;

10
11
12
13
14
15
16
17
/* Hooks. */

/**
 * Implements hook_xautoload().
 */
function d3_sankey_table_group_pp_xautoload($adapter) {
  $adapter->absolute()->addPsr4('Drupal\d3_sankey_table_group_pp', 'src');
}
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/**
 * Implements hook_field_formatter_info().
 */
function d3_sankey_table_group_pp_field_formatter_info() {
  $formatters = array();

  // A field formatter for tablefields, which displays a Sankey diagram using
  // the table grouping preprocessor.
  $formatters['tablefield_d3_sankey_table_group_pp'] = array(
    'label' => t('Sankey diagram (table grouping preprocessor)'),
    'field types' => array('tablefield'),
  );

  return $formatters;
}

/**
 * Implements hook_field_formatter_view().
 */
function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // A field formatter for tablefields, which displays a Sankey diagram using
  // the table grouping preprocessor.
  if ($display['type'] === 'tablefield_d3_sankey_table_group_pp') {
    foreach ($items as $delta => $item) {
      $chart = _uw_career_common_chart_options();

      // Initialize a GroupingPreprocessor, feed it the query results, get the
      // raw data out of the preprocessor, and add it to the chart.
      $preprocessor = new TableGroupingPreprocessor();
      foreach ($item['tabledata'] as $row) {
        $preprocessor->ingestRow((array) $row);
      }
      $raw_data = $preprocessor->getRawData();
      $chart['nodes'] = $raw_data->getAssocArrayNodes();
      $chart['links'] = $raw_data->getAssocArrayLinks();

      // Now draw the chart.
      $chart = d3_draw($chart);
      $element[$delta]['#markup'] = $chart;
    }
  }

  return $element;
}