class Sandwich
Defines a Sandwich attribute class.
Provides an example of how to define a new attribute type for use in defining a plugin type. Demonstrates documenting the arguments for the class constructor to help developers understand how to configure plugin instances.
When defining attributes for a plugin type start by extending the existing Drupal\Component\Plugin\Attribute\Plugin class. Then create a __construct() method whose arguments are the configuration options for the plugin type.
Classes that extend the Plugin class should always accept $id, $deriver arguments. Developers can add any additional required, or optional, arguments necessary to configure their specific plugin type.
The #[\Attribute(\Attribute::TARGET_CLASS)] line says that the class, Block, defines a #[Block()] attribute, and that the block attribute can only be used on classes. Or put another way, the code that the #[Block()] attribute is annotating must be a class, or it won’t validate.
Attributes
Hierarchy
- class \Drupal\Component\Plugin\Attribute\AttributeBase implements \Drupal\Component\Plugin\Attribute\AttributeInterface
- class \Drupal\Component\Plugin\Attribute\Plugin extends \Drupal\Component\Plugin\Attribute\AttributeBase
- class \Drupal\plugin_type_example\Attribute\Sandwich extends \Drupal\Component\Plugin\Attribute\Plugin
- class \Drupal\Component\Plugin\Attribute\Plugin extends \Drupal\Component\Plugin\Attribute\AttributeBase
Expanded class hierarchy of Sandwich
See also
\Drupal\plugin_type_example\SandwichPluginManager
3 files declare their use of Sandwich
- ExampleHamSandwich.php in modules/
plugin_type_example/ src/ Plugin/ Sandwich/ ExampleHamSandwich.php - ExampleMeatballSandwich.php in modules/
plugin_type_example/ src/ Plugin/ Sandwich/ ExampleMeatballSandwich.php - SandwichPluginManager.php in modules/
plugin_type_example/ src/ SandwichPluginManager.php
File
-
modules/
plugin_type_example/ src/ Attribute/ Sandwich.php, line 31
Namespace
Drupal\plugin_type_example\AttributeView source
class Sandwich extends Plugin {
/**
* Construct a sandwich attribute.
*
* @param string $id
* The plugin ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
* (optional) Description of the sandwich.
* @param float|null $calories
* (optional) Number of calories in the sandwich.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(public readonly string $id, public readonly ?TranslatableMarkup $description = NULL, public readonly ?float $calories = NULL, public readonly ?string $deriver = NULL) {
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|
| AttributeBase::$class | protected | property | The class used for this attribute class. | ||
| AttributeBase::$provider | protected | property | The provider of the attribute class. | ||
| AttributeBase::get | public | function | Gets the value of an attribute. | Overrides AttributeInterface::get | 6 |
| AttributeBase::getClass | public | function | Gets the class of the attribute class. | Overrides AttributeInterface::getClass | 1 |
| AttributeBase::getId | public | function | Gets the unique ID for this attribute class. | Overrides AttributeInterface::getId | |
| AttributeBase::getProvider | public | function | Gets the name of the provider of the attribute class. | Overrides AttributeInterface::getProvider | |
| AttributeBase::setClass | public | function | Sets the class of the attributed class. | Overrides AttributeInterface::setClass | 1 |
| AttributeBase::setProvider | public | function | Sets the name of the provider of the attribute class. | Overrides AttributeInterface::setProvider | |
| Sandwich::__construct | public | function | Construct a sandwich attribute. | Overrides Plugin::__construct |