Drupal Views模块不从外部数据库中获取表

时间:2013-01-31 18:47:48

标签: php drupal drupal-7 views drupal-views

我一直在关注Drupal 7的Views 3帮助文件,但我有点卡在我需要添加到模块的其他内容上,以使其对Views可见,因此我可以使用Views来显示数据在我的外部数据库中。

当然我的真实数据库有更多有用的字段,但是我无法显示它 - 所以我在尝试更复杂的事情之前将这个测试数据库改为“hello world”。这是架构。

database name = other

create table strings (
id int primary key auto_increment,
mystring varchar(50)
);

这是我的settings.php包含数据库:

<?php
// ...

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'drupal',
      'username' => 'drupal_user',
      'password' => 'my_other_pass',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),

    array (
        'database' => 'other',
        'username' => 'my_user',
        'password' => 'my_pass',
        'host' => 'localhost',
        'port' => '',
        'driver' => 'mysql',
        'prefix' => '',
    ),
  ),
);

//...

?>

这是我的test.views.inc文件,我的自定义模块名为test(已启用),它向Drupal描述了mystrings的表结构是什么样的。

<?php
//useful site explaining all of this: http://groups.drupal.org/node/17236
function test_views_data() {
    $data = array(
        'strings' => array(
            'table' => array(
                'group' => t('views test'),

                'base' => array(
                    'field' => 'id',
                    'title' => t("I guess node"),
                    'help' => t("help for I guess node I guess"),
                    'weight' => -10,
                    'database' => 'others',
                ),
            ),

            'id' => array(
                'title' => t('id'),
                /*'field' => array(
                    'handler' => 'views_handler_field_node',
                    'click sortable' => TRUE,
                ),*/
                'relationship' => array(
                    'label' => t("node I think"),
                    'base' => 'node',
                    'base_field => 'id',
                ),
                /*'argument' => array(
                    'handler' => 'view_handler_argument_node_nid',
                    'name field' => 'id for strings',
                    'numeric' => TRUE,
                    'validate type' => 'nid',
                ),*/

                /*'filter' => array(
                    'handler' => 'views_handler_filter_numeric',
                ),*/

                /*'sort' => array(
                    'handler' => 'views_handler_sort',
                ),*/
            ),

            'mystring' => array(
                'title' => t('mystring'),
                'field' => array(
                    'handler' => 'views_handler_field',
                    'click sortable' => TRUE,
                ),
                'filter' => array(
                    'handler' => 'views_handler_filter_string',
                ),
                'argument' => array(
                    'handler' => 'views_handler_argument_string',
                ),
                'sort' => array(
                    'handler' => 'views_handler_sort',
                ),
            ),
        ),
    );

    return $data;
}

这是我的test.module文件:

<?php

function test_help($section) {
    switch($section) {
        case "admin/help#test":
        return "<p>hello from test</p>";

        case "admin/modules#description":
        return "hello from test inside the admin help thing";
    }
}

function test_page() {
    return "<p>hello from the actual test page</p>";
}

function test_views_api() {
    return array('api' => 3.0);
}

这是当前的问题:当我尝试创建一个新视图时,我没有看到任何这些信息或任何与我的数据库和表格mystrings相关的信息。有人在我的自定义模块代码/视图用法中知道我做错了什么吗?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

我只是设置一个基本模块来测试它,它工作正常。我能用你的代码看到的主要问题是settings.php文件。

的settings.php

<?php
$databases = array (
  'default' => array (
      'default' => array (
      'database' => 'drupaldev',
      'username' => 'drupal_user',
      'password' => '',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),

  'testdb' => array(
    'default' => array (
      'database' => 'testdb',
      'username' => 'testdb_user',
      'password' => '',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

so_views.module

<?php

/**
 * Implements hook_views_api().
 * 
 * @return array
 */
function so_views_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_views_data().
 *
 * @return array
 */
function so_views_views_data() {
  return array(
    'example_table' => array(
      'table' => array(
        'group' => t('SO View Table'),
        'base' => array(
          'field' => 'nid',
          'title' => t('SO new Table'),
          'help' => t('Table contains data'),
          'weight' => -10,
          'database' => 'testdb',
        ),
        'join' => array(
          'node' => array(
            'left_field' => 'nid',
            'field' => 'nid',
          ),
        ),
      ),
      'nid' => array(
        'title' => t('Node Id'),
        'help' => t('This is the node Id'),
        'relationship' => array(
          'base' => 'node',
          'base field' => 'nid',
          'handler' => 'views_handler_relationship',
          'label' => t('Default label for the relationship'),
          'title' => t('Title shown when adding the relationship'),
          'help' => t('More information on this relationship'),
        ),
      ),
      'plain_text_field' => array(
        'title' => t('Plain text field'),
        'help' => t('Just a plain text field.'),
        'field' => array(
          'handler' => 'views_handler_field',
          'click sortable' => TRUE, // This is use by the table display plugin.
        ),
        'sort' => array(
          'handler' => 'views_handler_sort',
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_string',
        ),
        'argument' => array(
          'handler' => 'views_handler_argument_string',
        ),
      ),
      'numeric_field' => array(
        'title' => t('Numeric field'),
        'help' => t('Just a numeric field.'),
        'field' => array(
          'handler' => 'views_handler_field_numeric',
          'click sortable' => TRUE,
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_numeric',
        ),
        'sort' => array(
          'handler' => 'views_handler_sort',
        ),
      ),
      'boolean_field' => array(
        'title' => t('Boolean field'),
        'help' => t('Just an on/off field.'),
        'field' => array(
          'handler' => 'views_handler_field_boolean',
          'click sortable' => TRUE,
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_boolean_operator',
          // Note that you can override the field-wide label:
          'label' => t('Published'),
          // This setting is used by the boolean filter handler, as possible option.
          'type' => 'yes-no',
          // use boolean_field = 1 instead of boolean_field <> 0 in WHERE statment.
          'use equal' => TRUE,
        ),
        'sort' => array(
          'handler' => 'views_handler_sort',
        ),
      ),
      'timestamp_field' => array(
        'title' => t('Timestamp field'),
        'help' => t('Just a timestamp field.'),
        'field' => array(
          'handler' => 'views_handler_field_date',
          'click sortable' => TRUE,
        ),
        'sort' => array(
          'handler' => 'views_handler_sort_date',
        ),
        'filter' => array(
          'handler' => 'views_handler_filter_date',
        ),
      ),
    ),
  );
}

所有与视图相关的视图都来自文档。我建议安装advanced_help模块并查看yoursite.com/views/api-tables。

如果你想在另一个模块中设置它,那么创建一个info文件,找到所需的sql here