FCKeditor dans l'admin generator (Symfony 1.2, Doctrine)

Mise en place d’un “éditeur de texte riche” (FCKeditor) dans l’admin generator d’un projet Symfony 1.2 (Doctrine)

Pré-requis :

Intégrer FCK

  • Télécharger les fichiers
    http://www.fckeditor.net/download
  • Copier les dans web/js/fckeditor
  • Modifier

    /apps/backend/config/settings.yml

    ...
    .settings:
      rich_text_fck_js_dir: js/fckeditor
    ...

Créer un nouveau widget pour sfFormExtraPlugin

Créer ce fichier :

/plugin/sfFormExtraPlugin/lib/widget/sfWidgetFormTextareaFCK.class.php

addOption ('editor', 'fck');
        $this->addOption ('css', false);

        parent::configure ($options, $attributes);
    }

    /**
        * @param  string $name        The element name
        * @param  string $value       The value displayed in this widget
        * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
        * @param  array  $errors      An array of errors for the field
        *
        * @return string An HTML tag string
        *
        * @see sfWidgetForm
        */
    public function render ($name, $value = null, $attributes = array(), $errors = array())
    {
        $editorClass = 'sfRichTextEditorFCK';
        if (!class_exists ($editorClass)) {
            throw new sfConfigurationException (sprintf ('The rich text editor "%s" does not exist.', $editorClass));
        }

        $editor = new $editorClass ();
        if (!in_array ('sfRichTextEditor', class_parents ($editor))) {
            throw new sfConfigurationException (sprintf ('The editor "%s" must extend sfRichTextEditor.', $editor));
        }

        $attributes = array_merge ($this->attributes, $this->getOptions ());
        $editor->initialize ($name, $value, $attributes);
        return $editor->toHTML ();
    }
}
?>

J’ai fait trois modifications dans le fichier proposé par le site dont je donne le lien plus haut :

  1. Ligne 1 : Attention au cas ou le short tag n’est pas activé
  2. Standardisation du nom de la classe (sfWidgetFormTextareaFCK)
  3. Ligne 49 : Il faut modifier cette ligne pour pouvoir passer des attributs HTML par defaut au widget:
     $attributes = array_merge ($this->attributes, $this->getOptions ());

Utilisation

Modifier le formulaire de Contenu :

/lib/form/doctrine/ContenuForm.class.php

$this->widgetSchema['html'] = new sfWidgetFormTextareaFCK(
  array(),
  array('height'=>'700','width'=>'800'
));
     ...

Configurer l’upload de fichiers

Remplacer le fichier situé dans :

/web/js/fckeditor/editor/filemanager/connectors/php/config.php

Par celui-là :

http://trac.symfony-project.org/attachment/wiki/HowToUseFCKeditor/config.new

    Il y a 2 lignes importantes :

  1. UserFilesPath
    $Config['UserFilesPath'] = DIRECTORY_SEPARATOR . 'userfiles' . DIRECTORY_SEPARATOR ;

    que l’on transforme comme ceci :

    $Config['UserFilesPath'] = /uploads/fck/ ;
  2. UserFilesAbsolutePath
    $Config['UserFilesAbsolutePath'] = $_SERVER[DOCUMENT_ROOT] . $Config['UserFilesPath'] ;

    Qu’il vous faut éventuellement modifier si la racine de votre projet n’est pas la même que celle de votre serveur:

    $Config['UserFilesAbsolutePath'] = $_SERVER[DOCUMENT_ROOT] . /myProject/ . $Config['UserFilesPath'] ;


Sources :

Voir l’étude de cas
Lire l’article
Voir le témoignage
Fermer