<p>I don't think contains will behave like you expect (since <code>booksField</code> will return an array of objects and your checking that against an integer <code>id</code>).</p>

<p>Not the prettiest, but something like this should work:</p>

<pre><code>{{ entry.title }}

{% set read = false %}
{% for book in currentUser.booksField %}
    {% if book.id == entry.id %}
        {% set read = true %}
    {% endif %}
{% endfor %}

{% if read %}
    I have read this book
{% else %}
    I haven't read this book
{% endif %}
</code></pre>

<p>And you could make it more performant (especially if you have large numbers of books) by using <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow noreferrer">https://github.com/marionnewlevant/craft-mnbreakandcontinue</a>, which adds a <code>{% break %}</code> Twig tag.</p>

CC BY-SA 3.0
2017-06-07T23:04:39.640Z
2017-06-07T23:04:39.640Z
Brad Bell
<p>My initial though was that you do not need to not put the "entry.id" in quotes because it's a variable reference, but the <code>contains()</code> method appear to not work as expected here even when this was corrected.</p>

<p>The booksField returns an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a>, so you should be able to fetch the list of IDs as a plain array and then use Twig's <code>in</code> "<a href="https://twig.sensiolabs.org/doc/2.x/templates.html#containment-operator" rel="nofollow noreferrer">containment operator</a>" to search it:</p>

<pre><code>{{ entry.title }}

{% if entry.id in currentUser.booksField.ids() %}
  I have read this book
{% else %}
  I haven't read this book
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-06-07T22:28:17.060Z
Hi Isaac, thanks for this. I removed the quotes around ID but it still doesn't work I'm afraid. I'm not even sure if the 'contains' conditional is the right way of doing this, with the Entries field being an array. Any other ideas are welcome, thanks.
Stephen
CC BY-SA 3.0
2017-06-08T15:24:15.523Z
@Stephen it looks like an array but it's actually a ElementCriteriaModel. I will update my answer with some additional code that I think should do what you want.
Isaac Raway
CC BY-SA 3.0
2017-06-07T21:57:10.130Z
2017-06-08T15:27:33.227Z
2017-06-08T15:27:33.227Z
Isaac Raway
<p>I've got a user profile field named Books. This is an Entries field that contains entries from the Books channel. The idea is that users will save books to their their profile to show they have read them.</p>

<p>I've also got a listing page which outputs all the books that are available.</p>

<p>I'd like to add a conditional that checks to see if the entry ID for a book is contained within the Books entries field in the current user's profile.</p>

<p>I've tried the following but it doesn't work. It outputs as true for every entry, even those that aren't saved to the user's profile. Any help would be appreciated, thanks.</p>

<pre><code>{{ entry.title }}

{% if currentUser.booksField.contains("entry.id") %}
  I have read this book
{% else %}
  I haven't read this book
{% endif %}
</code></pre>

<conditional><user-profile><entries-field>
Conditional to see if an Entries field in a user profile contains a certain entry ID
# Conditional to see if an Entries field in a user profile contains a certain entry ID

(Asked by: username_0 on 2017-06-07)

I've got a user profile field named Books. This is an Entries field that contains entries from the Books channel. The idea is that users will save books to their their profile to show they have read them.
I've also got a listing page which outputs all the books that are available.
I'd like to add a conditional that checks to see if the entry ID for a book is contained within the Books entries field in the current user's profile.
I've tried the following but it doesn't work. It outputs as true for every entry, even those that aren't saved to the user's profile. Any help would be appreciated, thanks.
{{ entry.title }}

{% if currentUser.booksField.contains("entry.id") %}
  I have read this book
{% else %}
  I haven't read this book
{% endif %}

**Tags:** conditional, user-profile, entries-field

---

## 2 answers

### Answer by username_1 on 2017-06-07. Score: 1 | Accepted answer

I don't think contains will behave like you expect (since booksField will return an array of objects and your checking that against an integer id).
Not the prettiest, but something like this should work:
{{ entry.title }}

{% set read = false %}
{% for book in currentUser.booksField %}
    {% if book.id == entry.id %}
        {% set read = true %}
    {% endif %}
{% endfor %}

{% if read %}
    I have read this book
{% else %}
    I haven't read this book
{% endif %}

And you could make it more performant (especially if you have large numbers of books) by using https://github.com/marionnewlevant/craft-mnbreakandcontinue, which adds a {% break %} Twig tag.


---

### Answer by username_2 on 2017-06-07. Score: 1

My initial though was that you do not need to not put the "entry.id" in quotes because it's a variable reference, but the contains() method appear to not work as expected here even when this was corrected.
The booksField returns an ElementCriteriaModel, so you should be able to fetch the list of IDs as a plain array and then use Twig's in "containment operator" to search it:
{{ entry.title }}

{% if entry.id in currentUser.booksField.ids() %}
  I have read this book
{% else %}
  I haven't read this book
{% endif %}

> **Comments:**
> **username_0**: Hi Isaac, thanks for this. I removed the quotes around ID but it still doesn't work I'm afraid. I'm not even sure if the 'contains' conditional is the right way of doing this, with the Entries field being an array. Any other ideas are welcome, thanks.  
> **username_2**: @Stephen it looks like an array but it's actually a ElementCriteriaModel. I will update my answer with some additional code that I think should do what you want.  

---

