Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[3.3] Add Location::getFirstChild() method #148

Merged
merged 2 commits into from
Nov 19, 2019
Merged

[3.3] Add Location::getFirstChild() method #148

merged 2 commits into from
Nov 19, 2019

Conversation

pspanja
Copy link
Member

@pspanja pspanja commented Nov 19, 2019

Lately I've seen some uses of getting the first child of a Location, implemented through the getChildren() method:

{{ location.children(1)[0].content.name }}

Method getChildren() returns an array of Location objects, intended for iteration, and using it like that is awkward and does not provide for limiting on the ContentType. For that reason I'm adding a new method on the Location object:

Location::getFirstChild(?string $contentTypeIdentifier = null): ?Location

Use from Twig:

{% set firstChild = location.firstChild('article') %}

{% if firstChild is not null %}
    {{ firstChild.content.name }}
{% endif %}

Or in redirect configuration:

ezpublish:
  system:
    frontend_group:
      ngcontent_view:
        full:
          ng_frontpage:
            temporary_redirect: '@=location.getFirstChild'
            match:
              Identifier\ContentType: ng_frontpage

@pspanja pspanja changed the title Add Location::getFirstChild() method [3.3] Add Location::getFirstChild() method Nov 19, 2019
@pspanja pspanja merged commit f0d124d into master Nov 19, 2019
@pspanja
Copy link
Member Author

pspanja commented Nov 19, 2019

Thanks!

@pspanja pspanja deleted the first_child branch November 19, 2019 10:10
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants