-1

I have a view of content type documents wherein I want the logged in user to request access for each document and after it approved or rejected the status next to that list of documents should show approved or rejected respectively.

I created a custom module to capture the node id, the user id of the requester and the status of the request.

I then created a mymodule.view.inc file to add the fields of this custom module using handlers. This is the below code:

<?php

function mymodule_views_data() {
    $data = array();

    $data['mymodule']['table']['group'] = t('Document Approval');

    $data['mymodule']['table']['join']['node'] = array(
        'left_field' => 'nid',
        'field' => 'nid',
    );

    $data['mymodule']['nid'] = array(
        'title' => t('Nid'),
        'help' => t('The node id of the approval node'),
        'field' => array(
            'handler' => 'views_handler_field_node',
            'click sortable' => TRUE,
        ),
        'argument' => array(
            'handler' =>'views_handler_argument_node_nid',
            'name field' => 'title',
            'numeric' => TRUE,
            'validate type' => 'nid',
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_numeric',
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
        'relationship' => array(
            'label' => t('Node'),
            'base' => 'node',
            'base field' => 'nid',
        ),      
    );

    $data['mymodule']['uid'] = array(
        'title' => t('Uid'),
        'help' => t('The user id of the user requested access'),
        'field' => array(
            'handler' => 'views_handler_field_user',
            'click sortable' => TRUE,
        ),
        'argument' => array(
            'handler' =>'views_handler_argument_user_uid',
            'name field' => 'title',
            'numeric' => TRUE,
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_numeric',
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
        'relationship' => array(
            'label' => t('User'),
            'base' => 'users',
            'base field' => 'uid',
        ),      
    );

    $data['mymodule']['status'] = array(
        'title' => t('Status'),
        'help' => t('The status of the approval node'),
        'field' => array(
            'handler' => 'views_handler_field_node',
            'click sortable' => TRUE,
        ),
        'argument' => array(
            'handler' =>'views_handler_argument_node_nid',
            'name field' => 'title',
            'numeric' => TRUE,
            'validate type' => 'nid',
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_numeric',
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
        /*'relationship' => array(
            'label' => t('Node'),
            'base' => 'node',
            'base field' => 'nid',
        ),*/        
    );

    return $data;
}

I am able to see these fields in my view. I am confused as to how do I further use this in my views to get what I want. I want the user to see the status in front of each node.He should see all nodes. And for the user whose no record is found in my custom table he should also see all nodes with "request access" status.

I tried creating a relationship using nid of my custom table and adding a field status with requiring relationship, but I get duplicate results.

I would also like to know if there is any better way of handling this use case.

0

Try looking into Permissions by Term and similar modules that override how access is handled. That module can take care of hiding the nodes from users based on taxonomy terms. You can create a Vocabulary with access terms and assign nodes with them, and allow different user roles to view nodes based on that taxonomy.

Then in the Views listing all the nodes disable the node access checks:

 Disable access checks

Do not apply node_access checks to this query. Selecting this option omits that tag from the alterable query.

under Query settings.

This will list all the nodes regardless of the access rules, but it applies to the Views only. Once the user clicks through to the node the access check will kick in and they will/will not be able to view it based on the taxonomy access or other node access you set up.

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.