Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Textfield.php

class Textfield

Same name in this branch
  1. 11.1.x core/modules/text/src/Plugin/migrate/field/d6/TextField.php \Drupal\text\Plugin\migrate\field\d6\TextField
  2. 11.1.x core/modules/text/src/Plugin/migrate/field/d7/TextField.php \Drupal\text\Plugin\migrate\field\d7\TextField
  3. 11.1.x core/modules/config_translation/src/FormElement/Textfield.php \Drupal\config_translation\FormElement\Textfield

Provides a one-line text field form element.

Properties:

  • #maxlength: Maximum number of characters of input allowed.
  • #size: The size of the input element in characters.
  • #autocomplete_route_name: A route to be used as callback URL by the autocomplete JavaScript library.
  • #autocomplete_route_parameters: An array of parameters to be used in conjunction with the route name.
  • #pattern: A string for the native HTML5 pattern attribute.

Usage example:

$form['title'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Subject'),
    '#default_value' => $node->title,
    '#size' => 60,
    '#maxlength' => 128,
    '#pattern' => 'some-prefix-[a-z]+',
    '#required' => TRUE,
];

Hierarchy

  • class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
    • class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
      • class \Drupal\Core\Render\Element\RenderElementBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Render\Element\ElementInterface
        • class \Drupal\Core\Render\Element\FormElementBase extends \Drupal\Core\Render\Element\RenderElementBase implements \Drupal\Core\Render\Element\FormElementInterface
          • class \Drupal\Core\Render\Element\Textfield extends \Drupal\Core\Render\Element\FormElementBase

Expanded class hierarchy of Textfield

See also

\Drupal\Core\Render\Element

\Drupal\Core\Render\Element\Textarea

1 file declares its use of Textfield
EntityAutocomplete.php in core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
9 string references to 'Textfield'
ContentTranslationHandler::addTranslatabilityClue in core/modules/content_translation/src/ContentTranslationHandler.php
Adds a clue about the form element translatability.
EntityField::multiple_options_form in core/modules/views/src/Plugin/views/field/EntityField.php
Provide options for multiple value fields.
file.settings.yml in core/modules/file/config/install/file.settings.yml
core/modules/file/config/install/file.settings.yml
IconExtractorSettingsForm::buildStringForm in core/lib/Drupal/Core/Theme/Icon/IconExtractorSettingsForm.php
Build Drupal form for a string setting to a textfield.
RenderElementBase::preRenderAjaxForm in core/lib/Drupal/Core/Render/Element/RenderElementBase.php
Adds Ajax information about an element to communicate with JavaScript.

... See full list

File

core/lib/Drupal/Core/Render/Element/Textfield.php, line 38

Namespace

Drupal\Core\Render\Element
View source
class Textfield extends FormElementBase {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() {
        $class = static::class;
        return [
            '#input' => TRUE,
            '#size' => 60,
            '#maxlength' => 128,
            '#autocomplete_route_name' => FALSE,
            '#process' => [
                [
                    $class,
                    'processAutocomplete',
                ],
                [
                    $class,
                    'processAjaxForm',
                ],
                [
                    $class,
                    'processPattern',
                ],
                [
                    $class,
                    'processGroup',
                ],
            ],
            '#pre_render' => [
                [
                    $class,
                    'preRenderTextfield',
                ],
                [
                    $class,
                    'preRenderGroup',
                ],
            ],
            '#theme' => 'input__textfield',
            '#theme_wrappers' => [
                'form_element',
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
        if ($input !== FALSE && $input !== NULL) {
            // This should be a string, but allow other scalars since they might be
            // valid input in programmatic form submissions.
            if (!is_scalar($input)) {
                $input = '';
            }
            return str_replace([
                "\r",
                "\n",
            ], '', $input);
        }
        return NULL;
    }
    
    /**
     * Prepares a #type 'textfield' render element for input.html.twig.
     *
     * @param array $element
     *   An associative array containing the properties of the element.
     *   Properties used: #title, #value, #description, #size, #maxlength,
     *   #placeholder, #required, #attributes.
     *
     * @return array
     *   The $element with prepared variables ready for input.html.twig.
     */
    public static function preRenderTextfield($element) {
        $element['#attributes']['type'] = 'text';
        Element::setAttributes($element, [
            'id',
            'name',
            'value',
            'size',
            'maxlength',
            'placeholder',
        ]);
        static::setAttributes($element, [
            'form-text',
        ]);
        return $element;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FormElementBase::processAutocomplete public static function Adds autocomplete functionality to elements. 1
FormElementBase::processPattern public static function #process callback for #pattern form element property. 1
FormElementBase::validatePattern public static function #element_validate callback for #pattern form element property. 1
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 5
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes 2
Textfield::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo 3
Textfield::preRenderTextfield public static function Prepares a #type 'textfield' render element for input.html.twig.
Textfield::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementBase::valueCallback 3

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal