Views Plugins (Part 1) : Simple area handler plugin
For the example we are going to implement an area that will present some links and text in a custom way, not sure if it's really usefull, but that not the point of this article.
The Plugin system
For the first post on the plugins I will introduce briefly on the concept. For those that already been using Ctools plugins system, you already now about the plugin system purposes.
For those who doesn't know about it, the plugin system is a way to let other module implements her own use case for an existing features, think of Field formatter : provide your own render array for a particular field display, or Widget : provide your own form element for a particular field type, etc...
The plugin system has three base elements :
The plugin type is the central controlling class that defines how the plugins of this type will be discovered and instantiated. The type will describe the central purpose of all plugins of that type; e.g. cache backends, image actions, blocks, etc.
Plugin Discovery is the process of finding plugins within the available code base that qualify for use within this particular plugin type's use case.
The Factory is responsible for instantiating the specific plugin(s) chosen for a given use case.
Detailled informations : https://www.drupal.org/node/1637730
In our case Views is responsible of that implementations so we are not going further on that, let see now how to implement a plugin definition.
The Plugin definitions
The existing documentation on the plugin definitions are a little abstract for now to understand how it really works (https://www.drupal.org/node/1653532).
You have to understand simply that a Plugin in most case is a Class implementation, namespaced within the namespace of the plugin type, in our example this is : \Drupal\module_name\Plugin\views\area
So if I implement a custom views area Plugin in my module the class will be located under the location module_name/src/Plugin/views/area/MyAreaHandler.php
To know where to implement a plugin definition for a plugin type, you can in most case look at module docs, or directly in the source code of the module (looking at an example of a definition will be enough)
In most cases, the modules that implement a Plugin type will provide a base class for the plugins definitions, in our example views area provide a base class : \Drupal\views\Plugin\views\area\AreaPluginBase
Drupal provide also a base class, if you implement a custom Plugin type, for the Plugin definition : \Drupal\Component\Plugin\PluginBase
Your custom plugin definition class must also have annotation metadata, that is defined by the module that implement the plugin type, in our example : \Drupal\views\Annotation\ViewsArea
In the case of views you will also need to implement the hook_views_data() into module_name.views.inc file, there you will inform views about the name and metadata of your Area handler.
Hands on implementation
So we have a custom module let's call it module_name for the example :)
We will create the class that implements our plugin definition and we are gonna give it this Plugin ID : my_custom_site_area.
We save this file into module_name/src/Plugin/views/area/MyCustomSiteArea.php
Now we just have to implements the hook_views_data() and yes this is the end, you can use your awesome views area handler into any view and any area.
Define this hook into the file : module_name/module_name.views.inc