Skip to content

Instantly share code, notes, and snippets.

@larowlan
Forked from cmcintosh/gist:e16835459a317b7791b7
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larowlan/b4f9955e154b87745bd1 to your computer and use it in GitHub Desktop.
Save larowlan/b4f9955e154b87745bd1 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\comment_timer\Plugin\Field\FieldWidget\NumberWidget.
*/
namespace Drupal\comment_timer\Field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'comment_timer' widget.
*
* @FieldWidget(
* id = "comment_timer",
* label = @Translation("Comment Timer"),
* field_types = {
* "integer"
* }
* )
*/
class CommentTimerWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return ($field_definition->getTargetEntityTypeID() === 'comment');
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// $element += array(
// '#type' => 'number',
// '#default_value' => $value,
// '#placeholder' => $this->getSetting('placeholder'),
// '#attached' => array(
// 'library' => array(
// 'comment_timer/comment_timer'
// )
// )
// );
$element += array(
'#type' => 'fieldset',
'#tree' => TRUE
);
// Set the step for floating point and decimal numbers.
switch ($this->fieldDefinition->getType()) {
case 'decimal':
$element['#step'] = pow(0.1, $field_settings['scale']);
break;
case 'float':
$element['#step'] = 'any';
break;
}
// Set minimum and maximum.
if (is_numeric($field_settings['min'])) {
$element['#min'] = $field_settings['min'];
}
if (is_numeric($field_settings['max'])) {
$element['#max'] = $field_settings['max'];
}
// Add prefix and suffix.
if ($field_settings['prefix']) {
$prefixes = explode('|', $field_settings['prefix']);
$element['#field_prefix'] = $this->fieldFilterXss(array_pop($prefixes));
}
if ($field_settings['suffix']) {
$suffixes = explode('|', $field_settings['suffix']);
$element['#field_suffix'] = $this->fieldFilterXss(array_pop($suffixes));
}
return array('value' => $element);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment