Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[RFC] Twig relationship/taxonomy functions & filters #6774

Closed
wants to merge 4 commits into from
Closed

[RFC] Twig relationship/taxonomy functions & filters #6774

wants to merge 4 commits into from

Conversation

GwendolenLynch
Copy link
Contributor

@GwendolenLynch GwendolenLynch commented Jun 25, 2017

OK, so I had some time to waste waiting on PRs to get merged, and I got thinking about the debate at the last meeting regarding decoupling relationships & taxonomy objects from an entity in v4.

The point of contention was how we would approach that, so this is a PoC RFC … that I am either happy to see though to merge if we all like the approach, or just close the RFC and delete the branch if we think not … really, it wasn't a lot of work and I was bored! 🤣

Have a look, talk to me!

EDIT: It is worth noting that these are registered as filters & functions, the examples below are done using the Twig functions, 'cause personal taste.

{{ related }}

    {# Get all related, published, entities to "record" #}
    {% set related = related(record) %}

    {# Get only "pages" ContentType records related to this record #}
    {% set related_pages = related(record, 'pages') %}

    {# Get a maximum of 5 *each* of related "entries" & "pages" #}
    {% set related_selection = related(record, ['entries', 'pages'], 5) %}

Example use:

    {% set related = related(record) %}
    <ul>
    {% for entity in related.pages %}
        <li>{{ entity.title }}</li>
    {% endfor %}
    </ul>
    {% set related = related(record) %}
    {% for type, entities in related %}
        <p>{{ type }} related to this record:</p>
        <ul>
        {% for entity in entities %}
            <li>{{ entity.title }}</li>
        {% endfor %}
        </ul>
    {% endfor %}

{{ taxonomy }}

    {# Get all taxonomy entries for this "record" #}
    {% set taxonomy = taxonomy(record) %}

    {# Get only "categories" taxonomy entries for this "record" #}
    {% set taxonomy_by_type = taxonomy(record, 'categories') %}

    {# Get only "tags" & "categories" taxonomy entries for this "record" #}
    {% set taxonomy_by_types = taxonomy(record, ['tags', 'categories']) %}

Example use:

    {% set taxonomy = taxonomy(record) %}
    <ul>
    {% for entities in taxonomy.tags %}
        <li>{{ entity.name }}</li>
    {% endfor %}
    </ul>
    {% set taxonomy = taxonomy(record) %}
    {% for type, entities in taxonomy %}
        <p>{{ type }} taxonomy of this record:</p>
        <ul>
            {% for entity in entities %}
                <li>{{ entity.name }}</li>
            {% endfor %}
        </ul>
    {% endfor %}

@GwendolenLynch GwendolenLynch added the RFC Request For Comments label Jun 25, 2017
@GwendolenLynch
Copy link
Contributor Author

Updated the RFC examples with some comments, and a first "simpler" example for each … and a left out parameter that some 🐨 forgot to cherry-pick from the "messing around branch"

@GwendolenLynch
Copy link
Contributor Author

GwendolenLynch commented Jun 26, 2017

OK, seeing as this is PoC & RFC, I've also added the other thing that doesn't belong, as an object, coupled to the entity … contenttype.

Internally, it just returns a recursive ImmutableBag, hence the ability to .getPath('fields/image/extensions'), so maybe it is a prime candidate for a Twig specific extend … or something to simplify getting by path for front-enders?

{{ contenttype }}

    {# Get the ContentType definition for this record #}
    {% set contenttype = contenttype(record) %}

Example use:

    {% set contenttype = contenttype(record) %}

    {# A nice title with the configured icon (colon thing wasn't my idea) #}
    <h4><i class="fa {{ contenttype.icon_many|replace(':', '-') }}"></i> 
    {{ contenttype.name }} ContentType</h4>

    {# Accessing the a field configuration parameter for the field named 'image'  #}
    {% set extensions = contenttype.getPath('fields/image/extensions') %}
    <p>The 'image' field accepts these file extensions: {{ extensions|join(', ') }}</p>

    {# What taxonomies are defined as available for the ContentType of *this* record #}
    <p>Available taxonomies: {{ contenttype.taxonomy|join(', ') }}</p>

    {# What ContentTypes are defined as available to be related to this *this* record #}
    {# Note that 'contenttype.relations' contains the parameters relevant, 
     # so using 'keys' just returns their names  #}
    <p>Relatable ContentTypes: {{ contenttype.relations.keys|join(', ') }}</p>

@bobdenotter
Copy link
Member

We didn't get around to discussing this yet during the dev-meeting, so let's let it sit for a bit.

@GwendolenLynch
Copy link
Contributor Author

We didn't get around to discussing this yet during the dev-meeting, so let's let it sit for a bit.

Yeah, zero stress … That's the idea of this one … it can hang around for a while as it is not a lot of work to maintain (unlike other branches), plus I am not convinced this is exactly what we want (I think it is close-ish though) …

Oh, and WRT to the debugging stuff, that is not the scope for this one specifically (probably at all), as that will require a bit more work and help from Team Texas when he's back online. Nonetheless what we agree on here affects how that will be handled to an extent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants