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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

FormAction in CMS


Go to End


21 Posts   9240 Views

Avatar
Carbon Crayon

Community Member, 598 Posts

2 January 2009 at 4:00am

Edited: 02/01/2009 4:01am

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

Forum Moderator, 4102 Posts

2 January 2009 at 7:47am

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

Community Member, 598 Posts

2 January 2009 at 10:55am

Edited: 02/01/2009 11:00am

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

Community Member, 598 Posts

3 January 2009 at 2:31am

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

Forum Moderator, 4102 Posts

3 January 2009 at 4:18am

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

Community Member, 598 Posts

3 January 2009 at 6:43am

Edited: 03/01/2009 6:50am

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

Forum Moderator, 4102 Posts

3 January 2009 at 7:13am

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

Forum Moderator, 4102 Posts

3 January 2009 at 7:34am

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