2

I am currently printing the search from like this:

$block = module_invoke('search', 'block_view', 'search');
print render($block);

Wich generates:

<form action="/sescam/" method="post" id="search-block-form" accept-charset="UTF-8">
    <div>
        <div class="container-inline">
             <h2 class="element-invisible">Search form</h2>

            <div class="form-item form-type-textfield form-item-search-block-form">
                <label class="element-invisible" for="edit-search-block-form--2">Search</label>
                <input title="Enter the terms you wish to search for." type="text" id="edit-search-block-form--2" name="search_block_form" value="" size="15" maxlength="128" class="form-text" />
            </div>
            <div class="form-actions form-wrapper" id="edit-actions">
                <input type="submit" id="edit-submit" name="op" value="Search" class="form-submit" />
            </div>
            <input type="hidden" name="form_build_id" value="form-NhLKDp33kDMoJjHqw6gDFJw_A_Kp7Dhvrt_w0V-jk6U" />
            <input type="hidden" name="form_token" value="e9aNbw3gz2mWvZUSdjl54aXTlesQsNkn7cGSXVMpoWI" />
            <input type="hidden" name="form_id" value="search_block_form" />
        </div>
    </div>
</form>

The thing is that I would like to customize a little bit this html output,

How can I Do so?

-EDIT-

I tried to add this to my template.php

function my_theme_form_search_form_alter (& $form, & $form_state)
{
  print_r($form);
  print_r($form_state);
}

The thing is that those print_r won't output anything,

Anhy idea?

thanks!

  • The ID is search_block_form, so... – Clive Oct 6 '14 at 10:53
  • Hi @Clive Sorry but I am not familiar with Drupal yet, what do you mean? thanks ( If you mean I can customize the looks by selecting it with css, it's not really what I am looking for, I would like to customize the whole output..) – Toni Michel Caubet Oct 6 '14 at 10:54
  • Not what I meant, I've put an answer in – Clive Oct 6 '14 at 11:00
2

You're implementing hook_form_FORM_ID_alter(), to which you're providing search_form as the ID - if you look at the markup for the form you can see the ID should be search_block_form, so your function should be:

function MYTHEME_form_search_block_form_alter(&$form, &$form_state, $form_id) {
  // Make changes
}

To find out the ID of a form, you can temporarily implement the standard hook_form_alter(), and put the ID out to the screen:

function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);
}

Just as an aside, don't rely on seeing anything from print_r() - because of output buffering you may or may not see it. To debug Drupal, install the Devel module, and use the dpm() function to inspect vars.

| improve this answer | |
  • Hi again, I see.. Thing is I can play with the attributes and all that, but How should I rewrite the output? Shall I copy the original one? – Toni Michel Caubet Oct 6 '14 at 11:09
  • Depends entirely on what you want to change - drupal.stackexchange.com/questions/106894/… should give you a starting point – Clive Oct 6 '14 at 11:10
  • Thanks for your answer, the thing is that If I place $output = '<div></div>'; return $output I get the same previous output... any thoughts? – Toni Michel Caubet Oct 6 '14 at 11:17
  • 1
    You dont return output from the alter function implementation. You need to change values in the referenced variables provided instead. Eg $form['item']=array('#markup'=>'<h1>Huuhaa</h1>'); – user20301 Oct 6 '14 at 14:03
2

Just create a file search-block-form.tpl.php and put it inside your template folder.

Paste the following code into your file.

<?php

/**
 * @file
 * Displays the search form block.
 *
 * Available variables:
 * - $search_form: The complete search form ready for print.
 * - $search: Associative array of search elements. Can be used to print each
 *   form element separately.
 *
 * Default elements within $search:
 * - $search['search_block_form']: Text input area wrapped in a div.
 * - $search['actions']: Rendered form buttons.
 * - $search['hidden']: Hidden form elements. Used to validate forms when
 *   submitted.
 *
 * Modules can add to the search form, so it is recommended to check for their
 * existence before printing. The default keys will always exist. To check for
 * a module-provided field, use code like this:
 * @code
 *   <?php if (isset($search['extra_field'])): ?>
 *     <div class="extra-field">
 *       <?php print $search['extra_field']; ?>
 *     </div>
 *   <?php endif; ?>
 * @endcode
 *
 * @see template_preprocess_search_block_form()
 */
?>
<input type="text" id="edit-search-block-form--2" name="search_block_form" value="" placeholder="Search..." size="15" maxlength="128" />
<input class="button" id="edit-submit" name="op" value="GO!" type="submit" />
<?php echo $search['hidden']; ?>

Edit the code as you wish. Just make sure to leave echo $search['hidden'];.

You can find the original template in YOUR-WEBSITE/modules/search/search-clock-form.tpl.php.


On a side note: Instead of using print_r(), install Devel module and use dpm(). It's a lot better and is not visible to visitors.

| improve this answer | |
1

You can simply use the hook_form_alter as mentioned below.

Here is the code to be used in the theme's template.php file that will override Drupal's search form.

<?php
function theme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
      print_r($form);
  print_r($form_state); // Code here

 }
}
?>

Either way if you are working within your custom module. It will be used as

<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
      print_r($form);
  print_r($form_state); // Code here

 }
}
?>

Hope, it will work for you!

| improve this answer | |

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.