<p>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. </p>

<ul>
<li>An advantage of this approach is that you could easily add more "statuses", such as primary, secondary. </li>
<li>A disadvantage would be that the interface for editing these entries would be more complex /  little less straightforward. </li>
</ul>

CC BY-SA 3.0
2017-08-12T23:13:05.227Z
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
CC BY-SA 3.0
2017-08-12T23:56:31.287Z
Just added an update with some code to reference!
wolliemilliams
CC BY-SA 3.0
2017-08-13T11:31:27.720Z
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
CC BY-SA 3.0
2017-08-14T15:30:40.850Z
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
CC BY-SA 3.0
2017-08-14T16:36:31.783Z
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
CC BY-SA 3.0
2017-08-15T21:13:43.550Z
If I'm following this correctly @Youniteus is correct in his last comment. `block.teamMember.id` is going to return the ID of the field. `block.teamMember.first().id` will return the ID of the first selected team member.
Brad Bell
CC BY-SA 3.0
2017-08-15T21:23:28.087Z
Thanks Brad and Youniteus! I tried adding .first().id to the if statement and it still came up blank. I guess I'll move forward with two separate related entries fields - primary authors and all other authors. Your time and efforts are much appreciated!
wolliemilliams
CC BY-SA 3.0
2017-08-11T20:43:37.533Z
2017-08-11T20:43:37.533Z
Youniteus
<p>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).</p>

<p>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. </p>

<p>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.</p>

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

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

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

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

<p>This code is working perfectly fine, btw.</p>

<p>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.</p>

<p>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.</p>

<p>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 <code>&lt;span&gt;primary author&lt;/span&gt;</code> 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!</p>

<p><strong>UPDATE:</strong></p>

<p>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:</p>

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

<p>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: </p>

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

<p>I've tried the following to get the light switch's value to no avail:</p>

<pre><code>{% for block in relatedReports %}
    {% if block.primaryAuthor %}
        &lt;p&gt;Primary Author&lt;/p&gt;
    {% else %}
    {% endif %}
{% endfor %}
</code></pre>

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

<templating><matrix><relations>
Specify ‘primary’ entries in list of related entries
# Specify ‘primary’ entries in list of related entries

(Asked by: username_0 on 2017-08-11)

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 %}
    
        FEATURED PROJECTS
        {% for project in featuredProjects %}
            
                {% if project.projectPhoto | length %}
                
                {% endif %}
                
                    {{ project.title }}
                    {{ project.intro|striptags|slice(0,120)|default('Read more') }}...
                
            
        {% endfor %}
    
{% 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 %}
    
        
        
            {{ teamMember.title }}
            {{ teamMember.teamMemberTitle }}
            VIEW TEAM MEMBER 
        
    
{% 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 primary author 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 %}
  
    
    
        {{ entry.title }}
        {{ entry.teamMemberTitle }}
        VIEW TEAM MEMBER 
        {% if block.primaryAuthor %}
            Primary Author
        {% else %}
        {% endif %}
    
  
  {% 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 %}
    {{ entry.title }}
{% endfor %}

I've tried the following to get the light switch's value to no avail:
{% for block in relatedReports %}
    {% if block.primaryAuthor %}
        Primary Author
    {% 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)

**Tags:** templating, matrix, relations

---

## 1 answers

### Answer by username_1 on 2017-08-11. Score: 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.

> **Comments:**
> **username_0**: 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?  
> **username_0**: Just added an update with some code to reference!  
> **username_1**: 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.  
> **username_0**: 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.  
> **username_1**: 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')`?  
> **username_2**: If I'm following this correctly @Youniteus is correct in his last comment. `block.teamMember.id` is going to return the ID of the field. `block.teamMember.first().id` will return the ID of the first selected team member.  
> **username_0**: Thanks Brad and Youniteus! I tried adding .first().id to the if statement and it still came up blank. I guess I'll move forward with two separate related entries fields - primary authors and all other authors. Your time and efforts are much appreciated!  

---

