Drupal 7 开发内部培训资料, 分头诗人

在template.php里修改表单输出的问题-完美解决了

赞成!
1
否决!

下面是我在search.module里修改的,复制到template里就报错:Cannot redeclare search_form(),前面改为hook_form_search_form_alter或者主题名_search_form_alter报里一大堆变了未定义的错,请问怎么才能在template.php正确修改表单

function search_form($form, &$form_state, $action = '', $keys = '', $module = NULL, $prompt = NULL) {
  $module_info = FALSE;
  if (!$module) {
    $module_info = search_get_default_module_info();
  }
  else {
    $info = search_get_info();
    $module_info = isset($info[$module]) ? $info[$module] : FALSE;
  }

  // Sanity check.
  if (!$module_info) {
    form_set_error(NULL, t('Search is currently disabled.'), 'error');
    return $form;
  }

  if (!$action) {
    $action = 'search/' . $module_info['path'];
  }
  if (!isset($prompt)) {
    $prompt =NULL;
  }

  $form['#action'] = url($action);
  // Record the $action for later use in redirecting.
  $form_state['action'] = $action;
  $form['#attributes']['class'][] = 'search-form';
  $form['module'] = array('#type' => 'value', '#value' => $module);
  $form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('search-wrapper clearfix')));
  $form['basic']['keys'] = array(
    '#type' => 'textfield',
    '#title' => $prompt,
    '#default_value' => $keys,
    '#size' => $prompt ? 40 : 20,
    '#maxlength' => 255,
    '#attributes' => array('class' => array('search-text input-border-margic')),
  );
  // processed_keys is used to coordinate keyword passing between other forms
  // that hook into the basic search form.
  $form['basic']['processed_keys'] = array('#type' => 'value', '#value' => '');
  $form['basic']['submit'] = array('#type' => 'submit', '#attributes' => array('class' => array('search-button')),'#value' => t('搜索'));

  return $form;
}

2 个回答

赞成!
1
否决!

请用 hook_form_search_form_alter() 或者 hook_form_alter()方法,如果报一大堆错误,证明你用错了。看看相关的例子。 

赞成!
1
否决!
//Search-Page
function vector_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form') {
    $prompt = NULL; /* 这个是下面 $form['basic']['keys'] 的标题,一般都不要,设置为空,或者你可以不要这个变量,但是记得删除后面这个变量的调用 */
    $keys =arg(2); /* text框值,这里显示的是所搜索的文字,arg(2)是显示的网址第3段类容 */
//下面就不用说明了,可以自己加减class看变化,就知道修改对应的神马地方了!
    $form['basic'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('search-wrapper clearfix')),/*这个就是加class*/
    );

    $form['basic']['keys'] = array(
      '#type' => 'textfield', 
      '#title' => $prompt, 
      '#default_value' => $keys, 
      '#size' => $prompt ? 40 : 20, 
      '#maxlength' => 255,
      '#attributes' => array('class' => array('search-text input-border-margic')),
    );

    $form['basic']['processed_keys'] = array(
      '#type' => 'value',
      '#value' => '',
    );

    $form['basic']['submit'] = array(
      '#type' => 'submit',
      '#attributes' => array('class' => array('search-button')),
      '#value' => t('搜索'), /* 按钮值*/
    );
  }
}

这样就可以了,其中vector为我的主题名字