Making a global function in PageController.php

silverstripe-4
Tags: #<Tag:0x00007f10c7e74a80>

#1

SS 4

So in SS3 I made a global function form in the page controller of page.php where I then could call the function by just using $ContactForm in any other templates.

I’ve tried to follow this same method in SS4 but having no luck, i’ve pasted my PageController.php file below

As you can see i’ve created a public function called QuestionForm, but then if I do try and do $QuestionForm in any other templates it doesn’t show up, only when I use the default page template. Worked fine in SS3 but in SS4 not working.

Any help would be great.

<?php

use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\View\Requirements;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\View\ViewableData;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Control\Email\Email;

class PageController extends ContentController {
    /**
     * An array of actions that can be accessed via a request. Each array element should be an action name, and the
     * permissions or conditions required to allow the user to access it.
     *
     * <code>
     * [
     *     'action', // anyone can access this action
     *     'action' => true, // same as above
     *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
     *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
     * ];
     * </code>
     *
     * @var array
     */
    private static $allowed_actions = array('QuestionForm');


    public function Success(){
        return $this->getRequest()->getVar('success') ? true : false;
    }

    /**
     * @return Form
     */
    public function QuestionForm(){
        // Create fields
        $form = Form::create($this,
            __FUNCTION__,
            FieldList::create(
                TextField::create('FirstName', '')->setAttribute('placeholder','First Name*')->addExtraClass('input-holder clearfix'),
                TextField::create('LastName', '')->setAttribute('placeholder','Last Name*')->addExtraClass('input-holder clearfix'),
                TextField::create('Telephone','')->setAttribute('placeholder','Phone Number')->addExtraClass('input-holder clearfix'),
                EmailField::create('Email','')->setAttribute('placeholder','Email Address*')->addExtraClass('input-holder clearfix'),
                DropdownField::create('Age', '', array( "17" => '17', '18-20' => '18-20', '21-22' => '21-22', '23' => '23', '24+' => '24+'))->setAttribute('placeholder','17')->addExtraClass('input-holder select clearfix'),
                DropdownField::create('Consent', '', array('Do Stor-a-File have your consent to email you?*' => 'Do Stor-a-File have your consent to email you?*', 'Yes, I consent to receive future service offers/newsletters' => 'Yes, I consent to receive future service offers/newsletters', 'No, I do not wish to receive future service offers/newsletters' => 'No, I do not wish to receive future service offers/newsletters'))->setAttribute('placeholder','Do Stor-a-File have your consent to email you?*')->addExtraClass('input-holder select clearfix'),
                LiteralField::create('Hint','<p style="color:#ffffff; clear:both;">By submitting this form I agree that Stor-a-File may process my data in the manner described in the Stor-a-File <a href="/privacy-policy" style="color:#ffffff; text-decoration: underline;">Privacy Notice</a>. If you agreed to receive marketing emails but subsequently change your mind, you may opt out at any time <a href="/privacy-policy" style="color:#ffffff; text-decoration: underline">here</a>.</p>')
            ),

            FieldList::create(FormAction::create('SendFooterForm','Submit')->setUseButtonTag(true)->addExtraClass('button-list')),
            RequiredFields::create('Name', 'Company', 'Email','Telephone','Service','Enquiry','Consent'));

        $form->addExtraClass('row clearfix');

        return $form;
    }

    /********* Admin area -> Settings(Left Menu)->Email Server and Send Email(Tabs) Configuration
     * Author: Vaz
     * Return configuration from database used to set up the email's forms
     * @param $string
     * @return mixed
     *                                                    */
    public function getSiteConfigData($string){
        return SiteConfig::get()->last()->getField($string);
    }


    public function getEmailSubmissionText(){
        return SiteConfig::get()->first()->getField('EmailSubmissionText');
    }

    /**
     * Author: Vaz
     * Set whether the email contact footer will be show. Database Table:HomePage
     * @return bool
     */
}

#2

This might seem like a silly question but it’s the only thing I can think of for now - do your other controllers extend PageController?


#3

Ah thats the reason, for some reason my other controllers were extending contentcontroller rather than pagecontroller. now fixed the issue! thanks