You are here

public function WebformAnalysisChart::build in Webform Analysis 8

Build.

Parameters

array $build: Build.

Overrides WebformAnalysisChartInterface::build

File

src/WebformAnalysisChart.php, line 36

Class

WebformAnalysisChart
Webform Analysis Chart.

Namespace

Drupal\webform_analysis

Code

public function build(array &$build = []) {
  if (!$this->entity) {
    return;
  }
  $analysis = new WebformAnalysis($this->entity);
  $build['components_data'] = $this
    ->buildComponentsData();
  $buildComponents =& $build['components_data'];
  $charts = [];
  $header = $this
    ->getHeader();
  foreach ($this->components as $component) {
    $id = $this
      ->createComponentId($component);
    $chart = $this
      ->createChart($id);
    $buildComponents['component__' . $component] = $this
      ->buildComponentData($analysis, $component, $id);
    switch ($chart['type']) {
      case '':
        $buildComponent =& $buildComponents['component__' . $component];
        $buildComponent['#data']['#rows'] = $analysis
          ->getComponentRows($component);
        break;
      case 'PieChart':
        $chart = $this
          ->buildPieChart($analysis, $component, $header) + $chart;
        break;
      default:
        $chart['data'] = $analysis
          ->getComponentRows($component, $header);
        break;
    }
    if ($chart['type'] && $chart['data']) {
      $charts[$id] = $chart;
    }
  }
  $build['#attached']['library'][] = 'webform_analysis/webform_charts';
  if ($charts) {
    $build['#attached'] += $this
      ->buildAttachedSettings($charts);
  }
  $build['#cache'] = [
    'max-age' => 0,
  ];
}