1

Any help would be greatly appreciated! I hope I explained my situation in enough detail to get the help that is needed.

Currently I have two channels that need to share information. This is going from one channel to an other.

Channel One is called Resellers This information needs to be feed into my other channel called Products. Reseller contains info about various resellers, places to purchase a product. In this channel I have a field for Category. The category is actually a Country name. The rest of the fields are plain text.

Channel 2 is called Products Products has various fields but I am interested in doing is pulling in info from Resellers and displaying it. I am able to pull in everything from Resellers except for the Category name.

In Products I have a matrix field that contains an entry field and a plain text field. The entry field pulls in the information about each reseller. The plain text field allows me to override a field from Reseller to display per product information. I got this part working without an issue.

What I am trying to create is the following structure: Country name then under the country list out each reseller alphabetically. You will see Country Name A, A means nothing it just shows alphabetical listing.

  • Country Name A

    • Reseller A
    • Reseller T
  • Country Name C

    • Reseller D
    • Reseller H
    • Reseller R
    • Reseller T

Below I have listed out my field handle names that are relevant to this issue.

From Resellers

  • rdCountry - field where category is selected from

I am able to pull in all other fields easily that are part of Resellers.

From Products

  • Matrix field - chooseResellers
    • Entries field - chooseReseller
    • Plain text field - customProductLink

Again I am able to display each reseller just fine. What I am having issues with is pulling in the Category/Country name. Once I pull in the country name I might need help with setting up my loop to display everything.

Thanks for your time and help, it is greatly appreciated!

EDIT: So the below code seems to solve my issue listed above. Now I just gotta loop through everything correctly.

{% for entry in craft.entries.slug(entrySlug).find() %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller %}
            <h3>{{ reseller.rdCountry.first() }}</h3>
        {% endfor %}
    {% endfor %}{# /matrix #}
{% endfor %}{# /for product #}

EDIT TWO - the code solves part of the problem but I do not think will allow me to loop through everything as I was expecting. Help is appreciated!

2 Answers 2

2

To achieve structure:

  • Country Name A

    • Reseller C
  • Country Name C

    • Reseller B
    • Reseller D

With configuration:

Category Countries

Channel Resellers:

  • rdCountry (Country Categories) field

Channel Products:

  • chooseResellers (Matrix) field
    • block
      • chooseReseller (Reseller Entries) field

You gonna need code like this:

{% for entry in craft.entries.slug(entrySlug).find() %}
    {% set countries = {} %}
    {% for block in entry.chooseResellers %}
        {% for reseller in block.chooseReseller.with(['rdCountry']) %}
            {% set countryName = reseller.rdCountry[0].title %}

            {% if countries[countryName] is defined %}
                {% set temp = countries[countryName] | merge([reseller]) %}
                {% set countries = countries | merge({(countryName): temp}) %}
            {% else %}
                {% set countries = countries | merge({(countryName): [reseller]}) %}
            {% endif %}

        {% endfor %}
    {% endfor %} 

    {% for countryName, resellers in countries %}
        <h3>{{ countryName }}</h3>
        {% for reseller in resellers %}
            <p>{{ reseller.title }}</p>
        {% endfor %}
    {% endfor %}
{% endfor %}

Because of the current section/field/relation configuration we need to use countries temporary array to store Country => Reseller structure.

.with(['rdCountry']) is what you call eager loading. It allows you to reduce sql queries, speed up the whole process. With eager loaded entries field you replace find() with index number.

countries looks like this at the end:

{  
   "Country Name A":[  
      {  
         "title":"Reseller C"
      }
   ],
   "Country Name C":[  
      {  
         "title":"Reseller B"
      },
      {  
         "title":"Reseller D"
      }
   ]
}
2
  • Wow thank you so much for the help! You really got me a lot further then I thought someone would be able to help me. I would like to apologize for my delay in getting back to you but I was never notified that someone replied to my posting. I hope your week is going well, mine is now that you helped out!
    – johnHoysa
    Commented Nov 23, 2016 at 20:11
  • One last question, how would I order the out put by title?
    – johnHoysa
    Commented Nov 23, 2016 at 21:13
0

I was able to get help from a co-worker on this issue. Below is the code used to get Countries and Resellers to sort correctly. Krzysztof Boduch's answer was pretty solid and allowed for a great starting point.

{% for entry in craft.entries.slug(entrySlug).find() %}
{% set resellersByCountry = {} %}
{% for block in entry.chooseResellers %}
    {% for reseller in block.chooseReseller.with(['rdCountry']) %}
        {% set countryName = reseller.rdCountry[0].title %}

        {% if resellersByCountry[countryName] is defined %}
            {% set temp = resellersByCountry[countryName] %}
            {% set temp = resellersByCountry[countryName] | merge([reseller]) %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): temp}) %}
        {% else %}
            {% set resellersByCountry = resellersByCountry | merge({(countryName): [reseller]}) %}
        {% endif %}

    {% endfor %}
{% endfor %} 

{# United States #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey == "United States" %}
    <h3>{{ countryKey }}</h3>
    {% for reseller in resellersByCountry[countryKey] | sort %}
        <p>{{ reseller.title }}</p>
    {% endfor %}
    {% endif %}
{% endfor %}

{# International #}
{% for countryKey in resellersByCountry | keys | sort %}
    {% if countryKey != "United States" %}
    <h3>{{ countryKey }}</h3>
    {% for reseller in resellersByCountry[countryKey] | sort %}
        <p>{{ reseller.title }}</p>
    {% endfor %}
{% endif %}
{% endfor %}

{% endfor %}

Your Answer

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

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