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

[RTM] add hook so you can modify the form #18

Merged
merged 5 commits into from
Jun 19, 2018
Merged

Conversation

fritzmg
Copy link
Contributor

@fritzmg fritzmg commented May 28, 2018

This would allow you to modify the form to your needs. Example:

public function modifyMailChimpForm(\Haste\Form\Form $objForm, \Oneup\Contao\MailChimpBundle\Module\ModuleSubscribe $objModule)
{
    $objForm->removeFormField('submit');

    $objForm->addFormField('mandatory', [
        'inputType' => 'explanation',
        'eval' => ['text' => '<p>Some additional explanation before the submit button.</p>']
    ]);

    $objForm->addFormField('submit', [
        'label' => $GLOBALS['TL_LANG']['tl_module']['mailchimp']['labelSubmit'],
        'inputType' => 'submit',
    ]);
}

However, what does not work is stuff like this:

$objForm->getWidget('SALUTATION')->label = 'Foo';

Though I do not really get why.

@bytehead bytehead self-assigned this May 28, 2018
@bytehead bytehead self-requested a review May 28, 2018 17:48
@fritzmg
Copy link
Contributor Author

fritzmg commented May 29, 2018

However, what does not work is stuff like this:

$objForm->getWidget('SALUTATION')->label = 'Foo';

Though I do not really get why.

Ah, now I know why :). Obviously you need to execute that after any addFormField calls for example, or more accurately anything that marks the form as "dirty", since this will cause a regeneration of the widgets later on.

@bytehead
Copy link
Member

bytehead commented Jun 4, 2018

I think about to use events instead of an old hook (at least for the version for Contao 4). What do you think about?

@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 4, 2018

Fine by me :). Haven't worked with events yet as a replacement for Contao Hooks.

@bytehead bytehead changed the title add hook so you can modify the form [WIP] add hook so you can modify the form Jun 4, 2018
@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 18, 2018

I have updated the PR using events instead of hooks.

Usage example:

# src/AppBundle/Resources/config/services.yml
services:
  AppBundle\EventListener\MailChimpModifyForm:
    tags:
      - { name: kernel.event_listener, event: contao_mailchimp.modify_subscribe_form, method: onModifySubscribeForm }
// src/AppBundle/EventListener/MailChimpModifyForm.php
namespace AppBundle\EventListener;

use Oneup\Contao\MailChimpBundle\Event\ModifyFormEvent;

class MailChimpModifyForm
{
    public function onModifySubscribeForm(ModifyFormEvent $event)
    {
        $objForm = $event->getForm();

        $objForm->removeFormField('submit');

        $objForm->addFormField('lorem', [
            'inputType' => 'explanation',
            'eval' => [
                'text' => '<p>Lorem ipsum dolor.</p>', 
                'class' => 'lorem-ipsum'
            ]
        ]);

        $objForm->addFormField('submit', [
            'label' => $GLOBALS['TL_LANG']['tl_module']['mailchimp']['labelSubmit'],
            'inputType' => 'submit',
        ]);
    }
}

@bytehead
Copy link
Member

Yeah 🎉

@bytehead bytehead removed the question label Jun 18, 2018
@bytehead bytehead changed the title [WIP] add hook so you can modify the form [RTM] add hook so you can modify the form Jun 18, 2018
@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 18, 2018

One additional thought: I added setter methods to the event object. This would allow you to set a completely new form object in the event listener. However currently it would not be used. The following would need to be added:

         // event: modify form
+        $event = new ModifyFormEvent($objForm, $this);
         System::getContainer()->get('event_dispatcher')->dispatch(
             ModifyFormEvent::SUBSCRIBE, 
-            new ModifyFormEvent($objForm, $this),
+            $event
         );
+        $objForm = $event->getForm();

@bytehead what do you think?

// edit: Also may be the setter method for the Module object does not really make sense

@fritzmg fritzmg changed the title [RTM] add hook so you can modify the form [RFC] add hook so you can modify the form Jun 18, 2018
@bytehead
Copy link
Member

I'm fine with your proposed change. Why did you choose to store the Module object in the event in general?

@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 18, 2018

Why did you choose to store the Module object in the event in general?

You could have several different signup and unsubscribe modules and you might want to only modify specific ones, using the module id, or module css class etc.

@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 19, 2018

I have ultimately decided to remove the setters altogether ;). I couldn't think of a good use case where you would want to return a completely new form. I want to keep things simple and straight forward.

@bytehead any comments? Otherwise I'd consider it RTM

@bytehead
Copy link
Member

I want to keep things simple and straight forward.

I like! :)

@fritzmg fritzmg changed the title [RFC] add hook so you can modify the form [RTM] add hook so you can modify the form Jun 19, 2018
@fritzmg
Copy link
Contributor Author

fritzmg commented Jun 19, 2018

👍 Will you release it as 4.4.0?

@bytehead
Copy link
Member

Yep!

@bytehead bytehead merged commit b0a9f80 into 1up-lab:master Jun 19, 2018
@bytehead
Copy link
Member

Thank you! Released in 4.4.0.

@fritzmg fritzmg deleted the patch-3 branch June 19, 2018 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants