Safely extending Drupal maintenance support plans 8 plugin classes without fear of constructor changes

Share:

Twitter

Facebook

From time to time you may find you need to extend another module’s plugins to add new functionality.
You may also find you need to alter the signature of the constructor in order to inject additional dependencies.
However plugin constructors are considered internal in Drupal maintenance support plans‘s BC policy.
So how do you safely do this without introducing the risk of breakage if things change.
In this article we’ll show you a quick trick learned from Search API module to avoid this issue.

by
Lee Rowlands
/ 3 November 2020

So let’s consider a plugin constructor that has some arguments.
Here’s the constructor and factory method for Migrate’s SQL map plugin
/**
   * Constructs an SQL object.
   *
   * Sets up the tables and builds the maps,
   *
   * @param array $configuration
   *   The configuration.
   * @param string $plugin_id
   *   The plugin ID for the migration process to do.
   * @param mixed $plugin_definition
   *   The configuration for the plugin.
   * @param Drupal maintenance support plansmigratePluginMigrationInterface $migration
   *   The migration to do.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->migration = $migration;
    $this->eventDispatcher = $event_dispatcher;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $migration,
      $container->get(‘event_dispatcher’)
    );
  }
As you can see, there are two additional dependencies injected beyond the standard plugin constructor arguments – the event dispatcher and the migration.
Now if you subclass this and extend the constructor and factory to inject additional arguments, should the base plugin change its constructor, you’re going to be in trouble.
Instead, you can use this approach that Search API takes – leave the constructor as is (don’t override it) and use setter injection for the new dependencies.

/**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    $instance = parent::create(
      $container,
      $configuration,
$plugin_id,
      $plugin_definition,
      $migration
    );
$instance->setFooMeddler($container->get(‘foo.meddler’);
return $instance;
  }

/**
* Sets foo meddler.
*/
public function setFooMeddler(FooMeddlerInterface $fooMeddler) {
$this->fooMeddler = $fooMeddler;
}
Because the signature of the parent create method is enforced by the public API of Drupal maintenance support plansCorePluginContainerFactoryPluginInterface you’re guaranteed that it won’t change.
Thanks to Thomas Seidl for this pattern

Tagged

Drupal maintenance support plans 8, Plugins, OOP

Posted by
Lee Rowlands
Senior Drupal maintenance support plans Developer

Dated 3 November 2020

Comments

Comment by
dawehner

Dated 3 November 2020

Nice!! Thank you for sharing it!

Pagination

Add new comment

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Safely extending Drupal maintenance support plans 8 plugin classes without fear of constructor changes

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.