Skip to content

feat: add dynamic lists based on submitted values - #1

Open
mnlmaier wants to merge 1 commit into
thomasvantuycom:mainfrom
mnlmaier:feature/conditional-lists
Open

feat: add dynamic lists based on submitted values#1
mnlmaier wants to merge 1 commit into
thomasvantuycom:mainfrom
mnlmaier:feature/conditional-lists

Conversation

@mnlmaier

Copy link
Copy Markdown

Hey, needed this for a feature in a project I am currently working on. Users should be able to pick which newsletters they'd like to subscribe to.

Thought it might be useful, if not, feel free to just close the PR and I'll continue working with my fork.

Thanks for the plugin!
Manuel


Lists were static per form, so every contact ended up in the same lists. Forms that let people pick what they want to receive had no way to route contacts to matching lists.

Enabling Dynamic Lists reveals a set of rules that map a form field and an expected value to additional lists, and makes Lists optional so the submission alone can determine where a contact ends up. Array values, such as a checkboxes field, match when the value is one of the selected options, so picking multiple options adds the contact to multiple lists. Leaving the value blank matches any truthy value. Matching rules are combined with the configured lists and deduplicated, for both regular and double opt-in contacts.

Lists were static per form, so every contact ended up in the same lists.
Forms that let people pick what they want to receive had no way to route
contacts to matching lists.

Enabling Dynamic Lists reveals a set of rules that map a form field and an
expected value to additional lists, and makes Lists optional so the
submission alone can determine where a contact ends up. Array values, such
as a checkboxes field, match when the value is one of the selected options,
so picking multiple options adds the contact to multiple lists. Leaving the
value blank matches any truthy value. Matching rules are combined with the
configured lists and deduplicated, for both regular and double opt-in
contacts.
@thomasvantuycom

Copy link
Copy Markdown
Owner

Hi @mnlmaier,

Thank you for the PR. I can definitely see how this functionality would be useful, but I'm wondering whether the proposed solution is the best and most general approach.

Having dynamic lists based on submitted form values is certainly useful and, in some cases, necessary. However, I'm not particularly fond of the current dynamic rules UI, where users have to manually copy the possible values of a form field and map each one to one or more lists. This feels somewhat cumbersome and error-prone, especially when the form field already represents the values we want to use. I'd rather have the value field be a select in that case, although that's more complex and something I'd need to investigate to see if it's possible.

I can also imagine a use case where someone simply wants a form field to determine the lists a submission should be added to. In that case, the addon would either need an additional lists_field option, or users would have to create a redundant mapping where each field value is manually mapped to a list with the same value.

Another solution for these cases would be to use a listener:

<?php

namespace App\Listeners;

use Statamic\Events\FormSubmitted;

class SetSubmissionBrevoLists
{
    public function handle(FormSubmitted $event): void
    {
        $submission = $event->submission;
        $form = $submission->form();

        if ($form->has('brevo')) {
            $config = collect($form->get('brevo'));
            $lists = $config->get('lists', []);

            $topicLists = [
                'banking'  => [2],
                'payments' => [3, 4],
                'commerce' => [5],
            ];

            $roleLists = [
                'partner' => [6],
                'member'  => [7],
            ];

            $additionalLists = [
                ...($topicLists[$submission->get('topics')] ?? []),
                ...($roleLists[$submission->get('role')] ?? []),
            ];

            $config->set(
                'lists',
                array_values(array_unique(array_merge($lists, $additionalLists)))
            );

            $form->set('brevo', $config);
            $submission->form($form);

            $event->submission = $submission;
        }
    }
}

Of course, I understand that having this functionality available through the UI would be preferable, so I'm not necessarily suggesting that listeners are the ideal solution. I'm mainly wondering if there's a more generic approach we could take that covers these use cases without requiring users to duplicate information that's already present in their form configuration.

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