Creating a custom plugin type
The plugin system provides a means to create specialized objects in Drupal that do not require the data storage features of the entity system.
This recipe is based on the GeoIP API module port to Drupal 8 that was started by the author. The GeoIP API module provides a way to get the country from a website visitor's IP address.
In this recipe, we will create a new plugin type called GeoLocator
that will return the country code for a given IP address. We will create a plugin manager, a default plugin interface, a plugin annotation definition, and provide a default plugin to find the country via the website's CDN.
Getting ready
We will use the geoip
namespace and module name in this recipe.
How to do it...
- All plugins need to have a service that acts as a plugin manager. Create a new file in your module's
src
directory calledGeoLocatorManager.php
. This will hold theGeoLocatorManager
class. - Create the
GeoLocatorManager
class by extending the\Drupal\Core\Plugin\DefaultPluginManager...