【问题标题】:How can I theme the template for edit or add a node for a specific content type?如何为模板设置主题以进行编辑或为特定内容类型添加节点?
【发布时间】:2010-12-05 01:07:48
【问题描述】:

我想为模板设置主题以进行编辑或为特定内容类型添加节点。
例如,为所有内容类型的表单设置主题,我使用文件page-node-{add|edit}.tpl.php(取决于我需要添加或编辑的内容)。

但我没有找到自定义节点类型的模板名称,例如 Products。

我只需要为产品设置主题,而不需要为其他内容类型设置主题。

我已尝试使用 page-node-edit-product.tpl.phppage-node-product-edit.tpl.php,但没有成功。

【问题讨论】:

    标签: php drupal templates drupal-6 preprocessor


    【解决方案1】:

    嗯。可能有更好的方法,但是预处理函数呢?

    我对 Drupal 还是很陌生,所以我可能会尝试这样的事情 [代码可能不起作用]:

    <?php
    function themeName_preprocess_page(&$vars, $hook) {
      if ((arg(0) == 'node') && (arg(1) == 'add' && arg(2) == 'product')) {
        $vars['template_files'][] =  'page-node-add-product';
      }
    }
    ?>
    

    制作新的预处理函数后,请务必清除缓存和主题注册表。

    【讨论】:

      【解决方案2】:

      我认为这是“正确”的做法。

      从节点模块:

      $form['#theme'] = array($node->type .'_node_form', 'node_form');
      

      所以 Drupal 将尝试主题化“product_node_form”

      这样您就可以创建一个实现此功能的模块。

      您需要实现 [hook_theme][1],并提供函数或模板。

      您可能会发现使用 [hook_form_alter][2] 添加一些类和普通的 CSS 来改变外观会更容易。

      【讨论】:

      • 啊,但我想使用一个完全不同的模板,而不是只为表单设置主题。
      • 虽然你可以按照你发布的那样做,但我不确定这是 OP 想要的。这只会更改实际表单的模板(当前不使用任何模板),不会更改正在使用的页面模板。从他的例子看来,这就是他想要的。
      【解决方案3】:
      function themename_preprocess_page(&$vars) { 
        // Add per content type pages
        if (isset($vars['node'])) {
          // Add template naming suggestion. It should alway use hyphens.
          // If node type is "custom_news", it will pickup "page-custom-news.tpl.php".
          $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
        }
      }
      

      在template.php中加入以上代码

      然后创建几个 tpl 文件

      1) 页面内容类型.tpl.php

      在显示和编辑内容时使用

      2) page-node-add-contenttype.tpl.php

      在添加该内容类型时使用。

      适用于 drupal 6。

      【讨论】:

        【解决方案4】:

        我自己是一个 drupal 菜鸟,但是像这样的东西(可能需要更多)有用吗?

        function phptemplate_node_form($form)
        {
          switch ($form['#node']->type) {
            case 'product':
            return theme_render_template(path_to_theme().'/node-edit-product.tpl.php', array('form' => $form));
            default:
             return theme_node_form($form);
        }
        }
        

        【讨论】:

          【解决方案5】:

          对我来说同样的问题。提示在哪里插入代码:

          <?php
          function themeName_preprocess_page(&$vars, $hook) {
            if ((arg(0) == 'node') && (arg(1) == 'add' || arg(2) == 'product')) {
              $vars['template_files'][] =  'page-node-add-product';
            }
          }
          ?>
          

          在 template.php 或 page-node - {add|edit}-example.tpl.php 中输入?

          【讨论】:

          • 在第二个比较中有一个错误它必须是 && 而不是 ||: 因为它适用于任何“添加”内容。
          【解决方案6】:

          我把它放在我主题目录下的 template.php 文件中:

          function MYTHEMENAME_theme($existing, $type, $theme, $path) {
            return array(
              // tell Drupal what template to use for the edit form
              family_individual_node_form' => array(
                  'arguments' => array('form' => NULL),
                  'template' => 'node-family_individual-edit'
              )
            );
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-08
            • 1970-01-01
            • 2021-05-12
            • 1970-01-01
            • 1970-01-01
            • 2023-03-09
            相关资源
            最近更新 更多