3

I want to embed stylesheets in the JCE Editor iframe only for specific pages, preferably using PHP. Right now, the JCE admin interface allows you to set stylesheets globally and by individual user profile for every instance where JCE is loaded in the admin control panel. However, I am creating custom components that load the editor for display like so:

<?php
$editor = JFactory::getEditor();  // JCE set by default
echo $editor->display();

I want to be able to load different stylesheets based on different sections of my components. As far as I know, this doesn't exist out of the box, so I'd like to see if there's some API method that could help me achieve this.

Something like:

<?php
$editor = JFactory::getEditor();  // JCE set by default

// calculate whether additional styles may be needed...
if (true === $needs_more_stylesheets_bool) {
   // Would be nice to do something like
   $editor->addStylesheet('specific_styles.css');
   // Or
   $editor->addInlineStyle('body{background:green}');
   // Or
   $editor->removeStylesheet('general_styles.css'); 

   // Or... with adding/editing user profiles... 
   $editor->loadUserProfile('user_2_with_different_stylesheets');
}
Nmk
  • 1,281
  • 2
  • 14
  • 25
danronmoon
  • 3,814
  • 5
  • 34
  • 56
  • Could you explain the problem with more details? – Matteus Hemström Nov 03 '12 at 10:38
  • Also, correct me if I'm wrong but it seems like JCE doesn't take any use of the `$params` variable. – Matteus Hemström Nov 03 '12 at 11:06
  • Updated question. I'm not sure about what the $params array accepts, or even if JCE might use them. – danronmoon Nov 03 '12 at 21:06
  • I looked into this, but I couldn't find anything close to a straight forward to accomplish this. That is, if the JCE profiles is not enough (using profiles you can use different sets of CSS). – Matteus Hemström Nov 03 '12 at 22:12
  • JCE profiles really aren't enough, unless I can pass the profile as a argument to some method. I'm using the editor for my custom components, so if there's something that works only with articles it would be insufficient. – danronmoon Nov 04 '12 at 00:39
  • Are you saying you want css to be used WHILE editing in a particular component? Would you load it based on the request? i.e. option="my_component"? – Elin Feb 19 '13 at 00:13
  • @Elin, yes I point my browser to `administrator/index.php?option=com_mycomponent&task=edit&cid=....`, and I have JCE popups that are embedded into the view using the 1st code snippet above. I would like to load my stylesheets into the iframes created by JCE based on what component they're being used in. I make several components for a site and sometimes their styling requirements vary. Would be nice to alter them before they're rendered. – danronmoon Feb 19 '13 at 00:21
  • You would have the context from the request so you could make a plugin that listens to the request and changes things based on that. – Elin Feb 21 '13 at 22:35

1 Answers1

0

I will suggest how you can add some inline styles, you can move on using the same method Editor class is located in root/libraries/joomla/html/editor.php

Around line you will find display function

public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array())
    {
        ...
        ...
$width = str_replace(';', '', $width);
        $height = str_replace(';', '', $height);

        // Initialise variables.
        $return = null;

        $args['name'] = $name;
        $args['content'] = $html;
        $args['width'] = $width;
        $args['height'] = $height;
        $args['col'] = $col;
        $args['row'] = $row;
        $args['buttons'] = $buttons;
        $args['id'] = $id ? $id : $name;
        $args['event'] = 'onDisplay';
                ...
}

I will try to pass my inline styles

public function display($name, $html, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array(),$inlinestyles){
...
$args['inlinestyles'] = $inlinestyles;
...
}

Now we have to edit our jce.php file located in root/plugins/editors/jce/jce.php

As you can see in the paramaters event we will change onDisplay event

Around line 108

public function onDisplay($name, $content, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null) {
... 
return $editor;
    }

Now we will change this function and use JDocument to parse our styles

public function onDisplay($name, $content, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null,$inlines) {
//blah blah some code
//here comes the fun part
if($inlines){
$document =& JFactory::getDocument();
$document->addStyleDeclaration($inlines);
}

} //end of ondisplay

Now in your component, you have to call your editor as it is documented in Joomla's docs

$inlines= 'BODY {'
        . 'background: #00ff00;'
        . 'color: rgb(0,0,255);'
        . '}'; 
$editor = JFactory::getEditor();
echo $editor->display("jobdesc", ""/*$itemData['body']*/, "400", "100", "150", "10", 1, null, null, null, array('mode' => 'advanced'),$inlines);

http://docs.joomla.org/JFactory/getEditor

vorillaz
  • 6,098
  • 2
  • 30
  • 46