-
-
Save larowlan/b4f9955e154b87745bd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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