Using WYSIWYG module with TinyMCE editor set to use theme styles the TinyMCE iframe links directly to the .less file rather than the preprocessed .css file. Is there any way LESS module can tap in to this? It looks to me the relevant function is wysiwyg_load_editor() in wysiwyg.module

Comments

mstrelan’s picture

I've come up with a solution/workaround, which currently only works with TinyMCE editor using the WYSIWYG module. The WYSIWYG module creates a javascript setting called "content_css" which is nested deep in to Drupal.settings. The code below will traverse the js settings searching for tinymce settings with a content_css setting. It will then call _less_pre_render() with only the files included in content_css and perform the relevant checks to ensure convert the less file to a css file, or serve up the already generated css file if current.

<?php
/**
 * Implements hook_js_alter().
 */
function MYMODULE_js_alter(&$javascript) {
  if (module_exists('less')) {
    $settings =& $javascript['settings'];
    foreach ($settings['data'] as $key => $setting) {
      if (isset($setting['wysiwyg']['configs']['tinymce'])) {
        $tinymce = $setting['wysiwyg']['configs']['tinymce'];
        foreach (array_keys($tinymce) as $profile) {
          if (substr($profile, 0, 6) == 'format') {
            if (isset($tinymce[$profile]['content_css'])) {
              $content_css = $setting['wysiwyg']['configs']['tinymce'][$profile]['content_css'];
              $css_files = explode(',', $content_css);
              $files = array('#items' => array());
              foreach ($css_files as $css_file) {
                if (strpos($css_file, base_path()) === 0) {
                  $css_file = substr($css_file, strlen(base_path()));
                }
                $files['#items'][$css_file] = array('data' => $css_file);
              }
              $files = _less_pre_render($files);
              
              $final_setting = array();
              foreach ($files['#items'] as $file) {
                if (isset($file['data'])) {
                  $final_settings[] = file_create_url($file['data']);
                }
              }
              
              $settings['data'][$key]['wysiwyg']['configs']['tinymce'][$profile]['content_css'] = implode(',', $final_settings);
            }
          }
        }
      }
    }
  }
}
?>
mstrelan’s picture

Status: Active » Needs review

I found a much cleaner approach, did not know hook_wysiwyg_editor_settings_alter() existed.

<?php
/**
 * Implements hook_wysiwyg_editor_settings_alter().
 * Check the css_content WYSIWYG setting for LESS files and replace with 
 * generated CSS files where necessary.
 */
function less_wysiwyg_editor_settings_alter(&$settings, &$context) {
  if (isset($settings['content_css'])) {
    $content_css = $settings['content_css'];
    $css_files = explode(',', $content_css);
    $files = array('#items' => array());
    foreach ($css_files as $css_file) {
      if (strpos($css_file, base_path()) === 0) {
        $css_file = substr($css_file, strlen(base_path()));
      }
      $files['#items'][$css_file] = array('data' => $css_file);
    }
    $files = _less_pre_render($files);
    $final_setting = array();
    foreach ($files['#items'] as $file) {
      if (isset($file['data'])) {
        $final_settings[] = file_create_url($file['data']);
      }
    }
    $settings['content_css'] = implode(',', $final_settings);    
  }
}
?>
lotyrin’s picture

Status: Needs review » Needs work

I like this approach. However, please provide a patch.

mstrelan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.56 KB
raincloud’s picture

I am also interested in this hook to be implemented in the LESS module. On my site, the code above is working well, hopefully it will be present in the next version.

corey.aufang’s picture

Ok, so it looks like different wysiwyg's show the css file list in a different format.

TinyMCE has $settings['content_css'] as a comma delimited string of file paths.

CKEditor has $settings['contentsCss'] which is an array of file paths.

openwysiwyg has $settings['CSSFile'] and it only allows one css file.

This opens the can of worms of multiple wysiwyg's for which support needs to be coded.

While the best solution would be for WYSIWYG to allow drupal to process the css files, which LESS would then act on, but that would require quite a change to WYSIWYG.

I agree that we need this to function, but I'm not sure if this is suited for this module, a bridge module, or if this should be something worked out on WYSIWYG's side of things.

raekjaer’s picture

StatusFileSize
new1.93 KB

I'm not a super PHP developer, but I have tried to make the suggested patch (nice approach) work with CKEditor as well as TinyMCE.

corey.aufang’s picture

I'm adding in support for tinymce, fckeditor, and ckeditor.

New dev soon.

corey.aufang’s picture

Please check out the latest beta.

If you are still finding problems, please retag this issue with the new version as all future development for D7 will be on the 7.x-3.x branch.

honza pobořil’s picture

Version: 7.x-2.x-dev » 7.x-3.0-beta1

Still does not work. Styles loads in head section in tinymce's iframe are same (loads uncompiled .less files).

corey.aufang’s picture

If you upgraded to 7.x-3.0-beta1 did you also update to the latest version of lessphp?

Anonymous’s picture

I can confirm this issue in TinyMCE using version 7.x-3.0-beta1

Anonymous’s picture

Never mind, now it appears to work just fine. Perhaps it was a caching issue.

corey.aufang’s picture

Version: 7.x-3.0-beta1 » 7.x-3.x-dev

There is now functionality in place to support the following WYSIWYGs:
tinymce
fckeditor
ckeditor

Those were the only editors that allowed for more than 1 file to be included in the editor's space.

The functionality for these is in less.wysiwyg.inc in the project, so if anyone else is able to get it working for other editors, please provide patches against this file.

corey.aufang’s picture

Status: Needs review » Fixed

This should be resolved in 7.x-3.0.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.