lightning_scheduler.module in Lightning Scheduler 8
File
lightning_scheduler.module
View source
<?php
use Drupal\Core\Cache\CacheCollectorInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Update\UpdateKernel;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\lightning_scheduler\Plugin\Field\FieldWidget\ModerationStateWidget;
function lightning_scheduler_library_info_alter(array &$libraries, $extension) {
if ($extension === 'seven') {
$dir = drupal_get_path('module', 'lightning_scheduler');
$libraries['lightning_scheduler.widget'] = [
'css' => [
'theme' => [
"/{$dir}/css/component.seven.css" => [],
],
],
];
}
}
function lightning_scheduler_system_info_alter(array &$info, Extension $extension, $type) {
if ($type === 'theme' && $extension
->getName() === 'seven') {
$info['libraries-extend']['lightning_scheduler/widget'][] = 'seven/lightning_scheduler.widget';
}
}
function lightning_scheduler_cron() {
if (Drupal::service('kernel') instanceof UpdateKernel) {
return;
}
$field_map = Drupal::service('entity_field.manager')
->getFieldMap();
$transition_manager = Drupal::service('lightning_scheduler.transition_manager');
$start = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
$now = Drupal::time()
->getRequestTime();
$start
->setTimestamp($now);
foreach ($field_map as $entity_type_id => $fields) {
if (isset($fields['scheduled_transition_state'], $fields['scheduled_transition_date'])) {
$transition_manager
->process($entity_type_id, $start);
}
}
$state = Drupal::state();
if ($state instanceof CacheCollectorInterface) {
$state
->resetCache();
}
}
function lightning_scheduler_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
$moderation_info = Drupal::service('content_moderation.moderation_information');
if ($moderation_info
->canModerateEntitiesOfEntityType($entity_type)) {
$fields['scheduled_transition_date'] = BaseFieldDefinition::create('datetime')
->setDisplayConfigurable('view', FALSE)
->setDisplayConfigurable('form', FALSE)
->setLabel(t('Scheduled transition date'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$fields['scheduled_transition_state'] = BaseFieldDefinition::create('string')
->setDisplayConfigurable('view', FALSE)
->setDisplayConfigurable('form', FALSE)
->setLabel(t('Scheduled transition state'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
}
return $fields;
}
function lightning_scheduler_field_widget_info_alter(array &$info) {
if (!empty($info['moderation_state_default'])) {
$info['moderation_state_default']['class'] = ModerationStateWidget::class;
}
}
function lightning_scheduler_local_tasks_alter(array &$local_tasks) {
$local_tasks['entity.node.edit_form']['options']['attributes']['rel'] = 'edit-form';
}