📜 ⬆️ ⬇️

Views module - API. The basics

Surely, many who work with drupal are familiar with the Views . As Drupaler.ru says, the Views module is a Setup and control for displaying any type of content anywhere on the site , i.e. it allows you to create pages, blocks, replace the contents of the nodes, user pages and more, forming the content of any available fields on the site. But what to do when it is necessary to display information provided by a third-party module, and to which we do not have access from Views?

To make it clear, further on a live example:

On the site I use the PrivateMSG module. It allows users to send private messages to each other. With Views, I assembled a block that displays information about the current user.

Task: Display in the block the number of new messages and the number of all messages in the Inbox folder of the current user.
')
Solution: Write a module that would add the necessary values ​​to the Views constructor.

Unfortunately, there is very little information on the Internet on this subject, and the Views API manual is rather complicated and incomprehensible.

So let's get started.
  1. Create a new module.
    As expected, create a directory, call it the name of our future module. I called it privatemsg_extraviews .
    In the directory create files privatemsg_extraviews.info, privatemsg_extraviews.module, privatemsg_extraviews.views.inc . Next, we need to create another 2 files, but more on that later.
  2. privatemsg_extraviews.info
    This file contains information about our module. It is necessary for the drupal to recognize the module.
    name = privatemsg_extraviews
    description = views Private Messages
    core = 6.x
    package = Mail
    version = "6.x-1.1"


    * This source code was highlighted with Source Code Highlighter .

    This is enough for the drupal to recognize our module.
  3. privatemsg_extraviews.module
    The main file of the module. It contains the entire structure of the module, all the hooks, and generally everything.
    We only need to indicate that the module works with Views.
    <?php

    /**
    * Implementation of hook_views_api().
    */
    function privatemsg_extraviews_views_api() {
    return array(
    'api' => 2, // API
    'path' => drupal_get_path( 'module' , 'privatemsg_extraviews' ), // Views
    );
    }


    * This source code was highlighted with Source Code Highlighter .

  4. privatemsg_extraviews.views.inc
    The most interesting. In this file we write the whole system of working with Views. The module itself will find it, because we specified the directory in the hook above.
    The main hook we need is hook_views_data () . In it, we determine which database table we add, and what information we return.
    <?php
    function privatemsg_extraviews_views_data() {
    // Views
    $data[ 'privatemsg' ][ 'table' ][ 'group' ] = t( 'Private Messages' );
    //
    $data[ 'privatemsg' ][ 'table' ][ 'join' ] = array(
    // users -
    'users' => array(
    //
    'left_field' => 'uid' ,
    'field' => 'uid' ,
    ),
    );
    // count - ,
    $data[ 'privatemsg' ][ 'count' ] = array(
    'title' => t( ' ' ),
    'help' => t( ' ""' ),
    //
    'field' => array(
    //
    'handler' => 'privatemsg_extraviews_handler_field_count' ,
    //
    'click sortable' => TRUE,
    ),
    );
    //
    $data[ 'privatemsg' ][ 'count_new' ] = array(
    'title' => t( ' ' ),
    'help' => t( ' ""' ),
    'field' => array(
    'handler' => 'privatemsg_extraviews_handler_field_count_new' ,
    'click sortable' => TRUE,
    ),
    );
    return $data;
    }


    * This source code was highlighted with Source Code Highlighter .


    Next we need hook_views_handlers () to initialize the field handlers.
    function privatemsg_extraviews_views_handlers() {
    return array(
    //
    'handlers' => array(
    'privatemsg_extraviews_handler_field_count' => array(
    // ,
    'parent' => 'views_handler_field_numeric' ,
    //
    'path' => drupal_get_path( 'module' , 'privatemsg_extraviews' ),
    ),
    //
    'privatemsg_extraviews_handler_field_count_new' => array(
    'parent' => 'views_handler_field_numeric' ,
    'path' => drupal_get_path( 'module' , 'privatemsg_extraviews' ),
    ),
    ),
    );
    }


    * This source code was highlighted with Source Code Highlighter .


    As I said above, we will need to create 2 more files - these are just field handlers. Create the privatemsg_extraviews_handler_field_count.inc and privatemsg_extraviews_handler_field_count_new.inc files in the module directory.
  5. privatemsg_extraviews_handler_field_count.inc
    Handler for the field "number of messages."
    <?php
    // , " "
    class privatemsg_extraviews_handler_field_count extends views_handler_field_numeric {
    //
    function query() {
    //
    $table = $ this ->query->ensure_table( 'pm_index' );
    //
    $sql = "SELECT COUNT(DISTINCT thread_id) FROM {pm_index} p WHERE p.deleted = 0 AND p.uid = users.uid" ;
    //
    $ this ->query->add_field( '' , "($sql)" , 'count' );
    $ this ->field_alias = 'count' ;
    }
    //
    function render($values) {
    $txt = $values->count;
    if ($txt) {
    return $txt;
    }
    else {
    return parent::render($values);
    }
    }
    }

    * This source code was highlighted with Source Code Highlighter .

  6. privatemsg_extraviews_handler_field_count_new.inc
    Handler for the field "Number of new messages." (likewise)
    <?php

    class privatemsg_extraviews_handler_field_count_new extends views_handler_field_numeric {
    function query() {
    $table = $ this ->query->ensure_table( 'pm_index' );
    $sql = "SELECT COUNT(DISTINCT thread_id) FROM {pm_index} p WHERE p.deleted = 0 AND p.is_new = 1 AND p.uid = users.uid" ;
    $ this ->query->add_field( '' , "($sql)" , 'count_new' );
    $ this ->field_alias = 'count_new' ;
    }

    function render($values) {
    $txt = $values->count_new;
    if ($txt) {
    return $txt;
    }
    else {
    return parent::render($values);
    }
    }
    }


    * This source code was highlighted with Source Code Highlighter .



After all done, Views will generate a type request.
SELECT users.uid AS uid,
( SELECT COUNT ( DISTINCT thread_id) FROM pm_index p WHERE p.deleted = 0 AND p.uid = users.uid) AS count ,
( SELECT COUNT ( DISTINCT thread_id) FROM pm_index p WHERE p.deleted = 0 AND p.is_new = 1 AND p.uid = users.uid) AS count_new
FROM users users
WHERE users.uid = 1


* This source code was highlighted with Source Code Highlighter .

and returns the required values.

I hope the instruction will be useful to someone. If there is interest, I can write in more detail about Views, there is still a lot of interesting things: how to create your own settings for the fields, how to create the fields available in filters and sorts, your arguments and links, and much more.

UPD: Transferred to the blog "Drupal". Thanks for the karma.

Source: https://habr.com/ru/post/80663/


All Articles