4

I'm currently working on a simple LMS in D8 and have already defined the fundamental content types. Among them is a content type called Course, which can be considered "Premium" content, i.e. all users may access those fields that contain more general information, but the users will have to buy a subscription for accessing the full content.

To make that possible without any Premium module (which, at the moment, have not been ported to Drupal 8), I created a simple grid-view and deactivated "SQL rewriting", which works perfectly, so far. For displaying the nodes in this view, I have created a new view mode, called gallery, so that the nodes that are displayed can be styled in a template.
I would like to apply different CSS styles to those nodes in the view that the user may access. I'm not sure if this can be done in the view itself.

Anyway, I'm currently thinking of checking the view permission inside node--course--gallery.html and I would like to add a class to the article tag, if access is permitted.

Unfortunately, I can't seem to find the right function for this. I set it up like this:

{%
  set classes = [
    node.bundle|clean_class,
    node.isPromoted() ? 'is-promoted',
    node.isSticky() ? 'is-sticky',
    not node.isPublished() ? 'is-unpublished',
    view_mode ? view_mode|clean_class,
    user.hasPermission('view nodes') ? 'is-permitted',
    node.access('view') ? 'is-permitted',
    'clearfix',
  ]
%}

Neither user.hasPermission('view nodes') ? 'is-permitted' nor node.access('view') ? 'is-permitted' seem to have any effect.

Do you have any idea?

2
  • I would use roles, assumed you have a premium role, see drupal.stackexchange.com/questions/226314/…
    – 4uk4
    Sep 13, 2018 at 6:31
  • That's a good suggestion and, indeed, I do have a few roles for defining access. However, it's not a single role and access to a node will also depend on the specific node settings. This could easily become a rather complex condition, so I hoped that all these checks can be subsumed by calling node-access() or something similar...
    – Marcus C.
    Sep 13, 2018 at 7:09

2 Answers 2

3

@Berdir, thanks again: Your comment in the last paragraph brought me on the right track. I now solved the problem as follows:

First, I extended hook_preprocess_node() to include

$node = $variables['elements']['#node'];
$check = $node->access('view');
$variables['viewaccess'] = $check;

(Still not sure, why $node = Node::load(1);, as mentioned here, doesn't work, but that's another story.) Then, I simply used

{%
  set classes = [
    node.bundle|clean_class,
    node.isPromoted() ? 'is-promoted',
    node.isSticky() ? 'is-sticky',
    not node.isPublished() ? 'is-unpublished',
    view_mode ? view_mode|clean_class,
    viewaccess ? 'is-permitted',
    'clearfix',
  ]
%}

instead of my original code. Works perfectly. Thanks to all, who posted helpful comments and replies.

3

Not really an answer, more like a suggestion for an alternative suggestion where that problem should exist, at least not exactly like that.

The way I handled similar problems (paywall for newspapers in my case) is to not use the node access system for this. In my case, I did it using logic in the template/preprocess as well as dynamically switching the view mode. If you can handle it on a field-by-field basis, then you can also implement hook_entity_field_access() and deny view access to just those specific fields.

That said, I would expect the method isn't allowed to be called due to the Twig sandbox that only allows is/get method calls, but then I'd kind of expect it to throw an exception.

I guess also that, with my suggestion, you'll need a way to check for something in the template but I would simply do that in hook_preprocess_node() and add it as a boolean variable to $variables.

1
  • Hmm, thanks for the quick reply and the suggestion. It hasn't occurred to me to simply use different view modes, but this indeed seems a feasible solution to me. Will investigate. Also, adding the access check in hook_preprocess_node(), as you mentioned, could maybe directly solve my problem. Not sure, though, if that's really necessary?
    – Marcus C.
    Sep 12, 2018 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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