You are here

function less_wysiwyg_editor_settings_alter in Less CSS Preprocessor 7.4

Same name and namespace in other branches
  1. 8 includes/less.wysiwyg.inc \less_wysiwyg_editor_settings_alter()
  2. 7.2 less.wysiwyg.inc \less_wysiwyg_editor_settings_alter()
  3. 7.3 less.wysiwyg.inc \less_wysiwyg_editor_settings_alter()

Implements hook_wysiwyg_editor_settings_alter().

Check the CSS WYSIWYG setting for LESS files and replace with generated CSS files where necessary.

1 call to less_wysiwyg_editor_settings_alter()
less_ckeditor_settings_alter in includes/less.wysiwyg.inc
Implements hook_ckeditor_settings_alter().

File

includes/less.wysiwyg.inc, line 14
Contains functions that handle WYSIWYG module integration.

Code

function less_wysiwyg_editor_settings_alter(&$settings, $context) {
  $wysiwyg = $context['editor']['name'];

  // Each editor has a different $settings array key for CSS files.
  $editors = array(
    'tinymce' => 'content_css',
    'fckeditor' => 'EditorAreaCSS',
    'ckeditor' => 'contentsCss',
  );
  if (!empty($editors[$wysiwyg]) && !empty($settings[$editors[$wysiwyg]])) {
    $stylesheets = $settings[$editors[$wysiwyg]];

    // Keep track if comma separated paths, or array of paths.
    $is_array = is_array($stylesheets);

    // $stylesheets is an array or comma separated list of file paths.
    $stylesheets = $is_array ? $stylesheets : explode(',', $stylesheets);

    // Create a 'value => value' array to preserve original stylesheet order.
    $stylesheets = drupal_map_assoc($stylesheets);

    // Do not attempt to process remote or regular CSS files.
    $local_stylesheets = array_filter(array_map('parse_url', $stylesheets), function ($value) {
      return empty($value['host']) && preg_match('/.*\\.less$/i', $value['path']) == 1;
    });

    // Prepare an array that can be handled by normal LESS module processing.
    $items = array_map(function ($stylesheet) {
      return array(
        'data' => trim($stylesheet['path'], '/'),
      );
    }, $local_stylesheets);
    $processed_styles = _less_pre_render(array(
      '#items' => $items,
    ));

    // Compiled path is in 'data' index of each sheet item.
    $compiled_sheets = array_map(function ($value) {
      return $value['data'];
    }, $processed_styles['#items']);

    // WYSIWYGs can't handle Drupal public:// paths, so get an accessible URL.
    $compiled_sheets = array_map('file_create_url', $compiled_sheets);

    // Replace paths to compiled output paths based on key (path) of original file.
    $processed_stylesheets = array_replace($stylesheets, $compiled_sheets);

    // Need a integer indexed array so JSON encoding doesn't attempt to make a object.
    $processed_stylesheets = array_values($processed_stylesheets);

    // Recombine file paths into comma separated list.
    $processed_stylesheets = $is_array ? $processed_stylesheets : implode(',', $processed_stylesheets);
    $settings[$editors[$wysiwyg]] = $processed_stylesheets;
  }
}