StringItem.php 2.63 KB
Newer Older
1 2 3 4
<?php

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

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

10 11
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
12
use Drupal\Core\Field\FieldStorageDefinitionInterface;
13
use Drupal\Core\Form\FormStateInterface;
14 15

/**
16
 * Defines the 'string' entity field type.
17
 *
18 19
 * @FieldType(
 *   id = "string",
20 21
 *   label = @Translation("Text (plain)"),
 *   description = @Translation("A field containing a plain string value."),
22
 *   category = @Translation("Text"),
23
 *   default_widget = "string_textfield",
24
 *   default_formatter = "string"
25
 * )
26
 */
27
class StringItem extends StringItemBase {
28

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

38 39 40
  /**
   * {@inheritdoc}
   */
41
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
42 43 44
    return array(
      'columns' => array(
        'value' => array(
45
          'type' => 'varchar',
46
          'length' => (int) $field_definition->getSetting('max_length'),
47
          'binary' => $field_definition->getSetting('case_sensitive'),
48 49 50 51 52
        ),
      ),
    );
  }

53 54 55 56 57 58
  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();

59
    if ($max_length = $this->getSetting('max_length')) {
60 61
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
      $constraints[] = $constraint_manager->create('ComplexData', array(
62 63 64 65 66 67
        '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)),
          ),
        ),
68 69 70 71 72 73
      ));
    }

    return $constraints;
  }

74 75 76 77 78
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
79
    $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
80 81 82
    return $values;
  }

83 84 85
  /**
   * {@inheritdoc}
   */
86
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    $element = array();

    $element['max_length'] = array(
      '#type' => 'number',
      '#title' => t('Maximum length'),
      '#default_value' => $this->getSetting('max_length'),
      '#required' => TRUE,
      '#description' => t('The maximum length of the field in characters.'),
      '#min' => 1,
      '#disabled' => $has_data,
    );

    return $element;
  }

102
}