Issue #2383277 by MrHaroldA: StringLongItem should not extend StringItem

8.0.x
Alex Pott 2014-11-29 08:38:45 +00:00
parent 2f35d45dbb
commit 1990524667
3 changed files with 41 additions and 40 deletions

View File

@ -10,10 +10,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random; use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/** /**
* Defines the 'string' entity field type. * Defines the 'string' entity field type.
@ -26,7 +23,7 @@ use Drupal\Core\TypedData\DataDefinition;
* default_formatter = "string" * default_formatter = "string"
* ) * )
*/ */
class StringItem extends FieldItemBase { class StringItem extends StringItemBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -37,18 +34,6 @@ class StringItem extends FieldItemBase {
) + parent::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} * {@inheritdoc}
*/ */
@ -90,15 +75,7 @@ class StringItem extends FieldItemBase {
*/ */
public static function generateSampleValue(FieldDefinitionInterface $field_definition) { public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random(); $random = new Random();
$max = $field_definition->getSetting('max_length'); $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
if ($max) {
$values['value'] = $random->word(mt_rand(1, $max));
}
else {
$values['value'] = $random->paragraphs();
}
return $values; return $values;
} }

View File

@ -0,0 +1,32 @@
<?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;
}
}

View File

@ -7,8 +7,9 @@
namespace Drupal\Core\Field\Plugin\Field\FieldType; 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\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/** /**
* Defines the 'string_long' field type. * Defines the 'string_long' field type.
@ -21,16 +22,7 @@ use Drupal\Core\Form\FormStateInterface;
* default_formatter = "string", * default_formatter = "string",
* ) * )
*/ */
class StringLongItem extends StringItem { class StringLongItem extends StringItemBase {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
unset($settings['max_length']);
return $settings;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -49,10 +41,10 @@ class StringLongItem extends StringItem {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$element = parent::storageSettingsForm($form, $form_state, $has_data); $random = new Random();
unset($element['max_length']); $values['value'] = $random->paragraphs();
return $element; return $values;
} }
} }