transmuter-1.0.0-alpha2/src/Plugin/TransmuterDefinition.php
src/Plugin/TransmuterDefinition.php
<?php
namespace Drupal\transmuter\Plugin;
use Drupal\Component\Plugin\Definition\DerivablePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\themespace\Plugin\Definition\ProviderTypedPluginDefinition;
/**
* Plugin definition class for element transmuter plugins.
*/
class TransmuterDefinition extends ProviderTypedPluginDefinition implements DerivablePluginDefinitionInterface {
/**
* Deriver class for preprocess plugin definitions.
*
* Deriver implements \Drupal\Component\Plugin\Derivative\DeriverInterface
* and return an array of element transmuter definition instances.
*
* @var class-string<\Drupal\Component\Plugin\Derivative\DeriverInterface>|null
*/
protected ?string $deriver = NULL;
/**
* The priority of this definition in relation to other transmuter plugins.
*
* This priority value helps to determine the order of preprocess plugins
* which implement the same theme hook and from the same extension type
* (module defined plugins always run before theme defined plugins).
*
* Plugins are sorted by ascending order of priority.
*
* @var int
*/
protected int $priority = 0;
/**
* The base hook to use when generated theme hooks.
*
* @var string|null
*
* @see \Drupal\transmuter\Attribute\Transmuter::__construct()
* @see \Drupal\transmuter\ThemeRegistryBuilder::doAlter()
*/
protected ?string $baseHook = NULL;
/**
* The theme hooks that the element transmuter plugin applies to.
*
* @var string[]
*/
protected array $hooks;
/**
* Create a new instance of the TransmuterDefinition class.
*
* @param array $definition
* The plugin definition values to set for this definition.
*
* @see \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function __construct(array $definition) {
if (empty($definition['hook']) || empty($definition['providerType'])) {
$msg = sprintf('Element transmuter "%s" definition is missing one of: hooks, provider type.', $definition['id']);
throw new InvalidPluginDefinitionException($msg);
}
// If plugin definition supports deriver definitions the set the deriver.
// Tempted to enforce that "class" is not empty, but a plugin manager
// might provider a default fallback class to use, meaning it can be valid
// that "class" could be empty here.
if (!empty($definition['deriver'])) {
$this->setDeriver($definition['deriver']);
}
elseif (empty($definition['class'])) {
$msg = sprintf('Element transmuter "%s" definition is missing class', $definition['id']);
throw new InvalidPluginDefinitionException($msg);
}
// Ensure that the hooks are always an array of hook values.
$definition['hook'] = (array) $definition['hook'];
// Assign values from the definition source.
[
'id' => $this->id,
'class' => $this->class,
'provider' => $this->provider,
'providerType' => $this->providerType,
'baseHook' => $this->baseHook,
'hook' => $this->hooks,
'priority' => $this->priority,
] = $definition + ['priority' => 0, 'baseHook' => NULL, 'class' => NULL];
}
/**
* {@inheritdoc}
*/
public function getDeriver(): ?string {
return $this->deriver;
}
/**
* {@inheritdoc}
*/
public function setDeriver($deriver): static {
$this->deriver = $deriver;
return $this;
}
/**
* The set priorty of this plugin definition (ascending order).
*
* The priority only is only compared and sorted against transmuter plugins
* with the same theme hook and extension type (module or theme).
*
* So theme provided plugins and hooks that are more specific ("node__page" vs
* "node") will still always applied later, regardless of this value.
*
* @return int
* The priority of this plugin definition, to use for sorting.
*/
public function getPriority(): int {
return $this->priority;
}
/**
* Get the base theme hook to use when generating new theme hooks.
*
* @return string|null
* The base hook to use when this theme hook needs to be created.
*/
public function getBaseHook(): ?string {
return $this->baseHook;
}
/**
* Get the list of render hooks this transmuter plugin applies to.
*
* @return string[]
* The list of theme render hooks this plugin applies to.
*/
public function getHooks(): array {
return $this->hooks;
}
}
