3

Pretty new to magento and I'm setting up a custom module to override the core newsletter. All I really want to do is add ajax submit to the form, which is straightforward so far, and also allow the form to be both displayed in a lightbox and on the page, depending on the context of the request. i.e within my custom module controller, would I use something like the below when the request is via AJAX, or is there a more recommended way to do this kind of thing? Thanks for any help

$html = Mage::getSingleton('core/layout')->createBlock('core/template')
            ->setTemplate('my_module/form.phtml')->toHtml();
$this->getResponse()->setBody($html);

EDIT: update after advice from beeplogic. I now get a blank page with the following code. Is there anything obvious I'm doing wrong here?

What I have is a MyCompany_Newsletter_SubscriberController which has a newAction(), overriding core Newsletter controller newAction. My controller action is called fine. I'm trying to target the default newsletter/subscriber/new In my module config.xml, I have:

<frontend>
    <routers>
        <newsletter>
            <args>
                <modules>
                    <MyCompany_Newsletter before="Mage_Newsletter">MyCompany_Newsletter</MyCompany_Newsletter>
                </modules>
            </args>
        </newsletter>
    </routers>
    <layout>
        <updates>
            <newsletter>
                <file>newsletter.xml</file>
            </newsletter>
        </updates>
    </layout>
</frontend>

Then in default/default/layout/newsletter.xml, I have:

<layout>
    <newsletter_subscriber>
        <block type="core/text_list" name="content" output="toHtml" as="content"/>
    </newsletter_subscriber>
    <newsletter_subscriber_new> <!--NOTE: this will be the generated action layout handle -->
        <reference name="content">
            <block type="core/template" template="newsletter/subscribe.phtml" />
        </reference>
    </newsletter_subscriber_new>
</layout>
  • I would recommend not overloading the newsletter.xml or hijacking the default route of the news letter module. Create your own frontend route, your controller can still extend the newsletter controller. Create your own layout XML file. This will help avoid conflicts in magento. If you need to the appropriate layout handle you can echo $this->getFullActionName() in the action method. – beeplogic Jul 31 '13 at 21:21
1

You can still use Magento's XML layout configuration to send snippets of HTML. In your controller you will need to specify the handle's to render:

public function formAction()
{
    $this->loadLayout('newsletter_ajax_form');
    $this->renderLayout();
}

Define the layout update for your module:

<frontend>
    <layout>
        <updates>
            <newsletter_ajax>
                <file>mycompany/newsletter.xml</file>
            </newsletter_ajax>
        </updates>
    </layout>
</frontend>

In the layout file: app/design/frontend/package/theme/layout/mycompany/newsletter.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <newsletter_ajax>
        <block type="core/text_list" name="content" output="toHtml" as="content"/>
    </newsletter_ajax>
    <newsletter_ajax_form> <!--NOTE: this will be the generated action layout handle -->
        <reference name="content">
            <block type="core/template" template="my_module/form.phtml" />
        </reference>
    </newsletter_ajax_form>
</layout>
  • Thanks beeplogic. I've followed your advice and am now getting a blank page (i'm in developer mode, no errors showing anywhere). I've edited my post to include more info on where I am now. – tomh545 Jul 31 '13 at 9:46
1

use the following form for newsletter instead of the default one

<form name="newsletter-validate-detail-popup" id="newsletter-validate-detail-popup" onSubmit="if(NewsletterSubscriberForm.validator.validate()){ new Ajax.Updater({success:'newsletter-validate-detail-popup'}, '<?php echo $this->getUrl('newsletter/subscriber/new') ?>', {asynchronous:true, evalScripts:false, onComplete:function(request, json){Element.hide('float-newsletter-loading');Element.show('feedback');hideFullDiv();}, onLoading:function(request, json){Element.hide('newsletter-validate-detail-popup');Element.show('float-newsletter-loading');}, parameters:Form.serialize(this)}); } return false;" action="/home/send" method="post">
        <div class="form-subscribe">
            <div class="v-fix" id="pop-fix"><input type="text" class="input-text required-entry validate-email" onblur="this.value==''?this.value='Enter your email address':''" onclick="this.value=='Enter your email address'?this.value='':''" value="Enter your email address" id="newsletter-popup" name="email"></div>
            <button title="Subscribe" class="button" type="submit" id="newsletter-float-submit"><span><span>Subscribe</span></span></button>
        </div>
    </form>

and edit the script as follows

<script type="text/javascript">
//&lt;![CDATA[
var NewsletterSubscriberForm = new VarienForm('newsletter-validate-detail-popup');
//]]&gt;

this is a built in functionality of magento, why develop a module for such a small task? (if u wanna learn creating a new module, you're welcome).

  • if u want to use it in popup/lightbox give it a different name and id and change the corresponding name and id in the varienForm object instantiation. – Shathish Aug 1 '13 at 12:11
  • Thanks Shathish. Part of the reason I was creating a module was that I thought for the lightbox part, I would need to detect if the request was AJAX, via the controller, before then loading the form template without rendering the rest of the page. Is that not the case? – tomh545 Aug 2 '13 at 10:28
  • I'm not aware your circumstances, it was my suggestion, if you find your method easier; you can do that :) – Shathish Aug 2 '13 at 10:35
  • Well, the newsletter form can be displayed in two contexts, either as normal in the body of the page, or within a lightbox/popup - the form itself would be the same and would ajax submit in both circumstances. I'm just trying to figure out the correct way to handle the rendering of just the form in the lightbox – tomh545 Aug 2 '13 at 10:41

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.