You are here

TextItemBase.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php

File

core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php
View source
<?php

/**
 * @file
 * Contains \Drupal\text\Plugin\Field\FieldType\TextItemBase.
 */
namespace Drupal\text\Plugin\Field\FieldType;

use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Base class for 'text' configurable field types.
 */
abstract class TextItemBase extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(t('Text'))
      ->setRequired(TRUE);
    $properties['format'] = DataDefinition::create('filter_format')
      ->setLabel(t('Text format'));
    $properties['processed'] = DataDefinition::create('string')
      ->setLabel(t('Processed text'))
      ->setDescription(t('The text with the text format applied.'))
      ->setComputed(TRUE)
      ->setClass('\\Drupal\\text\\TextProcessed')
      ->setSetting('text source', 'value');
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function applyDefaultValue($notify = TRUE) {

    // @todo: Add in the filter default format here.
    $this
      ->setValue(array(
      'format' => NULL,
    ), $notify);
    return $this;
  }

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

  /**
   * {@inheritdoc}
   */
  public function onChange($property_name, $notify = TRUE) {

    // Unset processed properties that are affected by the change.
    foreach ($this->definition
      ->getPropertyDefinitions() as $property => $definition) {
      if ($definition
        ->getClass() == '\\Drupal\\text\\TextProcessed') {
        if ($property_name == 'format' || $definition
          ->getSetting('text source') == $property_name) {
          $this
            ->writePropertyValue($property, NULL);
        }
      }
    }
    parent::onChange($property_name, $notify);
  }

  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
    $settings = $field_definition
      ->getSettings();
    if (empty($settings['max_length'])) {

      // Textarea handling
      $value = $random
        ->paragraphs();
    }
    else {

      // Textfield handling.
      $value = substr($random
        ->sentences(mt_rand(1, $settings['max_length'] / 3), FALSE), 0, $settings['max_length']);
    }
    $values = array(
      'value' => $value,
      'summary' => $value,
      'format' => filter_fallback_format(),
    );
    return $values;
  }

}

Classes

Namesort descending Description
TextItemBase Base class for 'text' configurable field types.