1

It is possible to get all entries by structureId ?

If yes how ?

For now i use something like this:

{% if entry.parent %}
    {%  set relatedEntries = entry.parent.children()|filter(v => v.type == 'default')|merge([entry.parent]) %}
{% else %}
    {% set relatedEntries = entry.children()|filter(v => v.type == 'default')|merge([entry]) %}
{% endif %}

But i'm very limited if for example some children entry has children then it doesn't works anymore.

1 Answer 1

3

Looks like you're trying to access an entries' descendants, which includes all children, children of children and so on. You can get all descendants of an entry using Entry::getDescendants():

{% set rootElement = entry.getAncestors()[0] ?? entry %}
{% set relatedEntries = rootElement.getDescendants() %}

Note the first line, which will always give you the first-level ancestor of the current entry, even if the current entry is multiple levels down in the hierarchy.

You can also limit an element query to entries that are descendants of a specific entry. This will allow you to specify additional conditions (like the type), this way you don't have to filter the list afterwards.

{% set rootElement = entry.getAncestors()[0] ?? entry %}
{% set relatedEntries = craft.entries()
    .descendantOf(rootElement)
    .type('default')
    .all()
%}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.