DimensionsItem.php 3.78 KB
Newer Older
bojanz's avatar
bojanz committed
1 2 3 4 5 6 7 8 9 10
<?php

namespace Drupal\physical\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\physical\Length;

/**
11
 * Plugin implementation of the 'physical_dimensions' field type.
bojanz's avatar
bojanz committed
12 13
 *
 * @FieldType(
14 15
 *   id = "physical_dimensions",
 *   label = @Translation("Dimensions"),
bojanz's avatar
bojanz committed
16 17
 *   description = @Translation("This field stores the length, width, height numbers and a unit of measure."),
 *   category = @Translation("Physical"),
18 19
 *   default_widget = "physical_dimensions_default",
 *   default_formatter = "physical_dimensions_default"
bojanz's avatar
bojanz committed
20 21
 * )
 */
22
class DimensionsItem extends FieldItemBase {
bojanz's avatar
bojanz committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'length' => [
          'description' => 'The length.',
          'type' => 'numeric',
          'default' => 0,
          'precision' => 19,
          'scale' => 6,
        ],
        'width' => [
          'description' => 'The width.',
          'type' => 'numeric',
          'size' => 'normal',
          'precision' => 19,
          'scale' => 6,
        ],
        'height' => [
          'description' => 'The height.',
          'type' => 'numeric',
          'size' => 'normal',
          'precision' => 19,
          'scale' => 6,
        ],
        'unit' => [
          'description' => 'The unit.',
          'type' => 'varchar',
          'length' => '255',
          'default' => '',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['length'] = DataDefinition::create('string')
      ->setLabel(t('Length'));
    $properties['width'] = DataDefinition::create('string')
      ->setLabel(t('Width'));
    $properties['height'] = DataDefinition::create('string')
      ->setLabel(t('Height'));
    $properties['unit'] = DataDefinition::create('string')
      ->setLabel(t('Unit'));

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
88
    $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
bojanz's avatar
bojanz committed
89
    $constraints = parent::getConstraints();
90
    $constraints[] = $constraint_manager->create('Dimensions', []);
bojanz's avatar
bojanz committed
91 92 93 94 95 96 97 98

    return $constraints;
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
99 100 101
    // The field is empty if the unit is empty or all numbers are empty.
    if (empty($this->unit)) {
      return TRUE;
bojanz's avatar
bojanz committed
102
    }
103
    if ($this->checkEmpty($this->length) && $this->checkEmpty($this->width) && $this->checkEmpty($this->height)) {
bojanz's avatar
bojanz committed
104 105 106 107 108 109
      return TRUE;
    }

    return FALSE;
  }

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  /**
   * Checks whether the given value is NULL or an empty string.
   *
   * Excludes zeroes from the check because they are considered valid numbers
   * in the context of this field.
   *
   * @param string $value
   *   The value.
   *
   * @return bool
   *   TRUE if the value is empty, FALSE otherwise.
   */
  protected function checkEmpty($value) {
    return is_null($value) || $value === '';
  }

bojanz's avatar
bojanz committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * Gets the length as a value object.
   *
   * @return \Drupal\physical\Length
   *   The length.
   */
  public function getLength() {
    return new Length($this->length, $this->unit);
  }

  /**
   * Gets the width as a value object.
   *
   * @return \Drupal\physical\Length
   *   The width.
   */
  public function getWidth() {
    return new Length($this->width, $this->unit);
  }

  /**
   * Gets the height as a value object.
   *
   * @return \Drupal\physical\Length
   *   The height.
   */
  public function getHeight() {
    return new Length($this->height, $this->unit);
  }

}