如何正确实现hook_views_plugins?

时间:2011-06-22 10:58:49

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

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

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',
      )
    ),
  );
}
?>

主题/我的-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目录中,要么放在我模块自己的插件目录中。我也在网上找不到任何好的例子,我也没有得到任何有用的错误信息来帮我调试。

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

非常感谢!

1 个答案:

答案 0 :(得分:1)

这里有一个教程:http://groups.drupal.org/node/10129。向下滚动到“编写视图2样式和行插件”。在教程示例中,它们同时创建样式和行插件。

作为替代方案,我尝试创建一个类似于你的样式插件,除了我将'use row plugin'设置为false。然后我能够从我的views-view-myplugin.tpl.php模板中访问原始rowdata。

相关问题