Views integration
CorpX - May 1, 2008 - 12:36
| Project: | Type-local nids |
| Version: | 6.x-1.2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | jbrown |
| Status: | active |
Description
Views integration
| Project: | Type-local nids |
| Version: | 6.x-1.2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | jbrown |
| Status: | active |
Views integration
#1
I have created views integration for the Type local nids module. This is only for Drupal 6 with the current Views 2.
Integration of the type_local_nids was reasonably simple but both integration and usage of information on how many nodes there are of each type gave a lot of complexity.
A1) Usage for Local nids
For normal usage within Views you would start by clicking on the Add tab. You have a number of Type options here. Normally you would select 'Node'.
When you initially go into the edit window of Views and add fields you will have extra fields available under the group Type local nids. You would normally just use the lnid field.
A2) Usage for reporting number of nodes of each type
Since Type local nids contains a database table that effectively gives the total number of nodes for each node type, I would expect this to be useful information for reports etc. I have attempted to add this into the views integration but it caused some problems. The table relates to the Drupals node_type table but this table is currently not integrated in current Views implementations. Consequently, attempting to integrate this information into Views required integrating most of this table as well.
Usage is different to that for Local nids.
When you click on the Add tab, included at or near the bottom of the Type options is a new type called Node type. Select this. When you initially go into the edit window of Views and add fields you will have fields available from the Node_type table along with next_lnid from the node_lnid_next table. The next_lnid is the number that will next be assigned for the local nid of this type. It is one greater than the number of nodes of this type.
Yes, it would have been better to have already subtracted one from it but (I presume) this would require writing a new handler and I have already spent too much time on this and I'm not sure if anybody wants it.
B) Implementation
For the tables defined by the type_local_nids to be understood by the views module you need to create a type_local_nids.views.inc file that describes the table and its relationships to the rest of the database. In order for views to know that this file is to be loaded you need to implement the hook_views_api by adding the following function into your type_local_nids.module file: You just need to copy the function (not the opening and closing ?php tags) into the bottom of your type_local_nids.module file.
<?php/**
* Implementation of hook_views_api().
*/
function type_local_nids_views_api() {
return array('api' => 2.0);
}
?>
Below is the contents of a type_local_nids.views.inc file that will allow you to create views for Drupal 6 with Views 2 that include the Type local nid module information: Copy all the below, including the opening ?php tag but not including the closing ?>, into an emply file. Name the file type_local_nids.views.inc and store it in the same directory as the type_local_nids.module file.
<?php
// $Id$
/**
* This file is used to tell the views module about the new
* node_lnid_next and node_lnid tables.
* We also use it to tell the views module about the node_type table.
*
* Database definition:
* @code
* CREATE TABLE node_lnid_next (
* type varchar(32) NOT NULL default '0',
* next_lnid int(10) unsigned NOT NULL default '0',
* PRIMARY KEY (type)
* KEY `node_lnid_next_type` (type)
* )
* CREATE TABLE node_lnid (
* nid int(10) unsigned NOT NULL default '0',
* lnid int(10) unsigned NOT NULL default '0',
* PRIMARY KEY (nid),
* KEY `node_lnid_nid` (nid)
* )
* CREATE TABLE node_type (
* type varchar(32) NOT NULL default '0',
* module varchar(32) NOT NULL default '0',
* description varchar(32) NOT NULL default '0',
* help varchar(32) NOT NULL default '0',
* has_title tinyint(3) unsigned NOT NULL default '0',
* title_label varchar(32) NOT NULL default '0',
* has_body tinyint(3) unsigned NOT NULL default '0',
* body_label varchar(32) NOT NULL default '0',
* min_word_count tinyint(35) unsigned NOT NULL default '0',
* (plus other fields we aren't bothering with),
* PRIMARY KEY (type)
* KEY `node_type_type` (type)
* )
* @endcode
*/
function type_local_nids_views_data() {
// Basic table information.
$data = array();
// ----------------------------------------------------------------
// New groups within Views called 'Type local nids' and 'Node type'
$data['node_lnid_next']['table']['group'] = t('Type local nids');
$data['node_lnid']['table']['group'] = t('Type local nids');
$data['node_type']['table']['group'] = t('Node type');
// tables + fields that can be used for SQL Joins to the node_lnid_next table.
// Joins described for joins to the node_type and the node tables.
$data['node_lnid_next']['table']['join'] = array(
'node_type' => array(
'left_field' => 'type',
'field' => 'type',
),
'node' => array(
'left_field' => 'type',
'field' => 'type',
'type' => 'INNER',
),
);
// tables + fields that can be used for SQL Joins to the node_lnid table
// Joins described for a join to the node table.
$data['node_lnid']['table']['join'] = array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
),
);
// tables + fields that can be used for SQL Joins to the node_type table
// Joins described for a join to the node table.
$data['node_type']['table']['join'] = array(
'node' => array(
'left_field' => 'type',
'field' => 'type',
'type' => 'INNER',
),
'node_lnid_next' => array(
'left_field' => 'type',
'field' => 'type',
),
);
// next_lnid field from the node_lnid_next table
$data['node_lnid_next']['next_lnid'] = array(
'title' => t('Next lnid'),
'help' => t('Next lnid that will be assigned. i.e. Number of lnids assigned for type plus 1'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// lnid field from the node_lnid table
$data['node_lnid']['lnid'] = array(
'title' => t('lnid'),
'help' => t('lnid for this node.'),
'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',
),
);
// Type field from the node_type table
$data['node_type']['type'] = array(
'title' => t('Type'),
'help' => t('Node type.'),
'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',
),
);
// Name field from the node_type table
$data['node_type']['name'] = array(
'title' => t('Name'),
'help' => t('Name of Node Type.'),
'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',
),
);
// Module field from the node_type table
$data['node_type']['module'] = array(
'title' => t('Module'),
'help' => t('Node Types Module.'),
'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',
),
);
// Description field from the node_type table
$data['node_type']['description'] = array(
'title' => t('Description'),
'help' => t('Description of Node Type.'),
'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',
),
);
// Help field from the node_type table
$data['node_type']['help'] = array(
'title' => t('Help'),
'help' => t('Help for Node Type.'),
'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',
),
);
// has_title field from the node_type table
$data['node_type']['has_title'] = array(
'title' => t('Has title'),
'help' => t('Indicator of whether this node type has a title.'),
'field' => array(
'handler' => 'views_handler_field_boolean',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// Title_label field from the node_type table
$data['node_type']['title_label'] = array(
'title' => t('Title label'),
'help' => t('Title label for Node Type.'),
'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',
),
);
// has_body field from the node_type table
$data['node_type']['has_body'] = array(
'title' => t('Has body'),
'help' => t('Indicator of whether this node type has a body.'),
'field' => array(
'handler' => 'views_handler_field_boolean',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// Body_label field from the node_type table
$data['node_type']['body_label'] = array(
'title' => t('Body label'),
'help' => t('Body label for Node Type.'),
'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',
),
);
// min_word_count field from the node_type table
$data['node_type']['min_word_count'] = array(
'title' => t('Min word count'),
'help' => t('Minimum word count for the body of this node type.'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
// Use node_type as a new base table
// by creating a new views type called 'Node type'
// This allows it to be selected as the 'view type'
// when you initially add a new view.
$data['node_type']['table']['base'] = array(
'field' => 'type',
'title' => t('Node type'),
'help' => t("Information relating to each defined node type."),
'weight' => 9,
);
// Note: No relationships to other tables have been added.
// Whenever a relationship was added, Views would refuse to show Node type: type
// as a field that could be added to a view ????
// If a relationship to the node or other tables is required then views should be started
// using the Views Node type rather than the above Views Node type type.
return $data;
}
?>
Hope this helps
Ken
www.ausvalue.com
#2
great! it looks, it works pefectly. it is exactly what i was looking for, Thx it helped o lot
HoKe
#3
Sweet. Great work! Will test this evening.
--
J.
#4
Thanks AusValue
I don't think it is a good idea to have {node_type} fields exposed to views and it certainly shouldn't be a part of this module.
Also what is the point of exposing the next lnid to views?
Can you create a patch that just exposes the lnid?
#5
This patch works well, but for convenience purposes of anybody else, I put it in patch form (attached).
I do not know what to do to make #4's request happen so I leave that to whomever knows how.
FYI, after applying the patch make sure to clear the system cache so that the local node information will appear in the views interface.