first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
services:
plugin.manager.{{ plugin_type }}:
class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager
parent: default_plugin_manager

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines {{ plugin_type }} annotation object.
*
* @Annotation
*/
final class {{ class_prefix }} extends Plugin {
/**
* The plugin ID.
*/
public readonly string $id;
/**
* The human-readable name of the plugin.
*
* @ingroup plugin_translatable
*/
public readonly string $title;
/**
* The description of the plugin.
*
* @ingroup plugin_translatable
*/
public readonly string $description;
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
/**
* Interface for {{ plugin_type }} plugins.
*/
interface {{ class_prefix }}Interface {
/**
* Returns the translated plugin label.
*/
public function label(): string;
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Component\Plugin\PluginBase;
/**
* Base class for {{ plugin_type }} plugins.
*/
abstract class {{ class_prefix }}PluginBase extends PluginBase implements {{ class_prefix }}Interface {
/**
* {@inheritdoc}
*/
public function label(): string {
// Cast the label to a string since it is a TranslatableMarkup object.
return (string) $this->pluginDefinition['label'];
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% apply sort_namespaces %}
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\{{ machine_name }}\Annotation\{{ class_prefix }};
{% endapply %}
/**
* {{ class_prefix }} plugin manager.
*/
final class {{ class_prefix }}PluginManager extends DefaultPluginManager {
/**
* Constructs the object.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/{{ class_prefix }}', $namespaces, $module_handler, {{ class_prefix }}Interface::class, {{ class_prefix }}::class);
$this->alterInfo('{{ plugin_type }}_info');
$this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins');
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\{{ class_prefix }};
use Drupal\{{ machine_name }}\{{ class_prefix }}PluginBase;
/**
* Plugin implementation of the {{ plugin_type }}.
*
* @{{ class_prefix }}(
* id = "foo",
* label = @Translation("Foo"),
* description = @Translation("Foo description.")
* )
*/
final class Foo extends {{ class_prefix }}PluginBase {
}

View File

@@ -0,0 +1,4 @@
services:
plugin.manager.{{ plugin_type }}:
class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager
parent: default_plugin_manager

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Attribute;
use Drupal\Component\Plugin\Attribute\AttributeBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* The {{ plugin_type }} attribute.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class {{ class_prefix }} extends AttributeBase {
/**
* Constructs a new {{ class_prefix }} instance.
*
* @param string $id
* The plugin ID. There are some implementation bugs that make the plugin
* available only if the ID follows a specific pattern. It must be either
* identical to group or prefixed with the group. E.g. if the group is "foo"
* the ID must be either "foo" or "foo:bar".
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label
* (optional) The human-readable name of the plugin.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* (optional) A brief description of the plugin.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly ?TranslatableMarkup $label,
public readonly ?TranslatableMarkup $description = NULL,
public readonly ?string $deriver = NULL,
) {}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
/**
* Interface for {{ plugin_type }} plugins.
*/
interface {{ class_prefix }}Interface {
/**
* Returns the translated plugin label.
*/
public function label(): string;
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Component\Plugin\PluginBase;
/**
* Base class for {{ plugin_type }} plugins.
*/
abstract class {{ class_prefix }}PluginBase extends PluginBase implements {{ class_prefix }}Interface {
/**
* {@inheritdoc}
*/
public function label(): string {
// Cast the label to a string since it is a TranslatableMarkup object.
return (string) $this->pluginDefinition['label'];
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
{% apply sort_namespaces %}
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\{{ machine_name }}\Attribute\{{ class_prefix }};
{% endapply %}
/**
* {{ class_prefix }} plugin manager.
*/
final class {{ class_prefix }}PluginManager extends DefaultPluginManager {
/**
* Constructs the object.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/{{ class_prefix }}', $namespaces, $module_handler, {{ class_prefix }}Interface::class, {{ class_prefix }}::class);
$this->alterInfo('{{ plugin_type }}_info');
$this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins');
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }}\Plugin\{{ class_prefix }};
{% apply sort_namespaces %}
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\{{ machine_name }}\Attribute\{{ class_prefix }};
use Drupal\{{ machine_name }}\{{ class_prefix }}PluginBase;
{% endapply %}
/**
* Plugin implementation of the {{ plugin_type }}.
*/
#[{{ class_prefix }}(
id: 'foo',
label: new TranslatableMarkup('Foo'),
description: new TranslatableMarkup('Foo description.'),
)]
final class Foo extends {{ class_prefix }}PluginBase {
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
/**
* @file
* Primary module hooks for {{ name }} module.
*/
/**
* Implements hook_{{ plugin_type }}_info().
*/
function {{ machine_name }}_{{ plugin_type }}_info(): array {
return [
'{{ machine_name }}.foo' => [
'id' => 'foo',
'label' => t('Foo'),
'description' => t('Foo description.'),
],
'{{ machine_name }}.bar' => [
'id' => 'bar',
'label' => t('Bar'),
'description' => t('Bar description.'),
],
];
}

View File

@@ -0,0 +1,4 @@
services:
plugin.manager.{{ plugin_type }}:
class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager
arguments: ['@module_handler', '@cache.discovery']

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Plugin\PluginBase;
/**
* Default class used for {{ plugin_type|pluralize }} plugins.
*/
final class {{ class_prefix }}Default extends PluginBase implements {{ class_prefix }}Interface {
/**
* {@inheritdoc}
*/
public function label(): string {
// The title from hook discovery may be a TranslatableMarkup object.
return (string) $this->pluginDefinition['label'];
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
/**
* Interface for {{ plugin_type }} plugins.
*/
interface {{ class_prefix }}Interface {
/**
* Returns the translated plugin label.
*/
public function label(): string;
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\HookDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
/**
* Defines a plugin manager to deal with {{ plugin_type|pluralize }}.
*
* @see \Drupal\{{ machine_name }}\{{ class_prefix }}Default
* @see \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
*/
final class {{ class_prefix }}PluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = [
// The {{ plugin_type }} id. Set by the plugin system based on the array key.
'id' => '',
// The {{ plugin_type }} label.
'label' => '',
// The {{ plugin_type }} description.
'description' => '',
// Default plugin class.
'class' => {{ class_prefix }}Default::class,
];
/**
* Constructs {{ class_prefix }}PluginManager object.
*/
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->factory = new ContainerFactory($this);
$this->moduleHandler = $module_handler;
$this->alterInfo('{{ plugin_type }}_info');
$this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins');
}
/**
* {@inheritdoc}
*/
protected function getDiscovery(): HookDiscovery {
if (!isset($this->discovery)) {
$this->discovery = new HookDiscovery($this->moduleHandler, '{{ plugin_type }}_info');
}
return $this->discovery;
}
}

View File

@@ -0,0 +1,9 @@
{{ machine_name }}.example_1:
label: 'Example 1'
description: 'Plugin description.'
{{ machine_name }}.example_2:
label: 'Example 2'
description: 'Plugin description.'
{{ machine_name }}.example_3:
label: 'Example 3'
description: 'Plugin description.'

View File

@@ -0,0 +1,4 @@
services:
plugin.manager.{{ plugin_type }}:
class: Drupal\{{ machine_name }}\{{ class_prefix }}PluginManager
arguments: ['@module_handler', '@cache.discovery']

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Plugin\PluginBase;
/**
* Default class used for {{ plugin_type|pluralize }} plugins.
*/
final class {{ class_prefix }}Default extends PluginBase implements {{ class_prefix }}Interface {
/**
* {@inheritdoc}
*/
public function label(): string {
// The title from YAML file discovery may be a TranslatableMarkup object.
return (string) $this->pluginDefinition['label'];
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
/**
* Interface for {{ plugin_type }} plugins.
*/
interface {{ class_prefix }}Interface {
/**
* Returns the translated plugin label.
*/
public function label(): string;
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Drupal\{{ machine_name }};
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
/**
* Defines a plugin manager to deal with {{ plugin_type|pluralize }}.
*
* Modules can define {{ plugin_type|pluralize }} in a MODULE_NAME.{{ plugin_type|pluralize }}.yml file contained
* in the module's base directory. Each {{ plugin_type }} has the following structure:
*
* @code
* MACHINE_NAME:
* label: STRING
* description: STRING
* @endcode
*
* @see \Drupal\{{ machine_name }}\{{ class_prefix }}Default
* @see \Drupal\{{ machine_name }}\{{ class_prefix }}Interface
*/
final class {{ class_prefix }}PluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = [
// The {{ plugin_type }} id. Set by the plugin system based on the top-level YAML key.
'id' => '',
// The {{ plugin_type }} label.
'label' => '',
// The {{ plugin_type }} description.
'description' => '',
// Default plugin class.
'class' => {{ class_prefix }}Default::class,
];
/**
* Constructs {{ class_prefix }}PluginManager object.
*/
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->factory = new ContainerFactory($this);
$this->moduleHandler = $module_handler;
$this->alterInfo('{{ plugin_type }}_info');
$this->setCacheBackend($cache_backend, '{{ plugin_type }}_plugins');
}
/**
* {@inheritdoc}
*/
protected function getDiscovery(): YamlDiscovery {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('{{ plugin_type|pluralize }}', $this->moduleHandler->getModuleDirectories());
$this->discovery->addTranslatableProperty('label', 'label_context');
$this->discovery->addTranslatableProperty('description', 'description_context');
}
return $this->discovery;
}
}