Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » FormAction in CMS

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 6096 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    FormAction in CMS Link to this post

    I am trying to create a button that will fire off a function in the CMS admin panel, but I can't seem to get it to work.

    here is what I tried first inside the getCMSFields function:

    $fields->addFieldToTab('Root.Content.Seasons', new FormAction("EndSeason", "Finish Season"));


    but that throws an error saying it cant find the function EndSeason in class form. I'm assuming the Form action needs to be in a proper form object, so then I tried:

    function EndSeasonForm(){

    $fields = new FieldSet(new NumericField ("Position"));
    $actions = new FieldSet(new FormAction("EndSeason", "Finish Season"));
       
       return new Form($this, 'EndSeasonForm', $fields, $actions);
    }

    function getCMSFields() {
          $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Seasons', $this->EndSeasonForm);
          return $fields;   
       }


    but that just breaks the whole CMS giving a "Call to a member function XML_val() on a non-object" error

    Does anyone know how to do this type of thing?

    cheer and Happy new year!

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    Wow. Well, I'll start by saying that I'll be amazed if this is possible.

    That said, this line has an issue:
    $fields->addFieldToTab('Root.Content.Seasons', $this->EndSeasonForm);

    You're passing $this->EndSeasonForm as a property of your model, when it is really a function. So when the CMS form gets parsed, it tries to run XML_val() on something that doesnt' exist. My hope is that if you pass it $this->EndSeasonForm() instead, it will call XML_val() on a valid Form object and you'll get some results.

    But just thinking about getting a form within a form to submit properly just gives me a headache. My guess is there's probably a better way to do this. What's your goal?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    adding the () on the end I get the same error

    Basically what I want to do is have a way of the user triggering a function from the CMS, ideally by a confirmation button. It's for a tennis club site where I have a load of fixture objects which make up the season and at the end of the season the user would click the 'end season' button which records all the fixtures and results into a 'PreviousSeason' object and deletes all of fixtures ready for the new season. So I'm thinking it doesn't have to be a form, I just need a button which executes the EndSeason() function.

    I can run it from the front end, but that seems a little inconsistent as all the other team management is done from the CMS.

    Any ideas?

    cheers

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    Ok so am I right in thinking that in order to add a button to the CMS interface i need to create a custom template? Does this also mean i have to create a whole new area (like newsletter/security etc) for my team management? That's not neccacerily bad, but I'm not entirely sure how to go about it.

    Is there no way to add a button to a Page model in the same way we add form fields?

    Thanks for the help

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    There is, but it's ugly. One thing you have to think about is that this new button will need some javascript attached to it because all the form submissions are done with AJAX in Silverstripe. This is why nested forms (the ImageField, for instance) renders in an iframe. I think that's your best shot. You might be able to pull off something like this...

    $fields->addFieldToTab("Root.Content.Main", new LiteralField('iframe','<iframe src="/MyController/iframe" width="500" height="200"></iframe>'));

    now create MyController..

    class MyController extends Controller
    {
    static $allowed_actions = array ('iframe');
    function SpecialForm()
    {
    return new Form(
    $this,
    "SpecialForm",
    $some_fieldset,
    new FieldSet(new FormAction('SomeFunction','Do some function!'))
    );
    }

    function SomeFunction($data,$form)
    {
    // do whatcha need to do
    }
    }

    And finally, in mysite/templates (not layout) create MyController_iframe.ss

    doctype
    <html>
    <body>
    $SpecialForm
    </body>
    </html>

    That's kind of the rough idea. I'll look into LeftAndMain and see if there's a way to customize the actions at the bottom of the page, but I really don't think so.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    Thanks for your help with this, I am moving forward, I now have a button which kindof works, only I need the 'EndSeason' function to get the ID of the page that the action was called from....before I was just calling $this->ID but now the function is in the EndSeasonController.php it doesnt work.

    Is there a way I can populate a hidden field with the ID of the page its on? At the moment I cant find a way for it to dynamically populate itself?

    Oh I also had to put an empty Link() method into the EndSeasonController for it to work....not sure why but is was erroring out saying it couldn't find the method Link() in Object.php. I stuck an empty one in and it worked :S

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    You need to pass the id through the url for the iframe

    <iframe src="/MyController/iframe/{$this->ID}"

    Use ImageField.php as an example.

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    Looks like you can add

    function getCMSActions()
    {
    return new FormAction....
    }

    to your model, and it should let you add a custom button. Try it out.

    6096 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.