terminal42 / contao-ajaxform

Submit a form through ajax and display error or confirmation message.
14 stars 7 forks source link

AjaxForm doesn't interpret the current page or template group #2

Closed brandbrilliance closed 12 years ago

brandbrilliance commented 12 years ago

I have custom form_widget and form_checkbox, etc. templates. AjaxForm doesn't support this, so I had to hack it to add the ability back in, so that my custom templates are used. Since the ajax always knows the page it's on by the content element, you can use that to regenerate the missing information, so the the parent FORM class can work correctly.

Added to

    public function generateAjax()
    {
        $this->blnAjax = true;
        $this->strTemplate = 'ajaxform_inline';

        // find page and set current objPage, as we are running in Ajax mode
        global $objPage;
        $pageId = $this->Database->prepare("SELECT pid FROM tl_article WHERE id=(SELECT pid FROM tl_content WHERE id=?)")->execute($this->intId)->pid;
        $objPage = $this->getPageDetails($pageId);

        // Get the page layout
        $objLayout = $this->getPageLayout($objPage->layout);
        $objPage->template = strlen($objLayout->template) ? $objLayout->template : 'fe_page';
        $objPage->templateGroup = $objLayout->templates;

        return parent::generate();
    }

Also I simply copied in the $this->getPageLayout() method from PageRegular, as it's not statically declared.

aschempp commented 12 years ago

The page object is automatically initialized, if the page=id parameter is sent to the ajax.php. If that is not the case, wouldn't adding that solve the problem?

brandbrilliance commented 12 years ago

Ok, I never noticed the pageDetails in ajax.php before. That only cuts out the resolution of the page Object. However it's the templateGroup which is required to render overridden templates.

Without this $objPage->templateGroup setting, you can't override the templates correctly and my contact form uses the standard templates, and I heavily modified them.

Also, you should really then code in the pageId into the ajax request URL automatically, then it's not needed in the AjaxRequest method (which is what I did).

aschempp commented 12 years ago

If the global $objPage is available, should the templateGroup be considered automatically?

brandbrilliance commented 12 years ago

Nope. the templateGroup variable isn't setup in getPageDetails, that is only added into the $objPage in PageRegular.php. Since ajax never runs PageRegular, it won't be setup and therefore the incorrect templates are used. ONLY when I add this $objPage->templateGroup variable, does it read the correct templates.

PS. I wouldn't have added it if it wasn't necessary ;-)

aschempp commented 12 years ago

This should be fixed with the latest version of https://github.com/aschempp/contao-ajax