Manu
Manu

Reputation: 4500

Creating a new TinyMCE form widget

I'm using tinyMCE in my forms and have noticed that I only use two configurations : a very limited one, for things like comments, and a more complex one, for the administators of the site.

For the moment I do that by repeating this sort of code in each form :

    //TinyMCE
    $this->widgetSchema['comment'] = new sfWidgetFormTextareaTinyMCE(array(
      'width'  => 550,
      'height' => 150,
      'config' => '
    theme_advanced_buttons1 : "bold,italic,separator,bullist,separator,link, sub,sup,separator,charmap",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_path : false,
    language : "fr"
'
    ));

Could I (and how) create two widgets, say TinyMCEsmall and TinyMCEfull so that I don't have to repeat code ?

Upvotes: 2

Views: 1358

Answers (2)

Jeremy Kauffman
Jeremy Kauffman

Reputation: 10403

Like this:

class sfWidgetFormTextareaTinyMCESmall extends sfWidgetFormTextareaTinyMCE
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);

    //assuming there are no options on the parent class that you need, call setOptions. If you need to retain some, make individual setOption calls.
    $this->addOption('width', 550);
    $this->addoption('height', 150);
    $this->addOption('config', '
theme_advanced_buttons1 : "bold,italic,separator,bullist,separator,link, sub,sup,separator,charmap",
theme_advanced_buttons2 : "",
theme_advanced_buttons3 : "",
theme_advanced_path : false,
language : "fr"
');
  }
}

Note that options you pass in will still take precedence.

Upvotes: 3

Kennethvr
Kennethvr

Reputation: 2680

sure, create a class that extends the sfWidgetFormTextareaTinyMCE and put your specifics in its constructor.

put it in let's say a var called myTinyMCE

and call at last in the constructor the parent::constructor(myTinyMCE)

where you give the setup as a parameter.

then in the form don't call the sfWidgetFormTextareaTinyMCE anymore, but your class you created...

class myClass extends sfWidgetFormTextareaTinyMCE { 

public class __construct(){
      myTinyMCE = array(
      'width'  => 550,
      'height' => 150,
      'config' => '
    theme_advanced_buttons1 : "bold,italic,separator,bullist,separator,link, sub,sup,separator,charmap",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_path : false,
    language : "fr"
'
    );

  parent::__construct(myTinyMCE);

 }

}

more info on that can be found here How do I get a PHP class constructor to call its parent's parent's constructor

Upvotes: -1

Related Questions