UriItem.php 1.82 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\Plugin\Field\FieldType\UriItem.
6 7
 */

8
namespace Drupal\Core\Field\Plugin\Field\FieldType;
9

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\TypedData\DataDefinition;
12 13

/**
14
 * Defines the 'uri' entity field type.
15
 *
16 17 18 19
 * URIs are not length limited by RFC 2616, but we need to provide a sensible
 * default. There is a de-facto limit of 2000 characters in browsers and other
 * implementors, so we go with 2048.
 *
20 21 22
 * @FieldType(
 *   id = "uri",
 *   label = @Translation("URI"),
23
 *   description = @Translation("An entity field containing a URI."),
24 25
 *   no_ui = TRUE,
 *   default_formatter = "uri_link",
26
 * )
27
 */
28
class UriItem extends StringItem {
29

30 31 32
  /**
   * {@inheritdoc}
   */
33
  public static function defaultStorageSettings() {
34 35
    return array(
      'max_length' => 2048,
36
    ) + parent::defaultStorageSettings();
37 38
  }

39
  /**
40
   * {@inheritdoc}
41
   */
42
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
43
    $properties['value'] = DataDefinition::create('uri')
44
      ->setLabel(t('URI value'))
45 46
      ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
      ->setRequired(TRUE);
47

48
    return $properties;
49
  }
50

51 52 53
  /**
   * {@inheritdoc}
   */
54
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
55 56 57
    return array(
      'columns' => array(
        'value' => array(
58 59
          'type' => 'varchar',
          'length' => (int) $field_definition->getSetting('max_length'),
60
          'binary' => $field_definition->getSetting('case_sensitive'),
61 62 63 64 65
        ),
      ),
    );
  }

66 67 68 69 70 71 72 73 74 75 76
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->getValue();
    if (!isset($value['value']) || $value['value'] === '') {
      return TRUE;
    }
    return parent::isEmpty();
  }

77
}