View source
<?php
namespace Drupal\duration_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\duration_field\Service\DurationServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DurationWidget extends WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface {
protected $durationService;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, DurationServiceInterface $durationService) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->durationService = $durationService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('duration_field.service'));
}
public static function defaultSettings() {
return [
'duration' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['duration'] = [
'#type' => 'duration',
'#title' => $this
->t('Duration'),
'#granularity' => $this
->getGranularity(),
'#default_value' => $this
->getSetting('duration'),
'#element_validate' => [
[
$this,
'settingsFormValidate',
],
],
];
return $element;
}
public function settingsFormValidate($element, FormStateInterface $form_state) {
$duration = $form_state
->getValue($element['#parents']);
if ($error_message = $this->durationService
->checkDurationInvalid($this->durationService
->convertValue($duration))) {
$form_state
->setError($element, $error_message);
}
}
public function settingsSummary() {
$summary = [];
$summary[] = t('Default Duration: @duration', [
'@duration' => $this
->getSetting('duration'),
]);
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$element['value'] = $element + [
'#type' => 'duration',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : '',
'#description' => $element['#description'],
'#cardinality' => $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality(),
'#granularity' => $this
->getGranularity(),
];
return $element;
}
private function getGranularity() {
$granularity = $this
->getFieldSetting('granularity');
$time_elements = [
'year' => 'y',
'month' => 'm',
'day' => 'd',
'hour' => 'h',
'minute' => 'i',
'second' => 's',
];
$granularity_elements = [];
foreach ($time_elements as $key => $time_element) {
if ($granularity[$key]) {
$granularity_elements[] = $time_element;
}
}
return implode(':', $granularity_elements);
}
}