Last active
August 29, 2015 14:14
-
-
Save cmcintosh/e16835459a317b7791b7 to your computer and use it in GitHub Desktop.
src/Plugin/Field/FieldWidget/CommentTimerWidget.php
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\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', | |
'#prefix' => '<div class="container-inline">', | |
'#suffix' => '</div>', | |
'#collapsible' => TRUE, | |
'#collapsed' => FALSE, | |
'#tree' => TRUE, | |
'#title' => t('Comment timer'), | |
'#attached' => array( | |
'library' => array( | |
'comment_timer/comment_timer' | |
) | |
) | |
); | |
$element['seconds'] = array( | |
'#type' => 'textfield', | |
'#description' => t('Time to store (hh:mm:ss)'), | |
'#size' => '8', | |
'#default_value' => '', | |
); | |
$element['elapsed'] = array( | |
'#type' => 'textfield', | |
'#description' => t('Time spent (hh:mm:ss)'), | |
'#size' => '8', | |
'#default_value' => _comment_timer_seconds_to_hms($seconds), | |
// This is the ID the JS works on. | |
'#id' => 'edit-comment-timer-counter', | |
); | |
$element['status'] = array( | |
'#type' => 'select', | |
'#default_value' => $status, | |
'#options' => array( | |
1 => t('Include in node time'), | |
0 => t('Exclude from node time'), | |
), | |
); | |
$element['reset'] = array( | |
'#type' => 'button', | |
'#value' => t('Reset'), | |
// This is the ID the JS works on. | |
'#id' => 'edit-comment-timer-reset', | |
); | |
$element['pause'] = array( | |
'#type' => 'button', | |
'#value' => t('Pause'), | |
// This is the ID the JS works on. | |
'#id' => 'edit-comment-timer-pause', | |
); | |
// 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
[Tue Jan 27 06:37:13 2015] [error] [client 192.168.50.1] Uncaught PHP Exception Drupal\Core\Entity\EntityStorageException: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment_timer' in 'field list': INSERT INTO {comment_field_data} (cid, comment_type, langcode, pid, entity_id, subject, uid, name, mail, homepage, hostname, created, changed, status, thread, entity_type, field_name, comment_timer, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15, :db_insert_placeholder_16, :db_insert_placeholder_17, :db_insert_placeholder_18); Array\n(\n [:db_insert_placeholder_0] => 15\n [:db_insert_placeholder_1] => comment\n [:db_insert_placeholder_2] => en\n [:db_insert_placeholder_3] => 0\n [:db_insert_placeholder_4] => 1\n [:db_insert_placeholder_5] => 1\n [:db_insert_placeholder_6] => 1\n [:db_insert_placeholder_7] => cmcintosh\n [:db_insert_placeholder_8] => \n [:db_insert_placeholder_9] => \n [:db_insert_placeholder_10] => 192.168.50.1\n [:db_insert_placeholder_11] => 1422340632\n [:db_insert_placeholder_12] => 1422340632\n [:db_insert_placeholder_13] => 1\n [:db_insert_placeholder_14] => 0c/\n [:db_insert_placeholder_15] => node\n [:db_insert_placeholder_16] => comment\n [:db_insert_placeholder_17] => 1\n [:db_insert_placeholder_18] => 1\n)\n" at /var/www/article_work/sandbox/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 913, referer: http://sandbox.dev/comment/reply/node/1/comment