View source
<?php
namespace Drupal\ultimate_cron;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;
class CronPlugin extends PluginBase implements PluginInspectionInterface, ConfigurableInterface, DependentPluginInterface, PluginFormInterface {
public static $multiple = FALSE;
public static $instances = array();
public $weight = 0;
public static $globalOptions = array();
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public static function getPluginTypes() {
return array(
'scheduler' => t('Scheduler'),
'launcher' => t('Launcher'),
'logger' => t('Logger'),
);
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = array_merge($this
->defaultConfiguration(), $configuration);
}
public function defaultConfiguration() {
return array();
}
public function calculateDependencies() {
return array();
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return array();
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public static function getGlobalOption($name) {
return isset(static::$globalOptions[$name]) ? static::$globalOptions[$name] : NULL;
}
public static function getGlobalOptions() {
return static::$globalOptions;
}
public static function setGlobalOption($name, $value) {
static::$globalOptions[$name] = $value;
}
public function settingsLabel($name, $value) {
if (is_array($value)) {
return implode(', ', $value);
}
else {
return $value;
}
}
public function isValid($job = NULL) {
return TRUE;
}
public function drupal_array_remove_nested_value(array &$array, array $parents) {
$ref =& $array;
$last_parent = array_pop($parents);
foreach ($parents as $parent) {
if (is_array($ref) && array_key_exists($parent, $ref)) {
$ref =& $ref[$parent];
}
else {
return;
}
}
unset($ref[$last_parent]);
}
public function cleanForm($elements, &$values, $parents) {
if (empty($elements)) {
return;
}
foreach (element_children($elements) as $child) {
if (empty($child) || empty($elements[$child]) || is_numeric($child)) {
continue;
}
$this
->cleanForm($elements[$child], $values, $parents);
$rel_parents = array_diff($elements[$child]['#parents'], $parents);
$key_exists = NULL;
$value = drupal_array_get_nested_value($values, $rel_parents, $key_exists);
if (!empty($elements[$child]['#markup'])) {
static::drupal_array_remove_nested_value($values, $rel_parents);
}
elseif ($key_exists && empty($value) && !empty($elements[$child]['#fallback']) && $value !== '0') {
static::drupal_array_remove_nested_value($values, $rel_parents);
}
}
}
public function fallbackalize(&$elements, &$values, $defaults, $remove_non_fallbacks = FALSE) {
if (empty($elements)) {
return;
}
foreach (element_children($elements) as $child) {
$element =& $elements[$child];
if (empty($element['#tree'])) {
$param_values =& $values;
$param_defaults =& $defaults;
}
else {
$param_values =& $values[$child];
$param_defaults =& $defaults[$child];
}
$this
->fallbackalize($element, $param_values, $param_defaults, $remove_non_fallbacks);
if (empty($element['#type']) || $element['#type'] == 'fieldset') {
continue;
}
if (!empty($element['#fallback'])) {
if (!$remove_non_fallbacks) {
if ($element['#type'] == 'radios') {
$label = $this
->settingsLabel($child, $defaults[$child]);
$element['#options'] = array(
'' => t('Default (@default)', array(
'@default' => $label,
)),
) + $element['#options'];
}
elseif ($element['#type'] == 'select' && empty($element['#multiple'])) {
$label = $this
->settingsLabel($child, $defaults[$child]);
$element['#options'] = array(
'' => t('Default (@default)', array(
'@default' => $label,
)),
) + $element['#options'];
}
elseif ($defaults[$child] !== '') {
$element['#description'] .= ' ' . t('(Blank = @default).', array(
'@default' => $this
->settingsLabel($child, $defaults[$child]),
));
}
unset($element['#required']);
}
}
elseif (!empty($element['#type']) && $remove_non_fallbacks) {
unset($elements[$child]);
}
elseif (!isset($element['#default_value']) || $element['#default_value'] === '') {
$empty = $element['#type'] == 'checkbox' ? FALSE : '';
$values[$child] = !empty($defaults[$child]) ? $defaults[$child] : $empty;
$element['#default_value'] = $values[$child];
}
}
}
}