Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

FormAction in CMS


Go to End
Reply

21 Posts   6322 Views

Avatar
Carbon Crayon

2 January 2009 at 4:00am (Last edited: 2 January 2009 4:01am), Community Member, 598 Posts

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!

Avatar
UncleCheese

2 January 2009 at 7:47am 4085 Posts

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?

Avatar
Carbon Crayon

2 January 2009 at 10:55am (Last edited: 2 January 2009 11:00am), Community Member, 598 Posts

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 :)

Avatar
Carbon Crayon

3 January 2009 at 2:31am Community Member, 598 Posts

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 :)

Avatar
UncleCheese

3 January 2009 at 4:18am 4085 Posts

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.

Avatar
Carbon Crayon

3 January 2009 at 6:43am (Last edited: 3 January 2009 6:50am), Community Member, 598 Posts

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

Avatar
UncleCheese

3 January 2009 at 7:13am 4085 Posts

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

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

Use ImageField.php as an example.

Avatar
UncleCheese

3 January 2009 at 7:34am 4085 Posts

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.

Go to Top