<p>You both are correct.</p>
<p>I started experimenting with and without .one(). But the real problem was that the person i was trying to help did not fill out all the required fields and thus the errors were thrown and the behaviour threw me off...</p>
<p>Sorry for this and thank you for all your help and tips</p>

CC BY-SA 4.0
2022-08-09T12:27:17.053Z
2022-08-09T12:27:17.053Z
Leonardo
<p>If the <code>linkedEntry</code> field is empty, then <code>category.linkedPage.one()</code> will evaluate to <code>null</code>, so you should always add a condition to check that a value exists before calling a method on it.</p>
<pre><code>{% set linkedEntry = category.linkedPage.one() %}

{# Explicit syntax. #}
{% if linkedEntry is not null %}
    {% set handle = linkedEntry.section.handle %}
{% endif %}

{# Alternative, simpler syntax. #}
{% if linkedEntry %}
    {% set handle = linkedEntry.section.handle %}
{% endif %}
</code></pre>

CC BY-SA 4.0
2022-08-02T11:48:43.140Z
2022-08-02T11:48:43.140Z
Ben Croker
<p>You haven't executed your element query: <a href="https://craftcms.com/docs/4.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/element-queries.html#executing-element-queries</a></p>
<p>(If you attempted to do this in Craft 3 it would have given you a slapped wrist by logging a deprecation warning in the control panel, but would have executed the element query for you anyway). Craft 4 is more strict about ensuring that you execute your element queries yourself. It's an important concept to understand, because otherwise you can blindly end up putting element queries inside loops, which is terrible for performance.</p>
<pre><code>{% set linkedEntry = category.linkedPage.one() %}
</code></pre>

CC BY-SA 4.0
2022-08-02T09:44:33.467Z
2022-08-02T09:44:33.467Z
James Smith
<p>Maybe Craft 4 is playing games with me but im stuck on this problem:</p>
<ul>
<li><code>linkedEntry</code> is an entry field which is limited to 1 entry.</li>
</ul>
<p>This outputs the section handle correctly</p>
<pre class="lang-php prettyprint-override"><code> {% set linkedEntry = category.linkedPage %}
 {% dd(linkedEntry.section.handle) %}
</code></pre>
<p>This tells me that the <code>section()</code> function is called without an parameter</p>
<pre class="lang-php prettyprint-override"><code>{% set linkedEntry = category.linkedPage %}
{% if linkedEntry.section.handle === 'exampleHandle' %}
</code></pre>
<p>And this tells me that i cant call the attribute <code>section</code> on null</p>
<pre class="lang-php prettyprint-override"><code>{% if category.linkedPage.section.handle === 'exampleHandle' %}
</code></pre>
<p>Am i missing something thats new in Craft 4?</p>
<p>Greetings</p>

<templating><entry><sections><craft4>
Get section handle of an entry inside of an EntryField
# Get section handle of an entry inside of an EntryField

(Asked by: username_0 on 2022-08-02)

Maybe Craft 4 is playing games with me but im stuck on this problem:

linkedEntry is an entry field which is limited to 1 entry.

This outputs the section handle correctly
 {% set linkedEntry = category.linkedPage %}
 {% dd(linkedEntry.section.handle) %}

This tells me that the section() function is called without an parameter
{% set linkedEntry = category.linkedPage %}
{% if linkedEntry.section.handle === 'exampleHandle' %}

And this tells me that i cant call the attribute section on null
{% if category.linkedPage.section.handle === 'exampleHandle' %}

Am i missing something thats new in Craft 4?
Greetings

**Tags:** templating, entry, sections, craft4


---

## 3 answers

### Answer by username_0 on 2022-08-09. Score: 0 | Accepted answer

You both are correct.

I started experimenting with and without .one(). But the real problem was that the person i was trying to help did not fill out all the required fields and thus the errors were thrown and the behaviour threw me off...

Sorry for this and thank you for all your help and tips


---

### Answer by username_1 on 2022-08-02. Score: 2

If the linkedEntry field is empty, then category.linkedPage.one() will evaluate to null, so you should always add a condition to check that a value exists before calling a method on it.
{% set linkedEntry = category.linkedPage.one() %}

{# Explicit syntax. #}
{% if linkedEntry is not null %}
    {% set handle = linkedEntry.section.handle %}
{% endif %}

{# Alternative, simpler syntax. #}
{% if linkedEntry %}
    {% set handle = linkedEntry.section.handle %}
{% endif %}


---

### Answer by username_2 on 2022-08-02. Score: 1

You haven't executed your element query: https://craftcms.com/docs/4.x/element-queries.html#executing-element-queries
(If you attempted to do this in Craft 3 it would have given you a slapped wrist by logging a deprecation warning in the control panel, but would have executed the element query for you anyway). Craft 4 is more strict about ensuring that you execute your element queries yourself. It's an important concept to understand, because otherwise you can blindly end up putting element queries inside loops, which is terrible for performance.
{% set linkedEntry = category.linkedPage.one() %}


---

