【问题标题】:How to properly implement hook_views_plugins?如何正确实现 hook_views_plugins?
【发布时间】:2011-06-22 10:58:49
【问题描述】:

我正在尝试创建自己的自定义格式输出视图。我在这段代码中取得了部分成功:

my_module.module:

<?php
function my_module_views_api() { // your module name into hook_views_api
  return array(
    'api' => 2,
    // might not need the line below, but in any case, the last arg is the name of your module
    'path' => drupal_get_path('module', 'my_module'),
  );
}
?>

my_module.views.inc:

<?php
/**
 * Implementation of hook_views_plugins().
 */
function my_module_views_plugins() {
  $path = drupal_get_path('module', 'my_module');
  return array(
    'style' => array(
      'my_module' => array(
        'title' => t('my_module'),
        'handler' => 'views_plugin_style_default',
        'theme' => 'my_module',
        'theme path' => $path . '/theme',
        'theme file' => 'my-module.tpl.php',
        'uses row plugin' => TRUE,
        'uses row class' => TRUE,
        'uses grouping' => TRUE,
        'uses options' => TRUE,
        'type' => 'normal',
      )
    ),
  );
}
?>

主题/my-module.tpl.php:

<?php
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
 if (!empty($title)): 
   print $title; 
 endif; 
 foreach ($rows as $id => $row): 
 ?>
 ROW:
 <?php
 print_r($row);
 endforeach; 
 ?>

上面的成功在于它将使用我的自定义 my-module.tpl.php 来输出行。但是,这些行是预先格式化的,可以由 views_plugin_style_default 处理程序推测。我花了几个小时尝试创建自己的此类处理程序,但没有成功,要么将其直接放在 views/plugins 目录中,要么放在我模块自己的插件目录中。我在网上也找不到任何好的示例,也没有得到任何有用的错误消息来帮助我调试。

是否有任何关于如何创建自定义视图处理程序的适当文档?或者你能提供一个可行的例子吗?

非常感谢!

【问题讨论】:

    标签: drupal drupal-views drupal-7 drupal-hooks


    【解决方案1】:

    这里有一个教程:http://groups.drupal.org/node/10129。向下滚动到“Writing Views 2 style and row plugins”。在教程示例中,他们创建了样式和行插件。

    作为替代方案,我尝试只创建一个样式插件,与您的类似,只是我将“使用行插件”设置为 false。然后我可以从我的 views-view-myplugin.tpl.php 模板中访问原始行数据。

    【讨论】:

    • 感谢您的回复。如果我在上面的代码中更改为 'uses row plugin' => FALSE,所有输出都会消失。不知道为什么。我可以使用不同的处理程序,例如views_plugin_style_default 和views_plugin_style_table。但是输出总是一样的。我去看看教程。
    猜你喜欢
    • 2015-02-27
    • 2013-07-22
    • 2021-08-17
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多