How to add a css class programmatically to a piece of code?


Tags: php,css,class,drupal

Problem :

I have this code that just paste the drupal(7) search form whatever I need to.

<?php print drupal_render(drupal_get_form('search_block_form')); ?> 

The thing is that there´s no way for me to target that particular input[type="text"] without modifying other text inputs within the page.

I´ve tried to wrap the code into a class DIV, let´s say special-search-form, so I could just do

.special-search-form input[type="text"] {}

But that won´t do. Is there a way to add a class inside that drupal_render() call?



Solution :

There are two ways to handle this.

A) Through a module

function hook_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'search_block_form':
      $form['#attributes']['class'][] = 'extra_class';
      break;
  }
}

I think this might also work too, though I'd need to clarify that ...

function hook_search_block_form_alter(...) {
  $form['#attributes']['class'][] = 'extra_class';
}

B) Through a theme [search-block-form.tpl.php]

<div class="extra_class">
  <?php echo $search_form; ?>
</div>

    CSS Howto..

    How to indent text like in a table, but without a table?

    css how to make the button in the middle of this code [closed]

    How can I get IE7 to see an HTC file in the same directory as my CSS?

    How to use an arrow for slideToggle indicator?

    Making dijit.Dialog Transparent when it shows up

    How to retrieve the background image url defined in CSS?

    How to adjust a div to not move when other sibling is faded out

    How to make website change its layout of image/text when the browser dimensions are changed?

    How to crop SVG file within HTML/CSS

    How to align horizontally nav bar in css?

    CSS Divs overlapping, how do I force one above the other?

    How can i get an image inside an li to disregard padding with css

    How to make CSS drop down menu appear/change on click

    How to make the effects of jQuery's $(element).css() persistent?

    how to style php file with css + 1 more than one element

    How To Embed font-awesome CSS into DOM Scripting (javascript)?

    How to make centered fixed width body responsive?

    How to write CSS selector for this page?

    How to align buttons below and right-aligned to a HTML list?

    How can I get CSS codes in a style element? [duplicate]

    How does Outlook.com have the title text underlined?

    How to align like-buttons on my website between
    elements and put a white background to them?

    how can I do this space between tables? [closed]

    Can't figure out how to fix CSS for 3rd party library element

    rails redirect_to notice - how to apply css

    CSS - how to align different font-sizes to each other?

    How to apply a responsive CSS height to an element using min/max-height and height?

    How to unbind element then allow that element to bind again

    How to change button CSS attributes

    how to change negative sign position css