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

Iterable field data #241

Merged
merged 8 commits into from
Jun 12, 2024
Merged

Iterable field data #241

merged 8 commits into from
Jun 12, 2024

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Jun 11, 2024

Description

Makes it possible to loop over CKEditor field values, which will split the value into chunks representing either HTML markup or nested entries.

{% for chunk in entry.myCkeditorField %}
  {% if chunk.type == 'markup' %}
    <div class="markup">
      {{ chunk }}
    </div>
  {% else %}
    <div class="entry" data-entry-id="{{ chunk.entry.id }}">
      {{ chunk }}
    </div>
  {% endif %}
{% endfor %}

Outputting nested entry chunks directly will render their partial templates. Alternatively, you could call the entry’s render() method explicitly if you want to pass custom variables to the partial template:

{{ chunk.entry.render({
  foo: 'bar',
}) }}

Or bypass the partial template entirely:

{% switch chunk.entry.type.handle %}
  {% case 'text' %}
    <p>{{ chunk.entry.myTextField }}</p>
  {% case 'img' %}
    {% set asset = chunk.entry.myAssetsField.eagerly().one() %}
    {% if asset %}
      {{ asset.getImg({ width: 500 }) }}
    {% endif %}
{% endswitch %}

Related issues

@brandonkelly brandonkelly requested a review from i-just June 11, 2024 16:32
@brandonkelly brandonkelly changed the title FieldData::getChunks() Iterable field data Jun 11, 2024
# Conflicts:
#	composer.json
#	composer.lock
#	src/Field.php
Copy link
Contributor

@i-just i-just left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Displaying the cke field content with {{ entry.myCkeField }} works as expected, but using the “chunked” approach and the example code from the PR, I had to run {{ chunk }} via the raw filter. That doesn’t seem right, does it?

There’s also a problem with editing cards with provisional changes. If you edit a nested entry and don’t save it, you see the card with the “edited” flag - that’s all good; if you now reload the page - the card still shows with the “edited” flag - that’s good; but if you try to edit that entry, you’ll get internal server error: “Invalid element ID” (because entry id is of a provisional draft and ElementsController::_elementById() doesn’t like that).

@brandonkelly brandonkelly merged commit 2dfb9ac into 4.x Jun 12, 2024
5 checks passed
@brandonkelly brandonkelly deleted the feature/chunks branch June 12, 2024 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants