Textfield.php

Same filename in this branch
  1. 11.x core/modules/config_translation/src/FormElement/Textfield.php
  2. 11.x core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  3. 11.x core/modules/text/src/Plugin/migrate/field/d7/TextField.php
Same filename in other branches
  1. 9 core/modules/config_translation/src/FormElement/Textfield.php
  2. 9 core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  3. 9 core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  4. 9 core/lib/Drupal/Core/Render/Element/Textfield.php
  5. 8.9.x core/modules/config_translation/src/FormElement/Textfield.php
  6. 8.9.x core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  7. 8.9.x core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  8. 8.9.x core/modules/text/src/Plugin/migrate/cckfield/TextField.php
  9. 8.9.x core/lib/Drupal/Core/Render/Element/Textfield.php
  10. 10 core/modules/config_translation/src/FormElement/Textfield.php
  11. 10 core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  12. 10 core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  13. 10 core/lib/Drupal/Core/Render/Element/Textfield.php

Namespace

Drupal\Core\Render\Element

File

core/lib/Drupal/Core/Render/Element/Textfield.php

View source
<?php

namespace Drupal\Core\Render\Element;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Attribute\FormElement;
use Drupal\Core\Render\Element;

/**
 * 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:
 * @code
 * $form['title'] = [
 *   '#type' => 'textfield',
 *   '#title' => $this->t('Subject'),
 *   '#default_value' => $node->title,
 *   '#size' => 60,
 *   '#maxlength' => 128,
 *   '#pattern' => 'some-prefix-[a-z]+',
 *   '#required' => TRUE,
 * ];
 * @endcode
 *
 * @see \Drupal\Core\Render\Element\Color
 * @see \Drupal\Core\Render\Element\Email
 * @see \Drupal\Core\Render\Element\MachineName
 * @see \Drupal\Core\Render\Element\Number
 * @see \Drupal\Core\Render\Element\Password
 * @see \Drupal\Core\Render\Element\PasswordConfirm
 * @see \Drupal\Core\Render\Element\Range
 * @see \Drupal\Core\Render\Element\Tel
 * @see \Drupal\Core\Render\Element\Url
 */
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;
    }

}

Classes

Title Deprecated Summary
Textfield Provides a one-line text field form element.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.