سؤال

Hi I'm developing a new site with Drupal, For some reason I want to add a span or label (I prefer a <span>) before the input search field which is rendered by Drupal.

Is there any way to achieve it? I'm not really happy to modify the core files.

Any help would be appreciated.

Drupal Version : 7

هل كانت مفيدة؟

المحلول

You can use hook_form_BASE_FORM_ID_alter() hook to add your HTML to the existing markups,

/**
 * Implements hook_form_BASE_FORM_ID_alter();
 */
function <MODULE_NAME>_search_block_form_alter(&$form, &$form_state, $form_id) {
    //Reffer prefix from the api to add <span> or <label>
    // https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#prefix

   // Reffer prefix from the api to add </span> or </label>
//https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#suffix

}

نصائح أخرى

You usually achieve this with the preprocessfunctions

You can use such functions in theme folder. Assuming that your theme is called "example"

// template_preprocess(&$variables, $hook), replace template 
//with the name of your theme
function example_preprocess(&$variables, $hook)
{
   dsm($hook); // You need the Devel module in order to use dsm()...
}
// this should output a list of hooks used on your page, 
//look for search_form or similar...then you could do
function example_preprocess(&$variables, $hook)
{
   if($hook == 'search_form')
       dsm($variables);
}

Look at the list of variables to identify the content to be edited, then it's just a question of editing that content in your function.

You should find a lot of examples if you do some research!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top