0

I have a site that shows upcoming football matches in a table, and I would like to include the clubs' logos next to each club name.

I have the following sections and entry types, both generated using an API and feedme to create entries

  • Teams section (includes fields for teamIDs and logos)

  • Matches section (includes fields for home team & home team ID, away team & away team ID, match time and location, but does not include logos)

The team ids and home/away team IDs match.

To display logos, I imagine the process should go something like this:

  • for each hometeam and awayteam in each row of the match table, identify the team ID
  • Use the ID to find the related teams in the teams section,
  • output the asset (logo) related teams logo in the match table.

This feels like a relatedTo / reverse relations problem, where i can say - in twig - something like:

{% if homeTeamId or awayTeamId in the fixtures section is the same as a Teamid field in the Team Section, then output that related teams's logo. %}

Can anyone suggest the most efficient way to structure this in Twig?

enter image description here enter image description here enter image description here

2
  • Is the home team ID and away team ID fields just text fields? Or are they entry fields? Seems like you could just have an away and home team field in the matches section and just get the logo of the select team from those fields. Dec 11, 2018 at 13:19
  • Hi Indigo, Yes - the hometeam ID and awayteam ID are just text fields, and are already present in the matches section (sorry that was unclear). How would I get the logo of the selected team for this field? (note, the logo of the team is not stored in the match entry, only in the club entry. the reason is that the match data is imported from an API that doesn't include logos.) Dec 13, 2018 at 11:42

1 Answer 1

1

The best way is to change home team ID and away team ID to entry fields that select the club. If you are using a plugin like Feed Me to bring in the matches, and you have all of the club IDs already entered in the club entries, you should be able to import matches and use the home and away IDs and match them to the appropriate Club in Feed Me.

Once that is done, you won't have to loop through all of the Clubs, just pull the one from each of the matches home and away fields like so:

{% for team in fixture.homeTeamId.limit(1).all() %}
    {% for logo in team.clubLogo.limit(1).all() %}
        <img src="{{ logo.getUrl() }}" />
    {% endfor %}
{% endfor %}

If your API isn't brought in with Feed Me (or akin) and you can't change the home/away fields to entry selectable fields, you can select a team by matching its ID field to the given ID field like so:

{% set awayId = fixture.awayTeamId %}
{% set awayTeam = craft.entries.section('clubs').search('clubId:'~awayId).limit(1) %}
  {% for team in awayTeam %}
    ...
1
  • A perfect solution! Thank you so much Indigo - this works a treat. Dec 14, 2018 at 13:47

Your Answer

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

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