3

Inside a custom block plugin I'm creating, I programmatically load the user form and return it like so

public function build() {
    $fb       = \Drupal::formBuilder();
    $userform = $fb->getForm("Drupal\user\Form\UserLoginForm");

    $render = [
        '#type'       => 'container',
        '#attributes' => [
            'class'      => ['row', 'expanded'],
            'id'         => ['login-page-block']
        ],
        'userlogin' => [
            '#type'    => 'html_tag',
            '#tag'     => 'h3',
            '#value'   => 'Welcome Members!',
            'caption'  => [
                '#type'   => 'html_tag',
                '#tag'    => 'p',
                '#value'  => 'To access member-exclusive content on our website, you must login.',
            ],
            'form' => $userform,
        ],
    ];

    return $render;
}

It creates labels for the inputs for Username and Password, but I'd rather ditch the labels and put 'Username' and 'Password' as placeholders inside the input fields. However I'm not sure how I would go about doing that. I performed a die(print_r($userform)); to get a list of fields in the array but I didn't see anything applying to the array.

Is there a way to place placeholder text inside the text input fields of a form that was programmatically retrieved and rendered?

UPDATE: I attempted changing the placeholder by writing

$userform['keys']['#attributes']['placeholder'] = t('Username');

But that didn't seem to do anything.

4

I was able to add the placeholder text by entering

    $userform['name']['#attributes']['placeholder'] = t('Username');
    $userform['pass']['#attributes']['placeholder'] = t('Password');

After looking at the results of die(print_r($userform)); more closely I saw that the username input was called 'name', and password was 'pass'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.