View source
<?php
namespace Drupal\duration_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\duration_field\Service\DurationService;
class DurationField extends FieldItemBase implements FieldItemInterface {
public static function defaultFieldSettings() {
return [
'granularity' => [
'year' => TRUE,
'month' => TRUE,
'day' => TRUE,
'hour' => TRUE,
'minute' => TRUE,
'second' => TRUE,
],
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$default_values = [];
foreach ($this
->getSetting('granularity') as $key => $value) {
if ($value) {
$default_values[] = $key;
}
}
$element['granularity'] = [
'#title' => $this
->t('Granularity'),
'#type' => 'checkboxes',
'#options' => [
'year' => $this
->t('Years'),
'month' => $this
->t('Months'),
'day' => $this
->t('Days'),
'hour' => $this
->t('Hours'),
'minute' => $this
->t('Minutes'),
'second' => $this
->t('Seconds'),
],
'#default_value' => $default_values,
];
return $element;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
],
];
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Duration'));
return $properties;
}
public function setValue($values, $notify = TRUE) {
if (is_array($values) && isset($values['value']) && is_array($values['value'])) {
$values['value'] = DurationService::convertValue($values['value']);
}
parent::setValue($values, $notify);
}
}