Drupal developers sometimes find themselves trying to handle the tasks that require creating a custom table in the database so as to later interact with this table through your own requests. Inherent to such cases is the requirement that multiple requests be issued, and that can happen to be quite exhausting. All you have to do to avoid the latter inconvenience is to describe the table for community`s favorite module Views. Here, in this article we will take a look at how it can be done.
First of all, we create a new table in the database with the use of hook_schema():
/** * Implements hook_schema(). */ function example_module_schema() { $schema['example_table'] = array( 'fields' => array( 'nid' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0, 'description' => 'Node {node}.nid.', ), 'uid' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => 0, 'description' => 'User {users}.uid.', ), 'plai_text_field' => array( 'description' => 'Example textfield.', 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', ), 'timestamp_field' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => 'Some timestamp.', ), ), 'primary key' => array('nid'), ); return $schema; }
In the table there should be written down the node's ID, user’s uid, any node/user relevant text fragment (a review, comment) and the timestamp.
Further on, an announcement will be made that the views API will be used in this module:
/** * Implements hook_views_api(). */ function example_module_views_api() { return array( 'api' => 3, 'path' => drupal_get_path('module', 'example_module'), ); }