diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php index ce78a40..b8df4b2 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php @@ -62,4 +62,25 @@ public static function schema(FieldDefinitionInterface $field_definition) { ); } + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); + $constraints = parent::getConstraints(); + + if ($max_length = $this->getFieldSetting('max_length')) { + $constraints[] = $constraint_manager->create('ComplexData', array( + 'value' => array( + 'Length' => array( + 'max' => $max_length, + 'maxMessage' => t('%name: the text may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)), + ) + ), + )); + } + + return $constraints; + } + } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 0aaffb5..e9c31e3 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -702,3 +702,15 @@ function field_hook_info() { return $hooks; } + +/** + * Implements hook_field_info_alter(). + */ +function field_field_info_alter(&$info) { + // Let core's field types to have formatters and widgets. + $info['string']['default_formatter'] = 'text_plain'; + $info['integer']['default_formatter'] = 'text_plain'; + + $info['string']['default_widget'] = 'field_text'; + $info['integer']['default_widget'] = 'field_text'; +} diff --git a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldFormatter/TextPlainFormatter.php b/core/modules/field/lib/Drupal/field/Plugin/Field/FieldFormatter/FieldTextFormatter.php similarity index 67% rename from core/modules/text/lib/Drupal/text/Plugin/Field/FieldFormatter/TextPlainFormatter.php rename to core/modules/field/lib/Drupal/field/Plugin/Field/FieldFormatter/FieldTextFormatter.php index 6c15bf5..95038b6 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldFormatter/TextPlainFormatter.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Field/FieldFormatter/FieldTextFormatter.php @@ -2,11 +2,12 @@ /** * @file - * Contains \Drupal\text\Plugin\field\formatter\TextPlainFormatter. + * Contains \Drupal\field\Plugin\Field\FieldFormatter\FieldTextFormatter. */ -namespace Drupal\text\Plugin\Field\FieldFormatter; +namespace Drupal\field\Plugin\Field\FieldFormatter; +use Drupal\Component\Utility\String; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; @@ -17,16 +18,15 @@ * id = "text_plain", * label = @Translation("Plain text"), * field_types = { - * "text", - * "text_long", - * "text_with_summary" + * "string", + * "integer" * }, * edit = { * "editor" = "plain_text" * } * ) */ -class TextPlainFormatter extends FormatterBase { +class FieldTextFormatter extends FormatterBase { /** * {@inheritdoc} @@ -37,7 +37,7 @@ public function viewElements(FieldItemListInterface $items) { foreach ($items as $delta => $item) { // The text value has no text format assigned to it, so the user input // should equal the output, including newlines. - $elements[$delta] = array('#markup' => nl2br(check_plain($item->value))); + $elements[$delta] = array('#markup' => nl2br(String::checkPlain($item->value))); } return $elements; diff --git a/core/modules/field/lib/Drupal/field/Plugin/Field/FieldWidget/TextWidget.php b/core/modules/field/lib/Drupal/field/Plugin/Field/FieldWidget/TextWidget.php new file mode 100644 index 0000000..d57e713 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Plugin/Field/FieldWidget/TextWidget.php @@ -0,0 +1,82 @@ + 'number', + '#title' => t('Size of textfield'), + '#default_value' => $this->getSetting('size'), + '#required' => TRUE, + '#min' => 1, + ); + $element['placeholder'] = array( + '#type' => 'textfield', + '#title' => t('Placeholder'), + '#default_value' => $this->getSetting('placeholder'), + '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), + ); + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = array(); + + $summary[] = t('Textfield size: !size', array('!size' => $this->getSetting('size'))); + $placeholder = $this->getSetting('placeholder'); + if (!empty($placeholder)) { + $summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder)); + } + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { + $element['value'] = $element + array( + '#type' => 'textfield', + '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, + '#size' => $this->getSetting('size'), + '#placeholder' => $this->getSetting('placeholder'), + '#maxlength' => $this->getFieldSetting('max_length'), + '#attributes' => array('class' => array('text-full')), + ); + + return $element; + } + +} diff --git a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php index cd46217..18d89bf 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverviewBase.php @@ -53,7 +53,8 @@ public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManager $field_type_manager, PluginManagerBase $plugin_manager) { parent::__construct($entity_manager); - $this->fieldTypes = $field_type_manager->getConfigurableDefinitions(); + //$this->fieldTypes = $field_type_manager->getConfigurableDefinitions(); + $this->fieldTypes = $field_type_manager->getDefinitions(); $this->pluginManager = $plugin_manager; } diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index 017e89c..5239924 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -379,7 +379,7 @@ public static function baseFieldDefinitions($entity_type) { ->setLabel(t('Language code')) ->setDescription(t('The node language code.')); - $fields['title'] = FieldDefinition::create('text') + $fields['title'] = FieldDefinition::create('string') // @todo Account for $node_type->title_label when per-bundle overrides are // possible - https://drupal.org/node/2114707. ->setLabel(t('Title')) @@ -390,15 +390,14 @@ public static function baseFieldDefinitions($entity_type) { ->setSettings(array( 'default_value' => '', 'max_length' => 255, - 'text_processing' => 0, )) ->setDisplayOptions('view', array( 'label' => 'hidden', - 'type' => 'text_default', + 'type' => 'plain_text', 'weight' => -5, )) ->setDisplayOptions('form', array( - 'type' => 'text_textfield', + 'type' => 'field_text', 'weight' => -5, )) ->setDisplayConfigurable('form', TRUE); diff --git a/core/modules/text/text.module b/core/modules/text/text.module index 675c63e..95a4326 100644 --- a/core/modules/text/text.module +++ b/core/modules/text/text.module @@ -189,3 +189,13 @@ function text_filter_format_update($format) { function text_filter_format_disable($format) { field_cache_clear(); } + +/** + * Implements hook_field_formatter_info_alter(). + */ +function text_field_formatter_info_alter(&$info) { + // Let a new field types to re-use an existing formatter. + $info['text_plain']['field_types'][] = 'text'; + $info['text_plain']['field_types'][] = 'text_long'; + $info['text_plain']['field_types'][] = 'text_with_summary'; +}