class SmartdateFieldBuilder in Smart Date 3.3.x
Same name and namespace in other branches
- 3.4.x src/Plugin/diff/Field/SmartdateFieldBuilder.php \Drupal\smart_date\Plugin\diff\Field\SmartdateFieldBuilder
Plugin to diff text fields.
Plugin annotation
@FieldDiffBuilder(
id = "smartdate_field_diff_builder",
label = @Translation("Smart Date Field Diff"),
field_types = {
"smartdate"
},
)
Hierarchy
- class \Drupal\smart_date\Plugin\diff\Field\SmartdateFieldBuilder extends \Drupal\diff\FieldDiffBuilderBase uses SmartDateTrait
Expanded class hierarchy of SmartdateFieldBuilder
File
- src/
Plugin/ diff/ Field/ SmartdateFieldBuilder.php, line 22
Namespace
Drupal\smart_date\Plugin\diff\FieldView source
class SmartdateFieldBuilder extends FieldDiffBuilderBase {
use SmartDateTrait;
/**
* {@inheritdoc}
*/
public function build(FieldItemListInterface $field_items) {
$result = [];
$format = \Drupal::entityTypeManager()
->getStorage('smart_date_format')
->load($this->configuration['format']);
// Every item from $field_items is of type FieldItemInterface.
foreach ($field_items as $field_key => $field_item) {
$values = $field_item
->getValue();
if (!isset($values['value'])) {
continue;
}
if (!isset($values['end_value'])) {
if (isset($values['duration']) && $values['duration'] > 0) {
$values['end_value'] = $values['value'] + $values['duration'] * 60;
}
else {
$values['end_value'] = $values['value'];
}
}
$date_value = static::formatSmartDate($values['value'], $values['end_value'], $format
->getOptions(), NULL, 'string');
$result[$field_key][] = $this
->t('Date: @date_value', [
'@date_value' => $date_value,
]);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
unset($form['format_type']);
// Change the description of the timezone_override element.
if (isset($form['timezone_override'])) {
$form['timezone_override']['#description'] = $this
->t('The time zone selected here will be used unless overridden on an individual date.');
}
// Ask the user to choose a Smart Date Format.
$formatOptions = [];
$smartDateFormats = \Drupal::entityTypeManager()
->getStorage('smart_date_format')
->loadMultiple();
foreach ($smartDateFormats as $type => $format) {
if ($format instanceof SmartDateFormat) {
$formatted = static::formatSmartDate(time(), time() + 3600, $format
->getOptions(), NULL, 'string');
$formatOptions[$type] = $format
->label() . ' (' . $formatted . ')';
}
}
$form['format'] = [
'#type' => 'select',
'#title' => $this
->t('Smart Date Format'),
'#description' => $this
->t('Choose which display configuration to use.'),
'#default_value' => 'default',
'#options' => $formatOptions,
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['timezone_override'] = $form_state
->getValue('timezone_override');
$this->configuration['format'] = $form_state
->getValue('format');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$default_configuration = array(
'timezone_override' => 0,
'format' => 'default',
);
$default_configuration += parent::defaultConfiguration();
return $default_configuration;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SmartdateFieldBuilder:: |
public | function | ||
SmartdateFieldBuilder:: |
public | function | ||
SmartdateFieldBuilder:: |
public | function | ||
SmartdateFieldBuilder:: |
public | function | ||
SmartDateTrait:: |
private | function | Add spans provides classes to allow the dates and times to be styled. | |
SmartDateTrait:: |
private | function | Add spans provides classes to allow the dates and times to be styled. | |
SmartDateTrait:: |
private static | function | Helper function to turn a simple, nested array into a render array. | |
SmartDateTrait:: |
public static | function | Creates a formatted date value as a string. | |
SmartDateTrait:: |
public static | function | Evaluates whether or not a provided range is "all day". | |
SmartDateTrait:: |
public static | function | Load a Smart Date Format from a format name. | |
SmartDateTrait:: |
private static | function | Reduce duplication in a provided date range. | |
SmartDateTrait:: |
private static | function | Format a provided range, using provided settings. | |
SmartDateTrait:: |
public static | function | Removes date tokens from format settings. | |
SmartDateTrait:: |
public static | function | Removes time tokens from format settings. | |
SmartDateTrait:: |
public static | function | Removes timezone tokens from time settings. | |
SmartDateTrait:: |
private static | function | Helper function to apply time formats. | |
SmartDateTrait:: |
public | function | 4 |