Commit 19905246 authored by alexpott's avatar alexpott

Issue #2383277 by MrHaroldA: StringLongItem should not extend StringItem

parent 2f35d45d
......@@ -10,10 +10,7 @@
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Defines the 'string' entity field type.
......@@ -26,7 +23,7 @@
* default_formatter = "string"
* )
*/
class StringItem extends FieldItemBase {
class StringItem extends StringItemBase {
/**
* {@inheritdoc}
......@@ -37,18 +34,6 @@ public static function defaultStorageSettings() {
) + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
// This is called very early by the user entity roles field. Prevent
// early t() calls by using the TranslationWrapper.
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslationWrapper('Text value'));
return $properties;
}
/**
* {@inheritdoc}
*/
......@@ -90,15 +75,7 @@ public function getConstraints() {
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$max = $field_definition->getSetting('max_length');
if ($max) {
$values['value'] = $random->word(mt_rand(1, $max));
}
else {
$values['value'] = $random->paragraphs();
}
$values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
return $values;
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\TypedData\DataDefinition;
/**
* Base class for string field types.
*/
abstract class StringItemBase extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
// This is called very early by the user entity roles field. Prevent
// early t() calls by using the TranslationWrapper.
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslationWrapper('Text value'));
return $properties;
}
}
......@@ -7,8 +7,9 @@
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines the 'string_long' field type.
......@@ -21,16 +22,7 @@
* default_formatter = "string",
* )
*/
class StringLongItem extends StringItem {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
unset($settings['max_length']);
return $settings;
}
class StringLongItem extends StringItemBase {
/**
* {@inheritdoc}
......@@ -49,10 +41,10 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = parent::storageSettingsForm($form, $form_state, $has_data);
unset($element['max_length']);
return $element;
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$values['value'] = $random->paragraphs();
return $values;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment