<p>You can check if certain elements match for your Query via <code>exists()</code></p>

<pre><code>entry.floors.type('floor').floorNumber(1).exists()
</code></pre>

<p>returns true or false. However keep in mind <code>entry.floors</code> returns an element of type <code>Query</code> and you'll append the parameters <code>type</code> and <code>floorNumber</code> to the Query. These properties won't reset magically since you call the object by reference and not by value thus you'll either need to reset them</p>

<pre><code>{% do entry.floors.type(null).floorNumber(null) %}
</code></pre>

<p>or you don't use the object directly and <a href="https://docs.craftcms.com/v3/templating/functions.html#clone-object" rel="nofollow noreferrer">clone/copy</a> it instead </p>

<pre><code>{% set exists = clone(entry.floors).type('floor').floorNumber(1).exists() %}
</code></pre>

CC BY-SA 4.0
2018-07-03T16:41:21.550Z
Thanks for the explanation, this is essentially what I ended up doing. Can you explain the difference between calling by reference vs value? I still don't understand the distinction.
Bryce
CC BY-SA 4.0
2018-07-03T17:05:09.783Z
Are you on Craft CMS Slack by chance? It's far easier to explain it there
Robin Schambach
CC BY-SA 4.0
2018-07-03T06:08:17.257Z
2018-07-03T06:08:17.257Z
Robin Schambach
<p><a href="http://sanderson.brycekirk.com/floor-plans/1-bedroom-1-bathroom/f123-f148-f248" rel="nofollow noreferrer">http://sanderson.brycekirk.com/floor-plans/1-bedroom-1-bathroom/f123-f148-f248</a></p>

<p>This page displays a floor plan, of which there are many. Currently, within each floorplan entry, you add the floors it is on, along with the image of each floor:
<a href="https://i.sstatic.net/4gQAL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4gQAL.png" alt="enter image description here"></a>
I'm using a for loop to display a big button for each available floor, as you can see on the linked page.</p>

<p>My problem: I want to add disabled buttons for each floor the floorplan <em>isn't</em> on, but I can't figure out how to do it.</p>

<p>The logic should be something like this ("floors" is the name of the matrix field, "floor" is the block type handle):</p>

<pre><code>IF '1' exists in entry.floors.floor.floorNumber
    Show the big selectable '1' button
ELSE 
    Show the disabled '1' button

IF '2' exists in entry.floors.floor.floorNumber
    Show the big selectable '2' button
ELSE 
    Show the disabled '2' button

etc for 3 and 4
</code></pre>

<p>It seems like their should be some straightforward solution, but I've had no luck.</p>

<p>Current code for displaying the floor number buttons:</p>

<pre><code>{% for floor in entry.floors.all() %}
{% if floor.type.handle == 'floor' %}
    &lt;li class="tabs-title {% if loop.first %}is-active{% endif %}"&gt;&lt;a href="#panel{{ floor.floorNumber }}" {% if loop.first %}aria-selected="true"{% endif %}&gt;&lt;img src="{{ siteUrl }}assets/img/{{ floor.floorNumber }}.png" alt="{{ floor.floorNumber }}" /&gt;&lt;/a&gt;&lt;/li&gt;
{% endif %}
{% endfor %}
</code></pre>

