ScheduledPublish.php in Scheduled Publish 8.3
File
src/Plugin/Field/FieldType/ScheduledPublish.php
View source
<?php
namespace Drupal\scheduled_publish\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class ScheduledPublish extends FieldItemBase implements DateTimeItemInterface {
public static function defaultStorageSettings() {
return [];
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
return [];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('datetime_iso8601')
->setLabel(t('Date value'))
->setRequired(TRUE);
$properties['date'] = DataDefinition::create('any')
->setLabel(t('Computed date'))
->setDescription(t('The computed DateTime object.'))
->setComputed(TRUE)
->setClass('\\Drupal\\datetime\\DateTimeComputed')
->setSetting('date source', 'value');
$properties['moderation_state'] = DataDefinition::create('string')
->setLabel(t('The moderation state.'));
return $properties;
}
public function onChange($property_name, $notify = TRUE) {
if ($property_name === 'value') {
$this->date = NULL;
}
parent::onChange($property_name, $notify);
}
public static function schema(FieldStorageDefinitionInterface $field_definition) : array {
$schema = [
'columns' => [
'moderation_state' => [
'type' => 'varchar',
'length' => 32,
],
'value' => [
'description' => 'The date value.',
'type' => 'varchar',
'length' => 20,
],
],
'indexes' => [
'value' => [
'value',
],
],
];
return $schema;
}
public function isEmpty() {
$is_moderation_state = empty($this
->get('moderation_state')
->getValue());
$is_value = empty($this
->get('value')
->getValue());
return $is_moderation_state || $is_value;
}
}