TimeToDate.php in Tamper 8
File
src/Plugin/Tamper/TimeToDate.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class TimeToDate extends TamperBase {
const SETTING_DATE_FORMAT = 'date_format';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_DATE_FORMAT] = "\\I\\t'\\s g \\o'\\c\\l\\o\\c\\k \\J\\i\\m\\.";
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_DATE_FORMAT] = [
'#type' => 'textfield',
'#title' => $this
->t('Date format'),
'#default_value' => $this
->getSetting(self::SETTING_DATE_FORMAT),
'#description' => $this
->t('A user-defined php date format string like "m-d-Y H:i". See the <a href="@link">PHP manual</a> for available options.', [
'@link' => 'http://www.php.net/manual/function.date.php',
]),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_DATE_FORMAT => $form_state
->getValue(self::SETTING_DATE_FORMAT),
]);
}
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_numeric($data)) {
throw new TamperException('Input should be numeric.');
}
return date($this
->getSetting(self::SETTING_DATE_FORMAT), $data);
}
}
Classes
Name |
Description |
TimeToDate |
Plugin implementation of the TimetoDate plugin. |