0

I have a table where I keep some logs, for example, when a user visits a node, I record of the form:

The user 34 visited node 235.

Then I have a Views what show me the following fields:

  • NID
  • Title
  • Body

How to display only the nodes that the current user has visited or not?

EDIT:

Example of my custom table:

------------------------------------------------------
| UID | action                        | IP           |
------------------------------------------------------
| 34  | The user 34 visited node 235. | 200.54.21.24 |
------------------------------------------------------

UID = User ID

  • are you storing data in your record table in string? or it has columns nid, uid?? – Shabir A. Dec 3 '15 at 20:39
  • @Shabir Thank you for you interest, I updated the question. – rpayanm Dec 3 '15 at 20:53
  • You should change your structure a bit to add nid – Shabir A. Dec 3 '15 at 21:01
1

I would do the following to achieve this. In the custom table where you maintain log create a view handler using hook_views_data. Add the following code in your .module file

     function MODULENAME_views_api() {
       return array(
           'api' => '3.0-alpha1',
           'path' => drupal_get_path('module','MODULENAME'). '/views',
       );
    }

Then add a file with the name MODULENAME.views.inc in your module views folder and the following code.

    /**
 * Implements hook_views_data()
 *
 */
function MODULENAME_views_data() {

 $table = array(
    'custom-table-name' => array(  //  Custom table
       'table' => array(
          'group' => 'Custom Tables',      // Groupname
             'base' => array(
               'field' => 'id',  //Primary key
               'title' => '<your table name>',
            )
        ),
     //Description of id
        'id' => array(  
           'title' => t(' Id'),
           'help' => t(' Id field'),
           // is mydons_custom_id field sortable TRUE
           'field' => array(
              'click sortable' => TRUE,  
            ),
      //Filter handler for filtering records by mydons_custom_id     
           'filter' => array(
               'handler' => 'views_handler_filter_numeric'   
            ),  

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

         'user_id' => array(      
           'title' => t('User ID'),
           'help' => t('User ID referring to User for users table'),
           'field' => array(
              'click sortable' => TRUE,
           ),
           'filter' => array(
              'handler' => 'views_handler_filter_string'
           ),

           'sort' => array(
              'handler' => 'views_handler_sort'
           ),
           'relationship' => array(
            'base' => 'users', // The name of the table to join with.
            'base field' => 'uid', // The name of the field on the joined table.
            'relationship field' => 'user_id',
            // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
            'handler' => 'views_handler_relationship',
            'label' => t('User'),
            'title' => t('Referenced USER entity'),
            'help' => t('Add USER Fields'),
          ),

        ),

         'node_id' => array(      
           'title' => t('Node ID'),
           'help' => t('Node ID referring to Node'),
           'field' => array(
              'click sortable' => TRUE,
           ),
           'filter' => array(
              'handler' => 'views_handler_filter_string'
           ),

           'sort' => array(
              'handler' => 'views_handler_sort'
           ),
           'relationship' => array(
            'base' => 'node', // The name of the table to join with.
            'base field' => 'nid', // The name of the field on the joined table.
            'relationship field' => 'node_id',
            // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
            'handler' => 'views_handler_relationship',
            'label' => t('User'),
            'title' => t('Referenced USER entity'),
            'help' => t('Add USER Fields'),
          ),

        ),





      )
    );
    return $table;
}

Clear the cache and go to views page and create another view of your custom table. Add relationship to your node and users and contextual filters to show logged in users node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.