abstract class NumberPatternBase in Commerce Core 8.2
Provides a base class for number pattern plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_number_pattern\Plugin\Commerce\NumberPattern\NumberPatternBase implements NumberPatternInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of NumberPatternBase
File
- modules/
number_pattern/ src/ Plugin/ Commerce/ NumberPattern/ NumberPatternBase.php, line 16
Namespace
Drupal\commerce_number_pattern\Plugin\Commerce\NumberPatternView source
abstract class NumberPatternBase extends PluginBase implements NumberPatternInterface, ContainerFactoryPluginInterface {
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The parent config entity.
*
* Not available while the plugin is being configured.
*
* @var \Drupal\commerce_number_pattern\Entity\NumberPatternInterface
*/
protected $parentEntity;
/**
* Constructs a new NumberPatternBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
if (array_key_exists('_entity', $configuration)) {
$this->parentEntity = $configuration['_entity'];
unset($configuration['_entity']);
}
$this
->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'));
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'pattern' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$entity_type_id = $form_state
->get('target_entity_type');
$token_types = [
'pattern',
];
if ($entity_type_id) {
$token_types[] = $entity_type_id;
}
$form['pattern'] = [
'#title' => $this
->t('Pattern'),
'#type' => 'textfield',
'#description' => $this
->t('Allows adding a prefix (such as "INV-") or a suffix to the number.'),
'#default_value' => $this->configuration['pattern'],
'#required' => TRUE,
'#element_validate' => [
'token_element_validate',
],
'#token_types' => $token_types,
];
$form['pattern_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
'#global_types' => FALSE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration = [];
$this->configuration['pattern'] = $values['pattern'];
}
}
/**
* {@inheritdoc}
*/
public function generate(ContentEntityInterface $entity) {
$number = $this->token
->replace($this->configuration['pattern'], [
'pattern' => [],
$entity
->getEntityTypeId() => $entity,
]);
return $number;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
NumberPatternBase:: |
protected | property | The parent config entity. | |
NumberPatternBase:: |
protected | property | The token service. | |
NumberPatternBase:: |
public | function | 1 | |
NumberPatternBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
NumberPatternBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
1 |
NumberPatternBase:: |
public | function |
Generates a number for the given content entity. Overrides NumberPatternInterface:: |
1 |
NumberPatternBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
NumberPatternBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
NumberPatternBase:: |
public | function | 1 | |
NumberPatternBase:: |
public | function | 1 | |
NumberPatternBase:: |
public | function |
Constructs a new NumberPatternBase object. Overrides PluginBase:: |
1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |