5

I need to do some custom theming on the search block in Drupal 7.

Specifically, I need to add a class to the submit button that makes it invisible and removes it from the content flow. So:

<div class="form-actions form-wrapper" id="edit-actions">
  <input type="submit" id="edit-submit" name="op" value="Search" class="form-submit">
</div>

Becomes...

<div class="form-actions form-wrapper element-invisible" id="edit-actions">
  <input type="submit" id="edit-submit" name="op" value="Search" class="form-submit">
</div>

I added element-invisible to the edit-actions div element. However, I can't figure out how to do this. What's the best practice for doing this sort of thing?

13

One of the standard ways to do this would be to use hook_form_alter() and add the attributes to the actions element directly:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    $form['actions']['#attributes']['class'][] = 'element-invisible';
  }
}
  • Yeah if I can fully understand the way Drupal loads up it's arrays when dealing with forms I'll have a much happier time with this system. I love Drupal now, but when I finally figure that aspect of it out, I'll be in theming heaven. – Lester Peabody Mar 1 '12 at 23:08
  • Yeah it's a tricky one to get your head round at first but it's so powerful it really is worth learning about – Clive Mar 1 '12 at 23:11
0

You can also hide it using a php condition like this:

<?php
function your_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
      unset($form['actions']['submit']);
  }
}
?>

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.