【问题标题】:Styling/Theming Drupal 7 Content Type Record样式/主题 Drupal 7 内容类型记录
【发布时间】:2012-06-27 12:33:35
【问题描述】:

我开发了一个“汽车销售”的内容类型,包含以下字段:

  1. 制造商
  2. 型号
  3. 制作
  4. 燃料类型
  5. 变速箱(手动/自动)
  6. 颜色
  7. 已注册? (是/否)
  8. 里程
  9. 发动机功率
  10. 状况(新/翻新/二手)
  11. 价格
  12. 图片(多次上传)

我开发了这种内容类型的视图来显示汽车列表。现在我想为个人汽车销售记录开发一个屏幕/视图,如下所示:

除了排列字段,请注意我想在中间嵌入一个图片库。这可以通过 Drupal 7 Admin UI 实现,还是我需要创建自定义 CSS 和模板文件?如果我需要编辑某些模板文件/css,那些是什么?我正在使用 Zen 子主题。

【问题讨论】:

    标签: drupal-7 drupal-views drupal-theming


    【解决方案1】:

    我将通过创建一个页面,然后创建一个节点模板来完成此操作。首先创建一个新节点,然后记录模板名称的 NID。

    然后,在您的模板中,创建一个新文件,并按以下方式命名:node--[node id].tpl.php

    然后,在该文件中,粘贴以下帮助函数(或者,如果您要在站点的其他地方使用它,也可以将它放在 template.php 中):

    /**
     * Gets the resulting output of a view as an array of rows,
     * each containing the rendered fields of the view
     */
    function views_get_rendered_fields($name, $display_id = NULL) {
        $args = func_get_args();
        array_shift($args); // remove $name
        if (count($args)) {
            array_shift($args); // remove $display_id
        }
    
        $view = views_get_view($name);
        if (is_object($view)) {
            if (is_array($args)) {
              $view->set_arguments($args);
            }
            if (is_string($display_id)) {
              $view->set_display($display_id);
            }
            else {
              $view->init_display();
            }
            $view->pre_execute();
            $view->execute();
            $view->render();
            //dd($view->style_plugin);
            return $view->style_plugin->rendered_fields;
        } else {
            return array();
        }
    }
    

    然后将以下代码添加到您的模板中:

    <?php
      $cars = views_get_rendered_fields('view name', 'default', [...any arguments to be passed to the view]);
      foreach ($cars as $car): ?>
        <div>Put your mockup in here. It might be helpful to run <?php die('<pre>'.print_r($car, 1).'</pre>'); ?> to see what the $car array looks like.</div>
      <?php endforeach;
    ?>
    

    只需将代码中的占位符更改为您想要的任何标记,您就应该设置好!

    正如我上面提到的,使用&lt;?php die('&lt;pre&gt;'.print_r($car,1).'&lt;/pre&gt;'); ?&gt; 来直观地展示数组的打印效果总是很有帮助的。

    我在代码中一直使用views_get_rendered_fields,因为它允许我完全自定义视图的输出。

    提醒:每次创建新模板时,请务必清除缓存。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多