Getting return values from PHP functions in views

Support MB Views Getting return values from PHP functions in viewsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #24758
    MartinMartin
    Participant

    Hi,

    I have a view which I want to make dynamic based on the current user role. I'd like to be able to show the content of the view only if the user role is 'sitter' which is a role I've created. If the role is 'client' they should not see the content.

    I couldn't find a way to get the role in Twig so wrote a function in PHP to call from the Twig. The function works correctly but I can't see a way to use the result of the function in the Twig template.

    Ideally I'd have something like this:

    {% if ( {{mb.check_user_role()}} =1 ) %}
    TWIG FOR SITTER ROLES ONLY
    {% endif %}
    TWIG FOR CLIENT ROLES
    

    This approach doesn't seem to make use of any return value from the PHP function.

    I'm assuming that return functions aren't supported so is there another way to do what I'm trying to achieve?

    Thanks

    #24759
    Long NguyenLong Nguyen
    Moderator

    Hi Martin,

    There are two problems with your code.

    • Use a double couple of curly bracket {{ }} means to echo or display a value.
    • The comparison operator is == or strict compare ===, not = only.

    The code should be {% if ( mb.check_user_role() == 'user-role' ) %}

    #24771
    MartinMartin
    Participant

    Perfect. Thanks for that. It works exactly as wanted now (and I can be more detailed over what I'm doing with it too 😉 ).

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.