I am working on a Drupal 8 module, and a separate module of examples, on how to use the first module. In my example module, I have a function in my controller that looks like this:
public function basicUsage() {
// Basic line chart.
$series[] = [
'data' => [[0, 3], [4, 8], [8, 5], [9, 13]],
];
$params = [
"data" => $series,
"theme" => "flot_examples_basic_usage",
];
$flot = new FlotD8($params);
$output[] = $flot->outputPlot();
return $output;
}
I would like to use the standard entity notation if possible to simplify things, and make it look like this:
public function basicUsage() {
// Basic line chart.
$series[] = [
'data' => [[0, 3], [4, 8], [8, 5], [9, 13]],
];
$output["flot_d8"] = [ //maybe name needs to be FlotD8?
"#data" => $series,
"#theme" => "flot_examples_basic_usage",
];
return $output;
}
Here is the code for my class. What do I need to do or change to give it the behavior I'm looking for? Is it just a matter of extending a certain Core Entity class? I do not need any schema or database functionality.
<?php
namespace Drupal\flot_d8\Entity;
class FlotD8 {
protected $id;
protected $theme;
protected $data;
protected $options;
function __construct($params){
$this->id = isset($params['id']) ? $params['id'] : 'placeholder';
$this->theme = isset($params['theme']) ? $params['theme'] : 'flot_d8_my_template';
$this->data = $params['data'];
$this->options = isset($params['options']) ? $params['options'] : NULL;
}
/*
* Create the theme output.
*/
function outputPlot (){
$drupalsettings['flot_d8'][$this->id] = ['data' => $this->data, 'options' => $this->options];
$output = [
'#attached' => [
'drupalSettings' => $drupalsettings,
],
'#theme' => $this->theme,
'#options' => $this->options,
'#data' => $this->data,
];
return $output;
}
}