Converting a helper class into an Entity 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' =&gt; [[0, 3], [4, 8], [8, 5], [9, 13]],
  ];
  $params = [
    "data" =&gt; $series,
    "theme" =&gt; "flot_examples_basic_usage",
  ];
  $flot = new FlotD8($params);
  $output[] = $flot-&gt;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' =&gt; [[0, 3], [4, 8], [8, 5], [9, 13]],
  ];
  $output["flot_d8"] = [  //maybe name needs to be FlotD8?
    "#data" =&gt; $series,
    "#theme" =&gt; "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.

&lt;?php
namespace Drupal\flot_d8\Entity;
class FlotD8 {
  protected $id;
  protected $theme;
  protected $data;
  protected $options;

  function __construct($params){
    $this-&gt;id = isset($params['id']) ? $params['id'] : 'placeholder';
    $this-&gt;theme = isset($params['theme']) ? $params['theme'] : 'flot_d8_my_template';
    $this-&gt;data = $params['data'];
    $this-&gt;options = isset($params['options']) ? $params['options'] : NULL;
  }

  /*
   * Create the theme output.
   */
  function outputPlot (){
    $drupalsettings['flot_d8'][$this-&gt;id] = ['data' =&gt; $this-&gt;data, 'options' =&gt; $this-&gt;options];
    $output = [
      '#attached' =&gt; [
        'drupalSettings' =&gt; $drupalsettings,
      ],
      '#theme' =&gt; $this-&gt;theme,
      '#options' =&gt; $this-&gt;options,
      '#data' =&gt; $this-&gt;data,
    ];
    return $output;
  }
}
Why exactly do you want to use an entity?

Entities are about CRUD. Saving, loading and deleting data in the database or somewhere else.

From looking at your code, that doesn't really seem to be what you're after, you just have an API to display some data. That's not what entities are about at all.

You could make a service of your class but that doesn't really seem to match either.

I guess the only thing that would make sense is to define it as a render element. See RenderElement and Render API overview.

So you'd define your class with @RenderElement in an Element namespace, see Link for an example. You give it a name in the annotation, like yourmodule_plot and in your render array, reference it as #type => 'yourmodule_plot' and pass the data to it like that.

A render element class basically consists of a getInfo() method that defines some default properties. In your case, that would be #theme, #attached and so on. if you need to dynamically process your data, you can add a #pre_render callback like shown in the Link example that can then process the merged array of your defaults + data you specify in the controller.