You are here

OpignoTrueFalseFieldType.php in Opigno module 3.x

Same filename and directory in other branches
  1. 8 src/Plugin/Field/FieldType/OpignoTrueFalseFieldType.php

File

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

namespace Drupal\opigno_module\Plugin\Field\FieldType;

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

/**
 * Plugin implementation of the 'opigno_true_false_field' field type.
 *
 * @FieldType(
 *   id = "opigno_true_false_field",
 *   label = @Translation("True or False"),
 *   description = @Translation("A field that prompts a choice."),
 *   default_widget = "opigno_true_false_widget_type",
 *   default_formatter = "opigno_true_false_formatter_type"
 * )
 */
class OpignoTrueFalseFieldType extends FieldItemBase {

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

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

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = [
      'columns' => [
        'value' => [
          'type' => 'int',
          'description' => 'The value of the answer.',
        ],
      ],
    ];
    return $schema;
  }

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();
    return $constraints;
  }

  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $values['value'] = 0;
    return $values;
  }

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

}

Classes

Namesort descending Description
OpignoTrueFalseFieldType Plugin implementation of the 'opigno_true_false_field' field type.