1

I’m currently redesigning a website for a scientific research institute that conducts research projects in multiple different areas, produces reports on those projects once they are complete, and then occasionally has that work published in scientific journals and periodicals. The institute has a list of ‘technical team members’ (scientists) that need to relate to these projects, reports and publications in some way. More specifically, I need to be able to specify “primary authors” (for reports and publications) and “primary investigators” (for projects).

A bit about how I have it set up: I have four structures for each section: technical team, projects, reports, publications with entry types for indexes and details. I used structures so I could specify hierarchy in order to get URLs like “technical team/team member name”, “projects/project name”, etc. I used entry types so the client could edit the “overview content” on index pages (something not achievable with plain old _entry.html and index.html to my understanding). Also, this site is very big and this set up just worked for my content.

So, for example, this is how I am currently relating team members to projects. I have a ‘Featured projects’ entries field in my “Team Member” entry where the client can choose projects that the team member has been a part of. ’Featured projects’ are a little more in depth and have more information than reports and publications, but the set up is pretty much the same for reports and publications - just with fewer fields.

In templates/technical-team/_types/teamMember.html to get featured projects in a sidebar:

{% set featuredProjects = craft.entries({
  section: 'projects',
  type: 'featuredProject',
}).relatedTo(entry) %}
{% if featuredProjects|length %}
    <div class="related-block">
        <h4>FEATURED PROJECTS</h4>
        {% for project in featuredProjects %}
            <div class="related-item">
                {% if project.projectPhoto | length %}
                <img src="{{ project.projectPhoto.first.url('relatedPhoto') }}" />
                {% endif %}
                <div>
                    <a href="{{ project.url }}">{{ project.title }}</a>
                    <p>{{ project.intro|striptags|slice(0,120)|default('Read more') }}...</p>
                </div>
            </div>
        {% endfor %}
    </div>
{% endif %}

And then in templates/projects/_types/featuredProject.html to get related team members in a sidebar:

{% set teamMembers = craft.entries.section('technicalTeam').relatedTo(entry) %}
{% for teamMember in teamMembers %}
    <div class="related-item">
        <img src="{{ teamMember.bioPhoto.first.url }}" width="80" />
        <div>
            <a href="{{ teamMember.url }}">{{ teamMember.title }}</a>
            <p>{{ teamMember.teamMemberTitle }}</p>
            <a href="{{ teamMember.url }}" class="view-more">VIEW TEAM MEMBER <i class="fa fa-angle-right" aria-hidden="true"></i></a>
        </div>
    </div>
{% endfor %}

This code is working perfectly fine, btw.

Now for my question: what would be the best way to denote that a ‘related team member’ is the “primary investigator” (for featured projects) and the “primary author” (for reports and publications)? Side note: there can be more than one primary investigator and primary author for these projects, reports and publications.

What I would like to have would be, for example, on the report/publication “detail” it would pull the team member’s photo, name, and title (as it already does) and then, if they were the primary author, the words “primary author” would appear below their info. In the same way, on the team member detail page, it would list the reports or publications that are related to that team member and then, if they were the primary author, the words “primary author” would appear below the project info… you get the idea.

Is my only option to have them as separated entries fields? So, within a report entry I would have a “primary authors” tab with the ability to choose team members, and an “other authors” tab to choose all other team members involved? Perhaps, then I would add a <span>primary author</span> to the loop for primary authors? I feel like there has to be some sort of set up with categories, tags, or light switch fields that I can take advantage of but since this is my first time using Craft I’m not quite sure what that is. Appreciate any help/advice anyone can give!

UPDATE:

As per @Youniteus answer below, I have added a matrix field 'authors' to the report detail entry type which has two fields: 1. an entries field where the user can select entries from the 'technical team' section and 2. a light switch field that, when ticked on, displays the words "primary author" under that team member's bio. It's working great, here is my code:

{% for block in entry.authors %}
  {% set relatedTeamMembers = craft.entries.section('technicalTeam').relatedTo(block) %}
  {% for entry in relatedTeamMembers %}
  <div class="related-item">
    <img src="{{ entry.bioPhoto.first.url }}" width="80" />
    <div>
        <a href="{{ entry.url }}">{{ entry.title }}</a>
        <p>{{ entry.teamMemberTitle }}</p>
        <a href="{{ entry.url }}" class="view-more">VIEW TEAM MEMBER <i class="fa fa-angle-right" aria-hidden="true"></i></a>
        {% if block.primaryAuthor %}
            <p>Primary Author</p>
        {% else %}
        {% endif %}
    </div>
  </div>
  {% endfor %}
{% endfor %}

but on my team member's template (/templates/technical-team/_types/teamMember.html), I am unable to get the light switch's value. Only the related report's entry title and url. Here's how I'm getting that:

{% set relatedReports = craft.entries.section('reports').relatedTo({
    targetElement: entry,
    field: 'authors',
}) %}
{% for entry in relatedReports %}
    <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
{% endfor %}

I've tried the following to get the light switch's value to no avail:

{% for block in relatedReports %}
    {% if block.primaryAuthor %}
        <p>Primary Author</p>
    {% else %}
    {% endif %}
{% endfor %}

Is it even possible to get a different field from a matrix block that contains a related entries field from that related entry? (hope that's not too confusing)

1

Another option would be te create a matrix field that has a block that consists of both an entry field (with limit 1) and some other field to indicate whether the selected entry is a primary one.

  • An advantage of this approach is that you could easily add more "statuses", such as primary, secondary.
  • A disadvantage would be that the interface for editing these entries would be more complex / little less straightforward.
  • Thank you for your answer! I set up a matrix field 'authors' on the report entry with an entries field (to select team members) and a light switch field that, when checked, displays "primary author" under that team member's name. It's working beautifully on the report's detail page, however, I can't seem to get the light switch field's value to appear on the related team member's page, only that related report's url and title. Is it possible to get "other" fields in the matrix block? Or only those related entries' properties? – wolliemilliams Aug 12 '17 at 23:13
  • Just added an update with some code to reference! – wolliemilliams Aug 12 '17 at 23:56
  • Well, in the {% for entry in relatedReports %} block you could add another loop, to loop trough the matrix block with authors. If their id matches the id of the author which is displayed on the page and the lightswitch is enabled, then the author would be a primary author. – Youniteus Aug 13 '17 at 11:31
  • I am unable to get this solution to work. I've got {% for block in relatedReports.authors.type('relatedTeamMember') %} inside {% for item in relatedReports %} (changing entry to item to compare the id's) with the following if statement {% if block.teamMember.id == entry.id and block.primaryAuthor('1') %} some variations print 'primary author' on all entries, this currently prints nothing. Let me know if you have any ideas. Feel like I've tried everything at this point. – wolliemilliams Aug 14 '17 at 15:30
  • That looks a lot like how I would do it. Maybe you need block.teamMember.first.id instead of block.teamMember.id because entry fields could have multiple items? Also, I think that block.primaryAuthor is the same as block.primaryAuthor('1')? – Youniteus Aug 14 '17 at 16:36

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.