0

I have a custom entity which I list in views. I set access control for them, but when I list in views these entities, all entities shows up, those one too, which the user does not have permission to access.

What do I need to implement to filter out those entities, which the user does not have right to view?

0

After reviewing the list of Views hooks, I think hook_views_pre_render() is the one you can use to filter results before they are rendered.

/**
 * Implements hook_views_pre_render().
 */
function MODULE_views_pre_render(ViewExecutable $view) {
  if (!empty($view->result)) {
    $entities = [
      'your_custom_entity_id',
      'another_entity_id',
    ];

    foreach ($view->result as $key => $result) {
      if (empty($result->_entity)) {
        continue;
      }

      if (!in_array($result->_entity->getEntityTypeId(), $entities)) {
        continue;
      }

      $access = \Drupal::entityTypeManager()
        ->getAccessControlHandler($result->_entity->getEntityTypeId())
        ->access($result->_entity, 'view', NULL, TRUE);

      if (!$access->isAllowed()) {
        unset($view->result[$key]);
      }
    }
  }
}
  • Actually I don't know this will work or not, I've already has a solution. But I think you can have some issue here if you have a pager. – golddragon007 Sep 6 '18 at 8:45
0

Actually my final solution was this: https://www.drupal.org/project/drupal/issues/2997705 which maybe not the best, but it works.

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.