LinkItem.php 4.47 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\link\Plugin\Field\FieldType\LinkItem.
6 7
 */

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

10 11
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
12
use Drupal\Core\Field\FieldItemBase;
13
use Drupal\Core\Field\FieldStorageDefinitionInterface;
14
use Drupal\Core\Form\FormStateInterface;
15 16
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
17
use Drupal\link\LinkItemInterface;
18 19 20 21 22 23 24 25 26

/**
 * Plugin implementation of the 'link' field type.
 *
 * @FieldType(
 *   id = "link",
 *   label = @Translation("Link"),
 *   description = @Translation("Stores a URL string, optional varchar link text, and optional blob of attributes to assemble a link."),
 *   default_widget = "link_default",
27 28
 *   default_formatter = "link",
 *   constraints = {"LinkType" = {}}
29 30
 * )
 */
31
class LinkItem extends FieldItemBase implements LinkItemInterface {
32

33 34 35
  /**
   * {@inheritdoc}
   */
36
  public static function defaultFieldSettings() {
37
    return array(
38 39
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC
40
    ) + parent::defaultFieldSettings();
41 42
  }

43 44 45
  /**
   * {@inheritdoc}
   */
46
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
47 48 49 50
    // @todo Change the type from 'string' to 'uri':
    //   https://www.drupal.org/node/2412509.
    $properties['uri'] = DataDefinition::create('string')
      ->setLabel(t('URI'));
51 52 53

    $properties['title'] = DataDefinition::create('string')
      ->setLabel(t('Link text'));
54

55 56
    $properties['options'] = MapDataDefinition::create()
      ->setLabel(t('Options'));
57

58
    return $properties;
59 60 61 62 63
  }

  /**
   * {@inheritdoc}
   */
64
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
65 66
    return array(
      'columns' => array(
67 68
        'uri' => array(
          'description' => 'The URI of the link.',
69 70 71 72 73 74 75 76
          'type' => 'varchar',
          'length' => 2048,
        ),
        'title' => array(
          'description' => 'The link text.',
          'type' => 'varchar',
          'length' => 255,
        ),
77 78
        'options' => array(
          'description' => 'Serialized array of options for the link.',
79 80 81 82 83 84 85 86 87 88 89
          'type' => 'blob',
          'size' => 'big',
          'serialize' => TRUE,
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
90
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
91 92
    $element = array();

93 94 95 96 97 98 99 100 101 102 103
    $element['link_type'] = array(
      '#type' => 'radios',
      '#title' => t('Allowed link type'),
      '#default_value' => $this->getSetting('link_type'),
      '#options' => array(
        static::LINK_INTERNAL => t('Internal links only'),
        static::LINK_EXTERNAL => t('External links only'),
        static::LINK_GENERIC => t('Both internal and external links'),
      ),
    );

104 105 106
    $element['title'] = array(
      '#type' => 'radios',
      '#title' => t('Allow link text'),
107
      '#default_value' => $this->getSetting('title'),
108 109 110 111 112 113 114 115 116 117
      '#options' => array(
        DRUPAL_DISABLED => t('Disabled'),
        DRUPAL_OPTIONAL => t('Optional'),
        DRUPAL_REQUIRED => t('Required'),
      ),
    );

    return $element;
  }

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    // Set of possible top-level domains.
    $tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
    // Set random length for the domain name.
    $domain_length = mt_rand(7, 15);
    $random = new Random();

    switch ($field_definition->getSetting('title')) {
      case DRUPAL_DISABLED:
        $values['title'] = '';
        break;
      case DRUPAL_REQUIRED:
        $values['title'] = $random->sentences(4);
        break;
      case DRUPAL_OPTIONAL:
        // In case of optional title, randomize its generation.
        $values['title'] = mt_rand(0,1) ? $random->sentences(4) : '';
        break;
    }
140
    $values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))];
141 142 143
    return $values;
  }

144 145 146
  /**
   * {@inheritdoc}
   */
147
  public function isEmpty() {
148
    $value = $this->get('uri')->getValue();
149
    return $value === NULL || $value === '';
150 151 152 153 154
  }

  /**
   * {@inheritdoc}
   */
155
  public function isExternal() {
156 157 158
    // External links don't resolve to a route.
    $url = \Drupal::pathValidator()->getUrlIfValid($this->uri);
    return $url->isExternal();
159
  }
160 161 162 163 164

  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
165
    return 'uri';
166
  }
167
}