RadioactivityEnergy.php in Radioactivity 4.0.x
File
src/Plugin/Field/FieldWidget/RadioactivityEnergy.php
View source
<?php
namespace Drupal\radioactivity\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class RadioactivityEnergy extends WidgetBase {
public static function defaultSettings() {
return [
'energy' => 0.0,
'timestamp' => \Drupal::time()
->getRequestTime(),
] + parent::defaultSettings();
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = [
'#type' => 'textfield',
'#pattern' => '[0-9]+(\\.[0-9]+)?',
'#default_value' => isset($items[$delta]->energy) ? $items[$delta]->energy : 0,
] + $element;
if (!isset($form['advanced'])) {
return [
'energy' => $element,
];
}
return [
'#type' => 'details',
'#group' => 'advanced',
'#title' => $element['#title'],
'#required' => TRUE,
'#weight' => '40',
'energy' => $element,
];
}
}