DateTargetBase.php in Feeds 8.3
File
src/Feeds/Target/DateTargetBase.php
View source
<?php
namespace Drupal\feeds\Feeds\Target;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;
abstract class DateTargetBase extends FieldTargetBase implements ConfigurableTargetInterface {
protected function convertToDate($value) {
$value = trim($value);
if (ctype_digit($value) && strlen($value) === 4) {
$value = 'January ' . $value;
}
if (is_numeric($value)) {
$date = DrupalDateTime::createFromTimestamp($value, $this
->getTimezoneConfiguration());
}
elseif (strtotime($value)) {
$date = new DrupalDateTime($value, $this
->getTimezoneConfiguration());
}
if (isset($date) && !$date
->hasErrors()) {
return $date;
}
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'timezone' => 'UTC',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['timezone'] = [
'#type' => 'select',
'#title' => $this
->t('Timezone handling'),
'#options' => $this
->getTimezoneOptions(),
'#default_value' => $this->configuration['timezone'],
'#description' => $this
->t('This value will only be used if the timezone is missing.'),
];
return $form;
}
public function getTimezoneOptions() {
return [
'__SITE__' => $this
->t('Site default'),
] + system_time_zones();
}
public function getSummary() {
$summary = parent::getSummary();
$options = $this
->getTimezoneOptions();
$summary[] = $this
->t('Default timezone: %zone', [
'%zone' => $options[$this->configuration['timezone']],
]);
return $summary;
}
public function getTimezoneConfiguration() {
return $this->configuration['timezone'] == '__SITE__' ? \Drupal::config('system.date')
->get('timezone.default') : $this->configuration['timezone'];
}
}