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

Allow referencing existing assets (fixed Replicator Asset field issue) #41

Merged
merged 6 commits into from
Feb 24, 2023

Conversation

duncanmcclean
Copy link
Owner

@duncanmcclean duncanmcclean commented Feb 24, 2023

This pull request allows for referencing existing assets in Guest Entries forms. This means you can retain the same image currently present when using file uploads in a Replicator field.

Hopefully fixes #38.

Example

If you have a Replicator field, with an asset inside it.

On creation of an entry, you can upload an entry just fine. However, when you go to update that asset, the selected asset is removed. This only applies for Replicator fields with file uploads in them.

To workaround the issue, on the edit form, when you're looping through Replicator iterations, you can use a hidden field which when submitted will tell Guest Entries to retain the currently selected file.

{{ replicator_field }}
  {{ images }}
    <input type="hidden" name="rich_text_and_image[0][gallery][]" value="{{ path }}">
  {{ /images }}
{{ /replicator_field }}

Why you need to do it this way just for Replicator fields

And now for the explanation as to why you need to do it this way & just for asset fields inside Replicator fields....

Essentially, whenever you update an entry, we merge in the data we've got from the request into the entry's existing data & hit save.

This means if you update an entry but haven't changed any 'root' asset fields (ones not inside Replicators/Bards/etc), we leave that alone and the data for it remains untouched.

If you upload a new file to replace it, then obviously we do overwrite the existing value but that's expected.

However, when it comes to updating stuff inside Replicator/Bard fields, stuff works slightly differently. If you provide any data for the replicator field at all, we'll replace the entire value of the Replicator value, not just the value for the part of the Replicator field you're editing.

We do this as we have no way to know if you've removed an item from the Replicator field. So when you update a Replicator field with no value for the Assets field, we set it to null and it's essentially being reset.

That's exactly what's happening in the issue I mentioned above.

The solution essentially means that we allow you to keep the existing value of the Assets field by providing the path of the asset(s) you wish to save to the field.

To Do

  • Revert test & re-write in Pest

@duncanmcclean duncanmcclean marked this pull request as ready for review February 24, 2023 21:40
@duncanmcclean duncanmcclean merged commit 0d5ef35 into main Feb 24, 2023
@duncanmcclean duncanmcclean deleted the feature/allow-referencing-existing-assets branch February 24, 2023 21:41
@duncanmcclean duncanmcclean changed the title Allow referencing existing assets Allow referencing existing assets (fixed Replicator Asset field issue) Feb 24, 2023
@github-actions
Copy link

Released as part of v2.1.1.

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.

Re-submission on replicator pictures make them disappeared
1 participant