Suggesting different templates when theming a node form

て烟熏妆下的殇ゞ 提交于 2019-12-11 04:41:22

问题


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

This is the way of suggesting a template to render the 'post_node_form' in Drupal 6. BUT I want to get the node editing form from 2 different paths:

  • via AJAX through drupal_get_form('post_node_form')
  • via default node/add/post

If I replace "VARIABLE" depending on the path (or whatever other condition), it will not work because it seems? the name of the template is cached and you need to flush caches to refresh it.

Any solution of suggesting different form templates?

NOTE. This is not the case of node template, (then you can put the template suggestions in the preprocess hooks). It's about node FORM.


回答1:


Add this function/or modify if exists into template.php of your theme:

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



回答2:


Ok, I answer my own question:

The key of the solution is the hook preprocess_NAME_OF_MY_FORM , that is executed every page load and can be in your module or your theme.

So in my case, I wrote in my "posts" module:

/**
 * 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';
   }
}

I had in my module folder the files post-form-custom.tpl.php and post-form-default.tpl.php



来源:https://stackoverflow.com/questions/5041667/suggesting-different-templates-when-theming-a-node-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!