You are here

ComputedIntegerItem.php in Computed Field 8.2

Same filename and directory in other branches
  1. 3.x src/Plugin/Field/FieldType/ComputedIntegerItem.php

File

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

namespace Drupal\computed_field\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Plugin implementation of the 'computed_integer' field type.
 *
 * @FieldType(
 *   id = "computed_integer",
 *   label = @Translation("Computed (integer)"),
 *   description = @Translation("This field defines an integer field whose value is computed by PHP-Code"),
 *   category = @Translation("Computed"),
 *   default_widget = "computed_number_widget",
 *   default_formatter = "computed_integer"
 * )
 */
class ComputedIntegerItem extends ComputedFieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultStorageSettings() {
    return [
      'unsigned' => FALSE,
      // Valid size property values include: 'tiny', 'small', 'medium', 'normal'
      // and 'big'.
      'size' => 'normal',
    ] + parent::defaultStorageSettings();
  }

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

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

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $settings = $field_definition
      ->getSettings();
    $schema = [
      'columns' => [
        'value' => [
          'type' => 'int',
          'size' => $settings['size'],
        ],
      ],
    ];
    return $schema;
  }

  /**
   * {@inheritdoc}
   *
   * @todo Add useful code.
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $values['value'] = rand(PHP_INT_MIN, PHP_INT_MAX);
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::fieldSettingsForm($form, $form_state);
    $element['code']['#title'] = $this
      ->t('Code (PHP) to compute the <em>integer</em> value');
    $element['code']['#description'] .= '<p>' . t('The value will be rounded to an integer value.') . '</p>';
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    return [];
  }

  /**
   * {@inheritdoc}
   *
   * @return int
   *   The integer number.
   */
  public function executeCode() {
    return (int) parent::executeCode();
  }

}

Classes

Namesort descending Description
ComputedIntegerItem Plugin implementation of the 'computed_integer' field type.