DateTimeItem.php in Zircon Profile 8.0
File
core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php
View source
<?php
namespace Drupal\datetime\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
class DateTimeItem extends FieldItemBase {
public static function defaultStorageSettings() {
return array(
'datetime_type' => 'datetime',
) + parent::defaultStorageSettings();
}
const DATETIME_TYPE_DATE = 'date';
const DATETIME_TYPE_DATETIME = 'datetime';
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');
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'description' => 'The date value.',
'type' => 'varchar',
'length' => 20,
),
),
'indexes' => array(
'value' => array(
'value',
),
),
);
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = array();
$element['datetime_type'] = array(
'#type' => 'select',
'#title' => t('Date type'),
'#description' => t('Choose the type of date to create.'),
'#default_value' => $this
->getSetting('datetime_type'),
'#options' => array(
static::DATETIME_TYPE_DATETIME => t('Date and time'),
static::DATETIME_TYPE_DATE => t('Date only'),
),
);
return $element;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$type = $field_definition
->getSetting('datetime_type');
$timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
$values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $timestamp);
}
else {
$values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $timestamp);
}
return $values;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function onChange($property_name, $notify = TRUE) {
if ($property_name == 'value') {
$this->date = NULL;
}
parent::onChange($property_name, $notify);
}
}
Classes
Name |
Description |
DateTimeItem |
Plugin implementation of the 'datetime' field type. |