在主题节点表单时建议不同的模板

时间:2011-02-18 13:11:12

标签: drupal drupal-6

function posts_theme($existing, $type, $theme, $path) {
  return array(
      'post_node_form' => array(
        'arguments' => array('form' => NULL),
        'template' => VARIABLE,
      )
  );
}

这是建议模板在Drupal 6中呈现'post_node_form'的方式。但我想从2个不同的路径获取节点编辑表单:

  • 通过AJAX通过drupal_get_form('post_node_form')
  • 通过默认节点/添加/发布

如果我根据路径(或任何其他条件)替换“VARIABLE”,它将无法工作,因为它似乎?缓存模板的名称,您需要刷新缓存以刷新它。

建议使用不同表单模板的任何解决方案?

请注意。这不是节点模板的情况(然后您可以将模板建议放在预处理挂钩中)。这是关于节点FORM。

2 个答案:

答案 0 :(得分:0)

将此函数添加/或修改(如果存在)到您主题的template.php中:

function phptemplate_preprocess_page(&$vars) {
  // ...
  $node = menu_get_object();
  if ($node->type == 'post') {
    $vars['template_files'][] = VARIABLE;
  } 
  // ...
}

答案 1 :(得分:0)

好的,我回答了我自己的问题:

解决方案的关键是钩子preprocess_NAME_OF_MY_FORM,它在每个页面加载时执行,可以在您的模块或主题中。

所以在我的案例中,我在“posts”模块中写道:

/**
 * Implementation of hook_theme().
 */
function posts_theme($existing, $type, $theme, $path) {      
  return array(
      'post_node_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'post-form-custom',

      )
  );
}

function posts_preprocess_post_node_form(&$vars) {   
   // I check the path to know if node_form is retrieve through normal way or ajax way.      
   if (check_plain(arg(0)) == 'node'){
      $vars['template_files'][] = 'post-form-default';
   }
}

我的模块文件夹中包含文件post-form-custom.tpl.phppost-form-default.tpl.php

相关问题