2

I need to alter the text of the search input field. In my quest to do this, I have dpm() the content of $form on a call to hook_form_search_box_form_alter(). The output is the following.

Array
(
    [search_block_form] => Array
        (
            [#type] => textfield
            [#title] => Search
            [#title_display] => invisible
            [#size] => 15
            [#default_value] => 
            [#attributes] => Array
                (
                    [title] => Enter the terms you wish to search for.
                )

        )

    [actions] => Array
        (
            [#type] => actions
            [submit] => Array
                (
                    [#type] => submit
                    [#value] => Search
                )

        )

    [#submit] => Array
        (
            [0] => search_box_form_submit
        )

    [#form_id] => search_block_form
    [#type] => form
    [#build_id] => form-Y0XlzH4YO-EqVHxg5wnRf-PrnecuUSGdo5LTCPcLgLU
    [form_build_id] => Array
        (
            [#type] => hidden
            [#value] => form-Y0XlzH4YO-EqVHxg5wnRf-PrnecuUSGdo5LTCPcLgLU
            [#id] => form-Y0XlzH4YO-EqVHxg5wnRf-PrnecuUSGdo5LTCPcLgLU
            [#name] => form_build_id
            [#parents] => Array
                (
                    [0] => form_build_id
                )

        )

    [form_id] => Array
        (
            [#type] => hidden
            [#value] => search_block_form
            [#id] => edit-search-block-form
            [#parents] => Array
                (
                    [0] => form_id
                )

        )

    [#id] => search-block-form
    [#method] => post
    [#action] => /
    [#theme_wrappers] => Array
        (
            [0] => form
        )

    [#tree] => 
    [#parents] => Array
        (
        )

    [#validate] => Array
        (
        )

    [#theme] => Array
        (
            [0] => search_block_form
            [1] => search_box
        )

)

Is $form['search_block_form']['#title'] = ''; the right thing to do here?

2

1 Answer 1

1

@NoSssweat has provided a link, it is the answer if you want to alter a placeholder.

But reading your question I am getting other indications also.

FOR D7:

Case-1: If I read the sentence from your question "I need to alter the text of the search input field" I think you are talking about the placeholder, Now what is placeholder- you can see in following image the word "Search the site" in the text-area is called place-holder.

enter image description here

Case-2: But if I read your last sentence, "Is $form['search_block_form']['#title'] = ''; the right thing to do here?" then it talks about other things, this is corrosponds to Text appear when you hover on the input text-area("Enter the terms you wish to search for") as shown in following image.

enter image description here

I will explain both of that, The best tutorial for this is available here: How to Customize the Block Search Form.

Case-1
Paste the following code in your theme's template.php file, change the YourThemeName with your actual theme-name, save & clear the cache.

function YourThemeName_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    $form['search_block_form']['#attributes']['placeholder'] = t('Search the site');
}

Case-2

function YourThemeName_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    $form['search_block_form']['#attributes']['title'] = t('Place a new sentence');
}

enter image description here

Case-3: If you want title on the block, you can just go to block configuration and put the title whatever you want, it will appear as a title of the Block.

enter image description here

FOR D8:

as @NoSssweat pointed out in the following comment, how to do in D8. Hence, I am just repeating that comment here as it is needed.

function YourThemeNAme_form_search_block_form_alter(&$form, FormStateInterface $form_state) {
  $form['keys']['#attributes']['placeholder'] = t('The new placeholder');
}
3
  • I like that you went the extra mile and added pics.
    – No Sssweat
    Commented Aug 18, 2016 at 9:44
  • 1
    Just wanted to point out that for Drupal 8 instead of ['search_block_form'] you use ['keys']. Ex: $form['keys']['#attributes']['title'] = t('Search');
    – No Sssweat
    Commented Aug 18, 2016 at 10:10
  • @NoSssweat Thanks for pointing out, info about D8 is also needed here.
    – CodeNext
    Commented Aug 18, 2016 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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