Textfield.php

Same filename in this branch
  1. 8.9.x core/modules/config_translation/src/FormElement/Textfield.php
  2. 8.9.x core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  3. 8.9.x core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  4. 8.9.x core/modules/text/src/Plugin/migrate/cckfield/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. 10 core/modules/config_translation/src/FormElement/Textfield.php
  6. 10 core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  7. 10 core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  8. 10 core/lib/Drupal/Core/Render/Element/Textfield.php
  9. 11.x core/modules/config_translation/src/FormElement/Textfield.php
  10. 11.x core/modules/text/src/Plugin/migrate/field/d6/TextField.php
  11. 11.x core/modules/text/src/Plugin/migrate/field/d7/TextField.php
  12. 11.x 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\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'] = array(
 *   '#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
 *
 * @FormElement("textfield")
 */
class Textfield extends FormElement {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() {
        $class = get_class($this);
        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.