Skip to content

add boilerplate code for plugin collection #423

@joachim-n

Description

@joachim-n

We have a ticky box that uses EntityWithPluginCollectionInterface, but then it crashes because we don't implement the getPluginCollections method.

But that method needs a ton of boilerplate.

@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace Drupal\foo\Entity;
 
 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
+use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
 
 /**
  * Provides the Booking Unit Type entity.
@@ -39,6 +40,8 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
  *   config_export = {
  *     "id",
  *     "label",
+ *     "foo_type",
+ *     "foo_type_config",
  *   },
  *   links = {
  *     "add-form" = "/admin/structure/foo_unit_type/add",

and

    */
   protected $label = '';
 
+  /**
+   * The foo type plugin ID.
+   *
+   * @var string
+   */
+  protected $foo_type = '';
+
+  /**
+   * The foo type plugin configuration.
+   *
+   * @var array
+   */
+  protected $foo_type_config = [];
+
+  /**
+   * The plugin collection that holds the foo type plugin for this entity.
+   *
+   * @var \Drupal\Component\Plugin\DefaultSingleLazyPluginCollection
+   */
+  protected $fooPluginCollection;
+
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getPluginCollections() {
+    return [
+      'foo_type_config' => $this->getBookingTypePluginCollection(),
+    ];
+  }
+
+  /**
+   * Encapsulates the creation of the foo type plugin collection.
+   *
+   * @return \Drupal\Component\Plugin\DefaultSingleLazyPluginCollection
+   *   The plugin collection.
+   */
+  protected function getBookingTypePluginCollection() {
+    if (!$this->fooPluginCollection) {
+      $this->fooPluginCollection = new DefaultSingleLazyPluginCollection(
+        \Drupal::service('plugin.manager.foo_type'),
+        $this->foo_type,
+        $this->foo_type_config,
+      );
+    }
+    return $this->fooPluginCollection;
+  }
+
 }

& config schema for the 2 new properties:

     label:
       type: label
       label: Name
+    foo_type:
+      type: string
+      label: Foo type
+    foo_type_config:
+      type: foo.foo_availability.plugin.[%parent.content_plugin]
+      label: 'Foo configuration'
+
+# Schema for foo type content plugins.
+foo.foo_availability.plugin.*:
+  type: mapping
+  label: 'Foo type configuration'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions