1

I've had a look through some previous posts and the craft docs and it doesn't look like anyone has done anything similar to what I want.

Essentially I have a 'client area' where a user can log in and just see photos and videos that are set on their profile (added by the admin). This all works great but I require functionality so the user can add a comment to said video or photo. I know solutions such as Disqus have been suggested but I can't see away of making them private and locked down so other guests and members cannot get access to the comments.

Does anyone know of any plugins, or methods through Craft that could be a possible solution or starting point to a solution?

Thanks!

3

Something like this should work, but keep in mind there are lot's of ways to accomplish this. Without knowing your requirements, I'll assume you want an index page of assets, that link to an asset detail page. The detail page would show all comments and provide a form to add new comments.

On your assets index page (i.e. '/clientarea/lightbox'):

{% requireLogin %} 

{# show user profile information (if needed) #}
<div class="user--info">
    {{ currentUser.firstName }} {{ currentUser.lasName }}
</div>

{# loop through images in Assets field #}
<h2>Images</h2>
<div class="images">
    {% for image in currentUser.assetsFieldHandle %}
        <div class="image--details">
            {# display image, title and link #}
            <img src="{{ image.url}}" alt="{{ image.title }}"/><br>
            {{ image.title }}<br>
            <a href="/clientarea/lightbox/media/{{ image.id }}">View image</a>
        </div>
    {% endfor %}
</div>

{# repeat for videos, etc #}

In your asset detail page (i.e. 'clientarea/lightbox/media/123' ). Note: to get this uri, you can either create these folders in your templates folder (i.e. 'clientarea/lightbox/media/_entry.html'), or create a custom route.

{% requireLogin %}

{# fetch image #}
{% set imageId = craft.request.lastSegment %}
{% set image = craft.assets.id(imageId).first %}

{# check whether image exists, otherwise redirect 404 #}
{% if not image %}
    {% redirect 404 %}
{% endif %}

{# check whether they are authorized to view this asset #}
{% set authorized = false %}
{% set authorizedImages = craft.assets.relatedTo(currentUser) %}
{% for authorizedImage in authorizedImages %}
    {% if authorizedImage.id == image.id %}
        {% set authorized = true %}
    {% endif %}
{% endfor %}

{# if not authorized, redirect to 401 #}
{% if not authorized %}
    {% redirect 401 %}
{% endif %}

{# show user profile information (if needed) #}
<div class="user--info">
    {{ currentUser.firstName }} {{ currentUser.lasName }}
</div>

{# display image details #}
<div class="image--details">
    {# display image and title #}
    <img src="{{ image.url}}" alt="{{ image.title }}"/><br>
    {{ image.title }}
</div>

{# fetch comments from the 'clientComments' section that are related to this image #}
<ul class="image--comments">
    {% set comments = craft.entries.section('clientComments').relatedTo(image) %}
    {% for comment in comments %}
        <li>{{ comment.commentTextFieldHandle }}</li>
    {% endfor %}
</ul>

{# display a form to add a new comment #}
<div class="commentform">
    <form method="post" accept-charset="UTF-8">
        {{ getCsrfInput() }}
        <input type="hidden" name="action" value="entries/saveEntry">
        {# reload the current page on successful form submission #}
        <input type="hidden" name="redirect" value="{{ craft.request.path }}"> 
        {# section id for 'clientComments' #}
        <input type="hidden" name="sectionId" value="2"> 
        <input type="hidden" name="enabled" value="1">

        {# hidden field to save the image id #}
        <input type="hidden" name="fields[commentAssetsFieldHandle]" value="{{ image.id }}">

        <label for="comment">Comment</label>
        <textarea id="body" name="fields[commentTextFieldHandle]"></textarea>

        <input type="submit" value="Add comment">
    </form>
</div>

You will likely also need to add some checks for the asset type, in order to determine how to display the asset. But you get the general idea.

You will also need to make sure that they have permission to create entries in the clientComments section (but not view or edit other peoples entries).

And if the image or video is displayed on more than one user's profile, then you will also need to factor in the author when searching for comments. Either that, or also add a related users field to the comments section, and then search for comments related to both image and currentUser.

| improve this answer | |
  • Thanks for getting back to me, I'll have a play about and see if this works for me. Many Thanks! – Andy Feb 16 '15 at 12:00

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.