2

I am displaying a list of users with entries in the Design section using a Route. I am using this code for that and it seems to be working to display the correct users:

{# Grab all the Design Entries #}
{% set designs = craft.entries.section('designs').find() %}

{# Group by the authorId #}
{% set entriesByAuthor = designs|group('authorId') %}

{# Grab all of the users with the userIds in entriesByAuthor #}
{% set designers = craft.users.id(entriesByAuthor|keys).group('members') %}

Although the issues I am having is trying to display data from fields from the entries by these users. For example, entries in the Design section also have images associated with them. I am trying to display the first image from the 'screenshots' field from the Designs section:

{% for assets in designers %}
    {% for image in assets.screenshots %}

        {% if image %}
            <a href="{{ user.userAgencyUrl }}"><img src="{{ image.url }}" border="0" /></a>
        {% endif %}         

    {% endfor %}
{% endfor %}

This does not give an error, but does not display anything for the image. I also tried adding .first() to this to get the first image from each entry, but that resulted in an error.

Any help will be appreciated! I have searched thoroughly for information about displaying entry fields from a user list (instead of just user fields) and I have not been able to find this information. (I also tried several other methods, such as relatedTo.)

Thanks!

| improve this question | | | | |
1

You already have the right array of Design Entries grouped by your Designer's user IDs with this line:

{% set entriesByAuthor = designs|group('authorId') %}

Now all you have to do is to loop through this entriesByAuthor array. Use this syntax to iterate over Keys and Values:

{% for key, value in entriesByAuthor %}
    {{ key }}: {{ value }}
{% endfor %}

Within the loop key is the authorId you grouped by and value is an array of EntryModels that share the same value for the authorId property. To list the Design entries you have to nest another for-loop and iterate over that array of EntryModels.

To retrieve other Designer data besides the ID you first need to grab the UserModel. Create a new ElementCriteriaModel with the authorId as a parameter and use the first() function to get the model.

{# Grab all the Design Entries #}
{% set allDesigns = craft.entries.section('designs').limit(null).find() %}

{# Group by the authorId #}
{% set allDesignsByAuthor = allDesigns|group('authorId') %}

{# Loop through all Design Entries grouped by the authorId #}
{% for authorId, designsByAuthor in allDesignsByAuthor %}

    {# Grab the Designer's UserModel #}
    {% set designer = craft.users.id(authorId).first() %}

    <h2>{{ designer.name }}</h2>

    {# List the Designer's Design Entries #}
    {% for design in designsByAuthor %}

        <h3>{{ design.title }}</h3>

        {# List the Screenshots #}
        {% for image in design.screenshots %}

            <a href="{{ designer.userAgencyUrl }}">
                <img src="{{ image.url }}" alt="{{ image.title }}" />
            </a>

        {% endfor %}

    {% endfor %}

{% endfor %}

.

Edit:

Only now I got it, why you did this line to grab the UserModels with an ID of allDesignsByAuthorId|keys. This is in fact a good idea if you want list the users in another sort order than the user ID. Because I don't think you can do that with the approach I posted above.

Here's how you proceed after you fetched the Designers with a sort order of your desire. Loop through the array of Designers and create a new ElementCriteriaModel for each designer to grab the Designs using the Designer's user ID as the value for the authorId property.

{# Grab all the Design Entries #}
{% set allDesigns = craft.entries.section('designs').limit(null).find() %}

{# Group by the authorId #}
{% set allDesignsByAuthorId = allDesigns|group('authorId') %}

{# Grab all of the users with the userIds in allDesignsByAuthorId #}
{% set designers = craft.users.id(allDesignsByAuthorId|keys).order('lastName') %}

{# Loop through the array of Designers #}
{% for designer in designers %}

    <h2>{{ designer.name }}</h2>

    {# Grab all of the Design Entries created by this Designer #}
    {% set designs = craft.entries.section('designs').authorId(designer.id).find() %}

    {# List the entries #}
    {% for design in designs %}

        <h3>{{ design.title }}</h3>

        {# List the Screenshots #}
        {% for image in design.screenshots %}

            <a href="{{ designer.userAgencyUrl }}">
                <img src="{{ image.url }}" alt="{{ image.title }}" />
            </a>

        {% endfor %}

    {% endfor %}

{% endfor %}

.

2nd Edit:

You only need to show one image for each Designer? Do it like so:

{# Grab all the Design Entries #}
{% set allDesigns = craft.entries.section('designs').limit(null).find() %}

{# Group by the authorId #}
{% set allDesignsByAuthorId = allDesigns|group('authorId') %}

{# Grab all of the users with the userIds in allDesignsByAuthorId #}
{% set designers = craft.users.id(allDesignsByAuthorId|keys).order('lastName') %}

{# Loop through the array of Designers #}
{% for designer in designers %}

    {# Get the total number of Design Entries created by this Designer #}
    {% set totalDesigns = craft.entries.section('designs').authorId(designer.id).total() %}

    {# Grab the latest Design Entry created by this Designer #}
    {% set design = craft.entries.section('designs').authorId(designer.id).first() %}

    {# Grab the first screenshot image of that entry #}
    {% set image = design.screenshots.first() %}

    {# Output #}
    <h2>{{ designer.name }}</h2>

    <p>{{ totalDesigns }}</p>

    <a href="{{ designer.userAgencyUrl }}">
        <img src="{{ image.url }}" alt="{{ image.title }}" />
    </a>

{% endfor %}
| improve this answer | | | | |
  • Thank you very much for your detailed answer. I appreciate it! :) This info will definitely help when I want to create a list of design entries. Although for this, I am trying to list users (designers) who have posted design entries, and then for each user, display data from those entries. I will be displaying the total design entries each user has posted (which I have not coded yet) and I will pull the first image loaded from the image asset Design entry field 'screenshots' to display with each user listing on this Route page - which will list/paginate the users. I hope that makes sense! :) – Angela Sep 1 '14 at 12:26
  • @Angela that's exactly what you get with this, a list of users who have posted design entries (→ {% for designer in designers %}) and then you loop through all the design entries each of these users have posted (→ {% for design in designs %}) and output the images (→ {% for image in design.screenshots %}). If you only want to show the first image of each design entry, you can replace the for-loop and use the first() function on the ElementCriteriaModel. – carlcs Sep 1 '14 at 12:34
  • @Angela what do you mean with "I will be displaying the total design entries"? You need only the number of how many design entries that user has posted? And if I got you right, you want just one image to show up for each user. Which image should be shown if the user posted several entries, the first image of the latest entry? – carlcs Sep 1 '14 at 12:39
  • Ah - thanks for the clarification! I was viewing that wrong. :) I will be displaying the total number of Design entries the user has posted. Yes - I will be pulling the 'first' image in the screenshots field from the 'latest' entry by that designer. Thanks for the help! – Angela Sep 1 '14 at 12:57
  • Glad my answer works for you, @Angela. I just added the code to get the total number of entries. – carlcs Sep 1 '14 at 13:27

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.