D
Dean Bateman
Member
First Post
Conversation Starter
31 posts
if I have a homepage that displays 5 slides
can I set each slide to be visible if you are a member of a group?
-slide 1 - group 1
-slide 2 - group 2
-slide 3 - group 1 and 2
etc
currently i display my slides with this code on the home page
{% for child in collection %}
{% include ' slide.html.twig ' with { ' blog ' : page , ' page ' : child , ' truncate ' : true } %}
{% endfor %}
and have 5 subpages slide.md containing
this doesn't work, but not sure if it is possible and how to go about it, any hints would be great
last edited 02/24/23 by Dean Bateman
P
pamtbaau
Grav Forum Moderators
Legend
First Post
Conversation Starter
Well Liked
3127 posts
@dean_007, Grav's permissions does not provide the granularity on a image by image basis. A user either has or has no permission to see the page.
Having said that... Inside your loop, I suspect you can check the group of the user using grav.user.groups and only include the slide when the user belongs to the correct group.
See https://learn.getgrav.org/17/themes/theme-vars#user-object
last edited 02/24/23 by pamtbaau
D
Dean Bateman
Member
First Post
Conversation Starter
31 posts
Ok i can see where you are going with this
So if i were to create a variable like this
a llowedGroups :
- g roup1
- g roup2
var display = false;
{% for child in collection %}
{% if config . plugins . login . enabled and grav . user . username %}
{% for g in allowedGroups %}
{% if g == grav . user . group %}
display =true
{% else %}
display = false
{% endif %}
{% endfor %}
{% endif %}
{% if display == true or allowedGroups is empty %}
{% include ' slide.html.twig ' with { ' blog ' : page , ' page ' : child , ' truncate ' : true } %}
{% endif %}
display = false
{% endfor %}
This was typed on my phone so might not be absolutely correct, but ill give it a go.
Thanks for the idea, ill report back with how i go.
last edited 02/24/23 by Dean Bateman
D
Dean Bateman
Member
First Post
Conversation Starter
31 posts
How I ended up creating the code was to create a home page
{% for child in collection %}
{% include ' slide.html.twig ' with { ' blog ' : page , ' page ' : child , ' truncate ' : true } %}
{% endfor %}
with slide child pages that contain within the front matter the following
allowedGroups:
- project1
then create a user with a group of project1
if the slide group equals a user group it displays the slide else does nothing
{% if config . plugins . login . enabled and grav . user . username %}
{% for slideGroup in page . header . allowedGroups %}
{# <p>slideGroup: {{ slideGroup }} </p> #}
{% for userGroup in grav . user . groups %}
{# <p>userGroup: {{ userGroup }} </p> #}
{% if userGroup == slideGroup %}
{% include ' partials/slide-item.html.twig ' %}
{% else %}
{# <p> FALSE </p> #}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
i also added that if a user is not logged in it will display demo slides instead
these have no allowed groups added to the slide page
{% if config . plugins . login . enabled and not grav . user . username %}
{% if page . header . allowedGroups is empty %}
{% include ' partials/slide-item.html.twig ' %}
{% endif %}
{% endif %}
hope it helps someone, Thanks for the help!
last edited 03/27/23 by Dean Bateman
P
pamtbaau
Grav Forum Moderators
Legend
First Post
Conversation Starter
Well Liked
3127 posts
@dean_007, Maybe I don't exactly understand your problem and solution, but it seems you are creating child pages for the home page solely to add a user group to its frontmatter and show an image based on that user group.
Instead of looping through a collection of child pages containing the user group inside their frontmatter, why not add the user group to the metafile of each image and loop to the images of the home page and read the user group in their metafile?
See Metafiles
last edited 03/27/23 by pamtbaau