<p>Floor matrix:
<a href="https://i.sstatic.net/M7J9J.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/M7J9J.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2018-06-29T17:16:39.257Z
Could you eventually explain it more in Craft words? I'm sure I'm able to help you but I was not able to figure out the issue. What's your final goal? Do you want to search for a matrix field with or without a certain value? You can check if there is an element with certain criteria via `exists()` so `entry.floors.type('floor').floorNumber(1).exists()` will return true or false
Robin Schambach
CC BY-SA 4.0
2018-07-02T22:42:14.180Z
Yep, that seems to be the selector I'm looking for. For some reason though, using `exists()` screws up the matrix loop further down the page (the loop will only execute once). This happens even if I render no code, just set a variable. So setting `{% set twoExists = entry.floors.type('floor').floorNumber('2').exists() %} ` at the top of the page causes ONLY the second floor to render in the matrix loop, despite not calling that variable at all. Why is that??
Bryce
CC BY-SA 4.0
2018-07-02T22:47:22.843Z
The loop being affected: `{% for floor in entry.floors.all() %} {% if (floor.type.handle == 'floor') %}*Output floor plate images*{% endif %}{% endfor %}`
Bryce
CC BY-SA 4.0
2018-07-02T22:52:06.690Z
Ok... I was able to fix it using `{% for floor in entry.floors.type('floor').floorNumber('').all() %}` instead of the above. Is this a bug? Doesn't seem like setting that variable should be affecting a separate loop.
Bryce
<matrix>
Check if a value exists in a Matrix field
# Check if a value exists in a Matrix field

(Asked by: username_0 on 2018-06-28)

http://sanderson.brycekirk.com/floor-plans/1-bedroom-1-bathroom/f123-f148-f248
This page displays a floor plan, of which there are many. Currently, within each floorplan entry, you add the floors it is on, along with the image of each floor:

I'm using a for loop to display a big button for each available floor, as you can see on the linked page.
My problem: I want to add disabled buttons for each floor the floorplan isn't on, but I can't figure out how to do it.
The logic should be something like this ("floors" is the name of the matrix field, "floor" is the block type handle):
IF '1' exists in entry.floors.floor.floorNumber
    Show the big selectable '1' button
ELSE 
    Show the disabled '1' button

IF '2' exists in entry.floors.floor.floorNumber
    Show the big selectable '2' button
ELSE 
    Show the disabled '2' button

etc for 3 and 4

It seems like their should be some straightforward solution, but I've had no luck.
Current code for displaying the floor number buttons:
{% for floor in entry.floors.all() %}
{% if floor.type.handle == 'floor' %}
    
{% endif %}
{% endfor %}

Floor matrix:

**Tags:** matrix

> **Comments:**
> **username_1**: Could you eventually explain it more in Craft words? I'm sure I'm able to help you but I was not able to figure out the issue. What's your final goal? Do you want to search for a matrix field with or without a certain value? You can check if there is an element with certain criteria via `exists()` so `entry.floors.type('floor').floorNumber(1).exists()` will return true or false  
> **username_0**: Yep, that seems to be the selector I'm looking for. For some reason though, using `exists()` screws up the matrix loop further down the page (the loop will only execute once). This happens even if I render no code, just set a variable. So setting `{% set twoExists = entry.floors.type('floor').floorNumber('2').exists() %} ` at the top of the page causes ONLY the second floor to render in the matrix loop, despite not calling that variable at all. Why is that??  
> **username_0**: The loop being affected: `{% for floor in entry.floors.all() %} {% if (floor.type.handle == 'floor') %}*Output floor plate images*{% endif %}{% endfor %}`  
> **username_0**: Ok... I was able to fix it using `{% for floor in entry.floors.type('floor').floorNumber('').all() %}` instead of the above. Is this a bug? Doesn't seem like setting that variable should be affecting a separate loop.  

---

## 1 answers

### Answer by username_1 on 2018-07-03. Score: 2 | Accepted answer

You can check if certain elements match for your Query via exists()
entry.floors.type('floor').floorNumber(1).exists()

returns true or false. However keep in mind entry.floors returns an element of type Query and you'll append the parameters type and floorNumber to the Query. These properties won't reset magically since you call the object by reference and not by value thus you'll either need to reset them
{% do entry.floors.type(null).floorNumber(null) %}

or you don't use the object directly and clone/copy it instead 
{% set exists = clone(entry.floors).type('floor').floorNumber(1).exists() %}

> **Comments:**
> **username_0**: Thanks for the explanation, this is essentially what I ended up doing. Can you explain the difference between calling by reference vs value? I still don't understand the distinction.  
> **username_1**: Are you on Craft CMS Slack by chance? It's far easier to explain it there  

---

