2

I have a channel projects and a channel wishes (People can submit their 'wishes' for that particular project.

In a project I made the 'entry field' projectHasWishes to connect wishes to project.

Now we use guestentries plugin to get wishes from the visitors. This form is that particular projectpage on the frontend. How can I relate them easily to the right project? Can I use a hidden field for this? Or something else? Thanks for helping me out.

4

The way you've set it up now, it won't be possible without doing 2 requests: one to create the wish, and one to alter the project (add the wish relation).

If you'd turn it around, and give the wish section a field 'belongsToProject' (or whatever else you'd like to call it), you'd be able to create this hidden input field in your wish form:

<input type="hidden" name="fields[belongsToProject][]" value="{{ project.id }}" />

Notice the []. Those are needed because a relation field can have multiple related elements, even if you limit it to have only one. On saving, the field will be populated with the project relation you're looking for.

On viewing your projects and wishes, assuming you have a variable called project with your current project in it, you could simply do:

{% set wishesForProject = craft.entries.find({
    section: 'wishes',
    relatedTo: {
        targetElement: project,
        field: 'belongsToProject',
    },
}) %}

{% for wish in wishesForProject %}
    {{ wish.title }}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.