You are here

RadioactivityField.php in Radioactivity 8.2

File

src/Plugin/Field/FieldType/RadioactivityField.php
View source
<?php

/**
 * @file
 * Contains \Drupal\radioactivity\Plugin\Field\FieldType\RadioactivityField.
 */
namespace Drupal\radioactivity\Plugin\Field\FieldType;

use Drupal\Component\Utility\Random;
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;

/**
 * Plugin implementation of the 'radioactivity' field type.
 *
 * @FieldType(
 *   id = "radioactivity",
 *   label = @Translation("Radioactivity"),
 *   description = @Translation("Radioactivity field"),
 *   default_widget = "radioactivity_field_widget",
 *   default_formatter = "radioactivity_field_widget"
 * )
 */
class RadioactivityField extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultStorageSettings() {
    return array(
      'profile' => 0,
      'halflife' => 60 * 60 * 12,
      'granularity' => 60 * 15,
    ) + parent::defaultStorageSettings();
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {

    // Prevent early t() calls by using the TranslatableMarkup.
    $properties['energy'] = DataDefinition::create('float')
      ->setLabel(new TranslatableMarkup('Energy level'))
      ->setRequired(TRUE);
    $properties['timestamp'] = DataDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Energy timestamp'));
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = array(
      'columns' => array(
        'energy' => array(
          'description' => 'Energy level',
          'type' => 'float',
          'default' => 0,
        ),
        'timestamp' => array(
          'description' => 'Timestamp of last emit',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
    );
    return $schema;
  }

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    if ($max_length = $this
      ->getSetting('max_length')) {
      $constraint_manager = \Drupal::typedDataManager()
        ->getValidationConstraintManager();

      /*$constraints[] = $constraint_manager->create('ComplexData', array(
          'value' => array(
            'Length' => array(
              'max' => $max_length,
              'maxMessage' => t('%name: may not be longer than @max characters.', array(
                '%name' => $this->getFieldDefinition()->getLabel(),
                '@max' => $max_length
              )),
            ),
          ),
        ));*/
    }
    return $constraints;
  }

  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $values['energy'] = 1;
    $values['timestamp'] = time() / 60 / 60 * 60 * 60;
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $elements = [];
    $elements['profile'] = array(
      '#type' => 'select',
      '#title' => t('Profile'),
      '#default_value' => $this
        ->getSetting('profile'),
      '#required' => TRUE,
      '#options' => array(
        0 => 'Count',
        1 => 'Linear',
        2 => 'Decay',
      ),
      '#description' => t('Select the profile to use'),
    );
    $elements['granularity'] = array(
      '#type' => 'textfield',
      '#title' => t('Granularity in seconds'),
      '#pattern' => '[0-9]*',
      '#default_value' => $this
        ->getSetting('granularity'),
      '#description' => t('Granularity defines how long the energy levels are kept before applying any decay.'),
    );
    $elements['halflife'] = array(
      '#type' => 'textfield',
      '#title' => t('Half-life'),
      '#pattern' => '[0-9]*',
      '#default_value' => $this
        ->getSetting('halflife'),
      '#description' => t('Half-life determines the amount of time in which the energy level halves.'),
    );
    return $elements;
  }
  public function preSave() {
    parent::preSave();
    if (!$this->energy) {
      $this->energy = 0;
    }
    $this->timestamp = time();
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this
      ->get('energy')
      ->getValue();
    return $value === NULL;
  }

}

Classes

Namesort descending Description
RadioactivityField Plugin implementation of the 'radioactivity' field type.