View source
<?php
namespace Drupal\radioactivity\Plugin\Field\FieldType;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class RadioactivityField extends FieldItemBase {
public static function defaultStorageSettings() {
return [
'profile' => 'decay',
'halflife' => 60 * 60 * 12,
'granularity' => 60 * 15,
'cutoff' => 1,
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['energy'] = DataDefinition::create('float')
->setLabel(new TranslatableMarkup('Energy level'));
$properties['timestamp'] = DataDefinition::create('integer')
->setLabel(new TranslatableMarkup('Energy timestamp'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'energy' => [
'description' => 'Energy level',
'type' => 'float',
'default' => 0,
],
'timestamp' => [
'description' => 'Timestamp of last emit',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
];
return $schema;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['energy'] = 1;
$values['timestamp'] = \Drupal::time()
->getRequestTime();
return $values;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$elements = [];
$elements['profile'] = [
'#type' => 'radios',
'#title' => $this
->t('Energy profile'),
'#default_value' => $this
->getSetting('profile'),
'#required' => TRUE,
'#options' => [
'count' => 'Count',
'linear' => 'Linear',
'decay' => 'Decay',
],
'#description' => $this
->t('Count: Energy increases by 1 with each view. Never decreases.<br/>
Linear: Energy increases by the emission amount. Decreases by 1 per second.<br/>
Decay: Energy increases by the emission amount. Decreases 50% per half-life time.'),
];
$elements['granularity'] = [
'#type' => 'number',
'#title' => $this
->t('Granularity'),
'#min' => 1,
'#default_value' => $this
->getSetting('granularity'),
'#description' => $this
->t('The time in seconds that the energy levels are kept before applying the decay.'),
'#states' => [
'visible' => [
'input[name="settings[profile]"]' => [
[
'value' => 'linear',
],
[
'value' => 'decay',
],
],
],
],
];
$elements['halflife'] = [
'#type' => 'number',
'#title' => $this
->t('Half-life time'),
'#min' => 1,
'#default_value' => $this
->getSetting('halflife'),
'#description' => $this
->t('The time in seconds in which the energy level halves.'),
'#states' => [
'visible' => [
'input[name="settings[profile]"]' => [
'value' => 'decay',
],
],
],
];
$elements['cutoff'] = [
'#type' => 'textfield',
'#title' => $this
->t('Cutoff'),
'#pattern' => '[0-9]+(\\.[0-9]+)?',
'#default_value' => $this
->getSetting('cutoff'),
'#description' => $this
->t('Energy levels under this value is set to zero. Example: 0.5, 2.'),
'#states' => [
'invisible' => [
'input[name="settings[profile]"]' => [
'value' => 'count',
],
],
],
];
return $elements;
}
public function preSave() {
parent::preSave();
if (empty($this->energy)) {
$this->energy = 0;
}
if ($this
->getEntity()
->isNew() || $this
->energyHasChanged() || $this
->getsPublished()) {
$this->timestamp = \Drupal::time()
->getRequestTime();
}
}
public function isEmpty() {
$value = $this
->get('energy')
->getValue();
return $value === NULL;
}
protected function energyHasChanged() {
$original = $this
->getEntity()->original;
if (empty($original)) {
return FALSE;
}
$fieldName = $this
->getFieldDefinition()
->getName();
return $original
->get($fieldName)->energy != $this->energy;
}
protected function getsPublished() {
$entity = $this
->getEntity();
if (!$entity instanceof EntityPublishedInterface) {
return FALSE;
}
if (!$entity
->isPublished()) {
return FALSE;
}
$original = $this
->getEntity()->original;
if (empty($original)) {
return FALSE;
}
return !$original
->isPublished();
}
}