I am trying to add a form to the frontend in which a registered user will be able to create and publish new block in a matrix field. I have read the documentation about it but am still failing miserably in achieving it...
The entry is part of a structure section with sectionId movementCoursesEntries
. The matrix fieldId is videos
. The blockId is video_box
and it has two plain text fields videoTitle
and videoUrl
.
I have used this code but what I am getting is that the after submitting the form, all the old matrix blocks are being deleted and the new one is not being created. Any advice is highly appreciated!
<form method="post" accept-charset="UTF-8">
{{ csrfInput() }}
{{ actionInput('entries/save-entry') }}
{{ redirectInput('/members-zone/{slug}') }}
{{ hiddenInput('movementCoursesEntries', '2') }}
{{ hiddenInput('enabled', '1') }}
{{ hiddenInput('entryId', entry.id) }}
{% if entry is defined %}
{# Retain existing blocks + sort order #}
{% for video_box in clone(entry.videos).anyStatus().ids() %}
{{ hiddenInput('fields[videoTitle][sortOrder][]', video_box) }}
{{ hiddenInput('fields[videoUrl][sortOrder][]', video_box) }}
{% endfor %}
{% endif %}
{# Add a new text block #}
{{ hiddenInput('fields[videoTitle][sortOrder][]', 'new:1') }}
{{ hiddenInput('fields[videoUrl][sortOrder][]', 'new:1') }}
{# Prefix the block's input names with `fields[<FieldHandle>][blocks][new:1]` #}
{% namespace "fields[blocks][blocks][new:1]" %}
{{ hiddenInput('type', 'text') }}
<input name="fields[videoTitle]">
<input name="fields[videoUrl]">
{% endnamespace %}
<input type="submit" value="Publish">
</form>