View source
<?php
namespace Drupal\radioactivity\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class RadioactivityReferenceItem extends EntityReferenceItem {
public static function defaultStorageSettings() {
return [
'target_type' => 'radioactivity',
'profile' => 'decay',
'halflife' => 60 * 60 * 12,
'granularity' => 60 * 15,
'cutoff' => 1,
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
return [
'default_energy' => 0,
] + parent::defaultFieldSettings();
}
public static function getPreconfiguredOptions() {
return [];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
$properties['energy'] = DataDefinition::create('float')
->setLabel(new TranslatableMarkup('Energy level'))
->setDescription(new TranslatableMarkup('The radioactivity energy level'))
->setComputed(TRUE)
->setReadOnly(FALSE);
return $properties;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$elements = parent::storageSettingsForm($form, $form_state, $has_data);
$elements['target_type']['#access'] = FALSE;
$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]+)?',
'#size' => 20,
'#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 fieldSettingsForm(array $form, FormStateInterface $form_state) {
$field = $form_state
->getFormObject()
->getEntity();
$form = parent::fieldSettingsForm($form, $form_state);
$form['handler']['#access'] = FALSE;
$form['default_energy'] = [
'#type' => 'textfield',
'#title' => $this
->t('Default energy'),
'#description' => $this
->t('The default energy value for this field, used when creating new content.'),
'#required' => TRUE,
'#default_value' => $field
->getSetting('default_energy'),
'#pattern' => '[0-9]+(\\.[0-9]+)?',
'#size' => 20,
];
return $form;
}
public function preSave() {
$needsSave = FALSE;
$requestTime = \Drupal::time()
->getRequestTime();
if ($this
->hasNewEntity()) {
$this->entity
->setEnergy($this->energy);
$this->entity
->setTimestamp($requestTime);
}
elseif ($this->energy != $this->initial_energy) {
$this->entity
->setEnergy($this->energy);
$this->entity
->setTimestamp($requestTime);
$needsSave = TRUE;
}
if ($this
->getLangcode() !== $this->entity->getLangcode) {
$this->entity
->setLangcode($this
->getLangcode());
if (!$this
->hasNewEntity()) {
$needsSave = TRUE;
}
}
if ($needsSave) {
$this->entity
->save();
}
parent::preSave();
}
public function delete() {
if ($this->entity) {
$this->entity
->delete();
}
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$entity_storage = \Drupal::entityTypeManager()
->getStorage('radioactivity');
$entity = $entity_storage
->createWithSampleValues('radioactivity');
return [
'entity' => $entity,
'energy' => $entity
->getEnergy(),
];
}
}