3

In my template.php I'm trying to alter a form in a views exposed block, but I only want to do this if that block is in a certain region in my theme.

I'm using Drupal 7.

Is that possible? What php code would I use?

Here is what I'm using now:

function cmfzen_form_alter(&$form, &$form_state, $form_id) {

  // change the main search form
  // see http://drupal.stackexchange.com/questions/28404/easiest-way-to-add-x-webkit-speech-to-the-search-blocks-input-element
  // see http://adaptivethemes.com/html5-placeholder-text-for-your-drupal-7-search-field
  if ($form_id == "views_exposed_form" && $form['#id'] == "views-exposed-form-site-search-page"){

    $form['site-search']['#type'] = 'searchfield';
    $form['site-search']['#attributes'] = array(
      'class' => array('search-site'),
      'placeholder' => t("I'm looking for..."),
      'speech x-webkit-speech spellcheck' => 'false',
      );
    // change form submit button

    $form['submit']['#attributes'] = array('class' => array('search-button'));
  }
}
  • You will need hook_page_alter – AyeshK May 3 '13 at 17:01
  • Thank you, but could you provide more of an example? – gmclelland May 3 '13 at 17:06
3

use this code in hook_form_alter:

function cmfzen_form_alter(&$form, &$form_state, $form_id) {
    switch($form_id){
         case 'views_exposed_form':
         $region = 'NAME_OF_A_REGION';
         $blocks = block_list($region);
         kpr($blocks);
         //if the block is in the $blocks array do the changes.
         break;
    }
}

using block_list($region) you will get the blocks that are placed in a specific region, and it returns array of block objects (for more detail visit this page). Now check if the block you are looking for is in the $blocks array.

1

Try this


function cmfzen_form_alter(&$form, &$form_state, $form_id) {

  if (block_load('views','-exp-site_search-page')->region == 'header' &&
    $form_id == "views_exposed_form" &&
    $form['#id'] == "views-exposed-form-site-search-page"
  ) {
    // alter form
  }
}

Change 'header' to your region name and ensure the block delta '-exp-site_search-page' is the right one.

One more solution (may be better performance) is to make your own function that queries the database for just the region of a block :


function mymodule_block_region($module,$delta) {
    return db_query('SELECT region FROM {block} WHERE module = :module AND delta = :delta'
    , array(':module' => $module, ':delta' => $delta))->fetchField();
}

And then use it instead of block_load('views','-exp-site_search-page')->region

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.