Drupal - How to check for entity access when displaying views fields?
Presuming your entity providing module has Views integration already setup, and you're not worried about pagination, you can
invoke hook_views_pre_render()
to iterate over the results and invoke your access callback for each entity in your base table and filter out entries that the user doesn't have access to:
/**
* Implements hook_views_pre_render().
*/
function MYMODULE_views_pre_render(&$view) {
global $user;
// Iterate over View results for our custom entity
if ($view->base_table == 'my_entity_base_table') {
foreach ($view->result as $index => $row) {
// Presuming eid is the entity PK
$results = entity_load('my_entity_machine_name', array($row->eid));
if (!empty($results)) {
$entity = $results[$row->eid];
// If the custom access callback returns FALSE, remove from results.
if (!MYMODULE_my_entity_access_callback('view', $entity, $user)) {
unset($view->result[$index]);
}
}
}
}
}
If pagination is a concern, it's a tougher problem; adjusting the view results break consistent offsetting (e.g. page 1 might return 4 results, page 2 might return 10 results). Plus, the results of SQL query can't adjust for information that is only known by executing PHP.
In those instances, you'll have to adjust your method (e.g. hook_views_query_alter()
if the access callback is DB query based, altering your View pager options, etc.) to accommodate for the access callback manipulating the view.