【问题标题】:How can I programmatically pull a field from a custom drupal table into drupal 7 views which is joined by a id?如何以编程方式将自定义 drupal 表中的字段拉入由 id 连接的 drupal 7 视图?
【发布时间】:2012-06-18 20:54:23
【问题描述】:

关于 drupal 7 - 视图 3.0 API 我已经配置了一个视图(来自 UI)以从自定义内容类型和它显示 id 的字段之一中提取数据。我想创建一个连接(我希望以编程方式)到我的自定义表,并显示映射到视图中 id 的文本。我遇到的问题是如何找出将其加入哪个表和字段?在我的内容类型中,我创建了 field_game 字段。我的自定义表有 gameid 作为主键。

也许是这样的?

$data['MYCUSTOMTABLE']['table']['join'] = array(
// Index this array by the table name to which this table refers.
// 'left_field' is the primary key in the referenced table.
// 'field' is the foreign key in this table.
'node' => array(
  'left_table' => '??????'
  'left_field' => 'field_game', 
  'field' => 'gameid',
),
);

我搜索了高低,但没有什么能真正接近。任何帮助表示赞赏。

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    我在寻找类似问题时发现了这篇文章。但我已经解决了你的问题。

    我把这个放在 hook_views_query_alter($view, $query):

    if ($view->name == "kompasses") {
    
    $ga_join = new views_join();
    $ga_join->table = 'field_data_group_audience';
    $ga_join->field = 'entity_id';
    $ga_join->left_table = 'node';
    $ga_join->left_field = 'nid';
    $ga_join->type = 'inner';
    
    $query->add_relationship('audience_node', $ga_join, 'node', null);
    }
    

    这里要加入的表是:field_data_group_audience 和基表(来自名为“kompasses”的现有视图)。 Audience_node 将是连接的表别名。

    更多信息请见:http://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/class/views_plugin_query_default/7

    【讨论】:

      【解决方案2】:

      您已经很接近了,但请检查一下: Views 有关于如何在模块代码中执行此操作的文档。查看 views/docs/views.api.php 并阅读!

      http://drupalcontrib.org/api/drupal/contributions%21views%21views.api.php/7 是那里的源代码,里面充满了有用的 cmets(查看底部的源代码)。

      基本上你需要实现 hook_views_data_alter 来告诉 Views 你的表。这将允许您加入。要显示连接字段,您需要在传递给 hook_views_data_alter 的数组中设置正确的处理程序。查看views.api.php中的第343行

      <?php
      // This table references the {node} table. The declaration below creates an
      // 'implicit' relationship to the node table, so that when 'node' is the base
      // table, the fields are automatically available.
      $data['example_table']['table']['join'] = array(
        // Index this array by the table name to which this table refers.
        // 'left_field' is the primary key in the referenced table.
        // 'field' is the foreign key in this table.
        'node' => array(
          'left_field' => 'nid',
          'field' => 'nid',
        ),
      );
      
      // Next, describe each of the individual fields in this table to Views. This
      // is done by describing $data['example_table']['FIELD_NAME']. This part of
      // the array may then have further entries:
      //   - title: The label for the table field, as presented in Views.
      //   - help: The description text for the table field.
      //   - relation: A description of any relation handler for the table field.
      //   - field: A description of any field handler for the table field.
      //   - sort: A description of any sort handler for the table field.
      //   - filter: A description of any filter handler for the table field.
      //   - argument: A description of any argument handler for the table field.
      //   - area: A description of any handler for adding content to header,
      //     footer or as no result behaviour.
      //
      // The handler descriptions are described with examples below.
      
      // Node ID table field.
      $data['example_table']['nid'] = array(
        'title' => t('Example content'),
        'help' => t('Some example content that references a node.'),
        // The nid is a foreign key to the {node} table. This allows us to (easily)
        // add a relationship handler for this table field, making all the table
        // fields for the related node available.
        'relationship' => array(
          'base' => 'node', // The name of the table to join with
          'field' => 'nid', // The name of the field to join with
          'handler' => 'views_handler_relationship',
          'label' => t('Example node'),
        ),
      );
      

      【讨论】:

      • hook_views_data_alter() 用于更改从其他模块定义的数据,而不是从实现hook_views_data_alter() 的模块定义的数据;为此,有hook_views_data()
      • 在我的自定义表中没有任何节点 ID。那我怎么需要加入??用什么表??
      猜你喜欢
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2016-02-28
      相关资源
      最近更新 更多