1

I was referring this page in the process of learning hooks How to Customize the Block Search Form and found following code.

/**
* hook_form_FORM_ID_alter
*/
function YOURTHEME_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    $form['search_block_form']['#title'] = t('Search'); // Change the text on the label element
    $form['search_block_form']['#title_display'] = 'invisible'; // Toggle label visibilty
    $form['search_block_form']['#size'] = 40;  // define size of the textfield
    $form['search_block_form']['#default_value'] = t('Search'); // Set a default value for the textfield
    $form['actions']['submit']['#value'] = t('GO!'); // Change the text on the submit button
    $form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/search-button.png');

    // Add extra attributes to the text box
    $form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = 'Search';}";
    $form['search_block_form']['#attributes']['onfocus'] = "if (this.value == 'Search') {this.value = '';}";
    // Prevent user from searching the default text
    $form['#attributes']['onsubmit'] = "if(this.search_block_form.value=='Search'){ alert('Please enter a search'); return false; }";

    // Alternative (HTML5) placeholder attribute instead of using the javascript
    $form['search_block_form']['#attributes']['placeholder'] = t('Search');
} 

This code is very helpful and precise to point. For example, if we want to change the placeholder, we need to put following code inside the template.php file and it works.

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

Now,

  1. From the literature on api.drupal.org, I can get the information that Hook, YOURTHEME_form_FORM_ID_alter(&$form, &$form_state, $form_id), can be used to alter the various parts of forms.

  2. But on that page, there is nowhere a description about any part which all are described in above first code, For example, $form['search_block_form']['#attributes']['placeholder'] = t('Search'); This code is used to change the placeholder. And same way all other like title, #title_display etc...

Why I am asking this, because for hooks all things are already defines and to use them we have to use them as they are provided, So What is the method to connect this all dots, from Hook name to what can we change with that hook.

Precise Question,

  1. To make this question to the point, I what to know from where on api.drupal.org shall I find out, this part $form['search_block_form']['#attributes']['placeholder']? Or how shall I come to know that, if I have to change placeholder I have to use $form['search_block_form']['#attributes']['placeholder'] this code only?
1

where on api.drupal.org shall I find out, this part $form['search_block_form']['#attributes']['placeholder']?

I don't know, but this is basic PHP. $form is a multidimensional array with keys of search_block_form, #attributes, placeholder, etc...

All I know is that you can use the Devel module to see the structure and what's inside of the array by using dpm($form)

function YOURTHEME_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    dpm($form);
}
  • 2
    Ok, if we can come to know that tree using Devel module, then Devel module is the answer. I will test and update. Thx – CodeNext Jul 25 '16 at 8:51
  • 1
    I realize SE-participants are not allowed to ask for translation help (they should have dictionaries handy). At least that's what I remember from a moderator comment in the past (so unser-unfriendly I think for anybody who did not learn English on their mother's knees ...). But does that also apply to knowing what all the abbreviations mean? If it is relevant to understand the answer, would you mind explaining what IDK means? Here are my guesses: "I Do Know" or otherwise "I Don't Know" ... AFAIK, and IMO, time for LOL, ... CU! – Pierre.Vriens Jul 25 '16 at 11:01
  • Ok, I understand something. The Devel module is working perfectly fine. but not showing placeholder. I was under impression that, the placeholder is something drupal code part, hence it should be somewhere in code. But as I look carefully I realize it is an HTML attribute. Hence, there is not a point to find it in devel. So it is just applying HTML thing. Same way in code in question, onblur, onfocus, onsubmit are Javascript functions. Hence, also not a point to find. Hence remaining part ['search_block_form']['#attributes'] is itself explanatory. Hope I understand correctly? – CodeNext Jul 25 '16 at 13:42
  • 1
    @CodeNext that is correct. Since it is an HTML Attribute, you would only see it if the form had a placeholder already, but not if the form doesn't have one yet. Which is why the documentation that Clive's answer links to is very important. – No Sssweat Jul 25 '16 at 13:52
2

So What is the method to connect this all dots, from Hook name to what can we change with that hook.

There isn't one - each hook is different, has different input, different output, different contents, affects different parts of the system, and so on. You have to look for documentation contextual to what you're working with, each time you have something different.

In this case, you're working with a form, which means you should be referring to the Form API. From there it's an easy search to find information about #attributes:

Description: Additional HTML attributes, such as 'class' can be set using this mechanism.

Values: Any HTML attribute not covered by other properties, e.g. class (for control types), enctype (for forms).

It's basically an arbitrary pass-through of the key/value, so

$form['search_block_form']['#attributes']['foo'] = 'bar';

Will get you (e.g.)

<input type="text" foo="bar" />
  • WOW the form API documentation sure is in-depth, I wish all documentation was like this. – No Sssweat Jul 25 '16 at 9:47
  • Is it the same for D8? I don't see a link at top. – No Sssweat Jul 25 '16 at 9:49
  • 2
    It's pretty similar @NoSssweat, there's an article here which goes into a bit more detail – Clive Jul 25 '16 at 10:49
  • Thanks for explanation, it helped in overall understanding. – CodeNext Jul 25 '16 at 14:11

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.