abstract class WarmerPluginBase in Warmer 8
Same name and namespace in other branches
- 2.x src/Plugin/WarmerPluginBase.php \Drupal\warmer\Plugin\WarmerPluginBase
Base class for warmer plugins that implement settings forms.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\warmer\Plugin\WarmerPluginBase implements ConfigurableInterface, DependentPluginInterface, ContainerFactoryPluginInterface, PluginFormInterface, WarmerInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WarmerPluginBase
See also
\Drupal\warmer\Annotation\Warmer
\Drupal\warmer\Plugin\WarmerPluginManager
\Drupal\warmer\Plugin\WarmerInterface
8 files declare their use of WarmerPluginBase
- CdnWarmer.php in modules/
warmer_cdn/ src/ Plugin/ warmer/ CdnWarmer.php - EnqueueForm.php in src/
Form/ EnqueueForm.php - EntityWarmer.php in modules/
warmer_entity/ src/ Plugin/ warmer/ EntityWarmer.php - HookImplementations.php in src/
HookImplementations.php - QueueManager.php in src/
QueueManager.php
File
- src/
Plugin/ WarmerPluginBase.php, line 25
Namespace
Drupal\warmer\PluginView source
abstract class WarmerPluginBase extends PluginBase implements ContainerFactoryPluginInterface, PluginFormInterface, ConfigurableInterface, WarmerInterface, DependentPluginInterface {
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state, TimeInterface $time) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->state = $state;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$settings = $container
->get('config.factory')
->get('warmer.settings')
->get('warmers');
$plugin_settings = empty($settings[$plugin_id]) ? [] : $settings[$plugin_id];
$configuration = array_merge($plugin_settings, $configuration);
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('state'), $container
->get('datetime.time'));
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return [
'id' => $this
->getPluginId(),
] + $this->configuration + $this
->defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
return $this;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'frequency' => 5 * 60,
'batchSize' => 50,
];
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$frequency = $form_state
->getValue('frequency');
$batch_size = $form_state
->getValue('batchSize');
if (!is_numeric($frequency) || $frequency < 0) {
$form_state
->setError($form[$this
->getPluginId()]['frequency'], $this
->t('Frequency should be a positive number.'));
}
if (!is_numeric($batch_size) || $batch_size < 1) {
$form_state
->setError($form[$this
->getPluginId()]['batchSize'], $this
->t('Batch size should be a number greater than 1.'));
}
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
/**
* {@inheritdoc}
*/
public final function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$configuration = $this
->getConfiguration() + $this
->defaultConfiguration();
$plugin_id = $configuration['id'];
$definition = $this
->getPluginDefinition();
$form[$plugin_id] = empty($form[$plugin_id]) ? [
'#type' => 'details',
'#open' => TRUE,
'#title' => empty($definition['label']) ? $plugin_id : $definition['label'],
'#group' => 'warmers',
'#tree' => TRUE,
] : $form[$plugin_id];
if (!empty($definition['description'])) {
$form[$plugin_id]['description'] = [
'#type' => 'html_tag',
'#tag' => 'em',
'#value' => $definition['description'],
];
}
$form[$plugin_id]['frequency'] = [
'#type' => 'number',
'#title' => $this
->t('Frequency'),
'#description' => $this
->t('Only re-enqueue warming operations after at least this many seconds have passed.'),
'#default_value' => $this
->getFrequency(),
];
$form[$plugin_id]['batchSize'] = [
'#type' => 'number',
'#title' => $this
->t('Batch Size'),
'#description' => $this
->t('Number of items to enqueue and process in a single go.'),
'#default_value' => $this
->getBatchSize(),
];
$subform_state = SubformState::createForSubform($form[$plugin_id], $form, $form_state);
$form[$plugin_id] = $this
->addMoreConfigurationFormElements($form[$plugin_id], $subform_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues() + $this->configuration);
}
/**
* {@inheritdoc}
*/
public function getBatchSize() {
$configuration = $this
->getConfiguration();
return $configuration['batchSize'];
}
/**
* {@inheritdoc}
*/
public function getFrequency() {
$configuration = $this
->getConfiguration();
return $configuration['frequency'];
}
/**
* {@inheritdoc}
*/
public function isActive() {
$configuration = $this
->getConfiguration();
$last_run = $this->state
->get('previous_enqueue_time:' . $configuration['id']);
return $this->time
->getRequestTime() > $last_run + $this
->getFrequency();
}
/**
* {@inheritdoc}
*/
public function markAsEnqueued() {
$configuration = $this
->getConfiguration();
$this->state
->set('previous_enqueue_time:' . $configuration['id'], $this->time
->getRequestTime());
}
}
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. | |
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. | |
WarmerInterface:: |
public | function | Adds additional form elements to the configuration form. | 3 |
WarmerInterface:: |
public | function | Builds the next batch of IDs based on a position cursor. | 3 |
WarmerInterface:: |
public | function | Loads multiple items based on their IDs. | 3 |
WarmerInterface:: |
public | function | Warms multiple items. | 3 |
WarmerPluginBase:: |
protected | property | The state service. | |
WarmerPluginBase:: |
protected | property | The time service. | |
WarmerPluginBase:: |
final public | function |
Form constructor. Overrides PluginFormInterface:: |
|
WarmerPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
WarmerPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
3 |
WarmerPluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
1 |
WarmerPluginBase:: |
public | function |
Returns the batch size for the warming operation. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WarmerPluginBase:: |
public | function |
Returns the frequency for the warming operation. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Checks if the plugin should warm in this particular moment. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Marks a warmer as enqueued. Overrides WarmerInterface:: |
|
WarmerPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
WarmerPluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
WarmerPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
WarmerPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |