Issue #2383277 by MrHaroldA: StringLongItem should not extend StringItem
parent
2f35d45dbb
commit
1990524667
|
@ -10,10 +10,7 @@ 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\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 @@ use Drupal\Core\TypedData\DataDefinition;
|
|||
* default_formatter = "string"
|
||||
* )
|
||||
*/
|
||||
class StringItem extends FieldItemBase {
|
||||
class StringItem extends StringItemBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -37,18 +34,6 @@ class StringItem extends FieldItemBase {
|
|||
) + 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 @@ class StringItem extends FieldItemBase {
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 @@ use Drupal\Core\Form\FormStateInterface;
|
|||
* 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 @@ class StringLongItem extends StringItem {
|
|||
/**
|
||||
* {@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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue