1

I have a custom module where in a user requests access to view a node. I have a view page which currently lists all nodes and has a button "Request Access" to it. When the user clicks on it, an entry for the same is captured in my custom table for that node id. Then the admin approves or rejects it. If it is approved i want the user to view "Approved" status for that node id and for others it should show "Request Access". The name of my custom table is access_node_custom. I tried using the below code (with some static data):

function access_node_custom_views_pre_render(&$view) {
    if ($view->name !== 'access_node_custom'  || $view->current_display !== 'page_access_node_custom') {
        return;
    }

    $results = &$view->result;
    foreach ($results as $key => $result) {
        if($result->nid == 656){
            $view->field['request_access']->options['exclude'] = TRUE; //Hide request access since this is already approved
        }
    }
}

The problem with this is that it hides for every row on that page. I am fairly new to drupal so I don't know how can i do this. Is it possible to use relationship? or query alter?

Thank you.

1

$view->field['request_access']->options['exclude'] = TRUE;

Exclude will always exclude the field for every single row.

What you need to do is set it to null or empty ''

$result->field_request_access = null; // hide request

A possible downfall is that a user could guess, or have someone tell them the direct link, and still be able to see /node/656.

Thus; in addition, to what you're already doing, should also use hook_node_access

/*
 * Implements hook_node_access()
 */
function access_node_custom_node_access($node, $op, $account) {
  if($node->nid == 656 && $op == 'view'){
    // some db_query here
    if ($something_is_true) {
      return NODE_ACCESS_ALLOW;
    }
    else {
      return NODE_ACCESS_DENY;
    }
  }
}

If the person does not have access, the above will automatically hide any "view" link in Views.

  • Hello, My objective is to show a custom text based on the value of a field of node. – Estaa Shah Aug 3 at 19:33
  • @EstaaShah gotcha, updated my answer. – No Sssweat Aug 5 at 17:41

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.