devel_wizard-2.x-dev/templates/spell/plugin_manager/definition.php.twig
templates/spell/plugin_manager/definition.php.twig
<?php
declare(strict_types=1);
namespace {{ definition.namespace }};
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class {{ definition.class }} extends PluginDefinition implements \JsonSerializable {
/**
* The human-readable category.
*/
protected string|TranslatableMarkup $category;
/**
* Gets the human-readable category of the {{ label }} definition.
*/
public function getCategory(): string|TranslatableMarkup {
return $this->category;
}
/**
* Sets the human-readable category of the {{ label }} definition.
*/
public function setCategory(string|TranslatableMarkup $category): static {
$this->category = $category;
return $this;
}
/**
* The human-readable name.
*/
protected string|TranslatableMarkup $label;
/**
* Gets the human-readable name of the {{ label }} definition.
*/
public function getLabel(): string|TranslatableMarkup {
return $this->label;
}
/**
* Sets the human-readable name of the {{ label }} definition.
*/
public function setLabel(string|TranslatableMarkup $label): static {
$this->label = $label;
return $this;
}
/**
* Long description of this plugin instance.
*/
protected string|TranslatableMarkup $description;
/**
* Gets the description of this {{ label }} plugin.
*/
public function getDescription(): string|TranslatableMarkup {
return $this->description;
}
/**
* Sets the description of this {{ label }} definition.
*/
public function setDescription(string|TranslatableMarkup $description): static {
$this->description = $description;
return $this;
}
protected array $additional = [];
public function __construct(array $definition) {
if (array_key_exists('category', $definition)) {
$this->setCategory($definition['category']);
}
if (array_key_exists('label', $definition)) {
$this->setLabel($definition['label']);
}
if (array_key_exists('description', $definition)) {
$this->setDescription($definition['description']);
}
unset(
$definition['category'],
$definition['label'],
$definition['description'],
);
foreach ($definition as $property => $value) {
$this->set($property, $value);
}
}
/**
* {@inheritdoc}
*/
public function jsonSerialize(): array {
$state = [
'provider' => $this->getProvider(),
'id' => $this->id(),
'class' => $this->getClass(),
'category' => $this->getCategory()->getUntranslatedString(),
'label' => $this->getLabel()->getUntranslatedString(),
'description' => $this->getDescription()->getUntranslatedString(),
];
return $state + $this->additional;
}
/**
* Gets any arbitrary property.
*/
public function get(string $property): mixed {
if (property_exists($this, $property)) {
return $this->{$property};
}
return $this->additional[$property] ?? NULL;
}
/**
* Sets a value to an arbitrary property.
*/
public function set(string $property, mixed $value): static {
// @todo Do the following keys need protection: provider, id, class?
if (property_exists($this, $property)) {
$this->{$property} = $value;
return $this;
}
$this->additional[$property] = $value;
return $this;
}
}
