You are here

function ckeditor_load_by_field in CKEditor - WYSIWYG HTML editor 7

Load CKEditor for field ID

Parameters

object $field:

string $format:

Return value

object

1 call to ckeditor_load_by_field()
ckeditor_pre_render_text_format in ./ckeditor.module
This function creates the HTML objects required for CKEditor.

File

includes/ckeditor.lib.inc, line 1246
CKEditor - The text editor for the Internet - http://ckeditor.com Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.

Code

function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE) {
  global $user, $theme;
  static $processed_ids = array();
  static $is_running = FALSE;
  $use_ckeditor = FALSE;
  $format_arr = FALSE;
  $suffix = '';
  if (is_array($format)) {
    $format_arr = $format;
    $format = isset($format_arr['#value']) ? $format_arr['#value'] : $format_arr['#default_value'];
  }
  if (!isset($field['#id'])) {
    return $field;
  }
  if (isset($processed_ids[$field['#id']])) {
    return $field;
  }
  if (key_exists('#wysiwyg', $field) && !$field['#wysiwyg']) {
    return $field;
  }
  if (isset($field['#access']) && !$field['#access']) {
    return $field;
  }
  if ($field['#id'] == "edit-log") {
    return $field;
  }
  if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') {
    return $field;
  }

  // Pass Drupal's JS cache-busting string via settings along to CKEditor.
  drupal_add_js(array(
    'ckeditor' => array(
      'textarea_default_format' => array(
        $field['#id'] => $format,
      ),
      'timestamp' => variable_get('css_js_query_string', '0'),
    ),
  ), 'setting');
  if (!isset($processed_ids[$field['#id']])) {
    $processed_ids[$field['#id']] = array();
  }
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
  $profile = ckeditor_get_profile($format);
  $host = base_path();
  if ($profile === FALSE) {
    $ckeditor_in_default_format = FALSE;
    foreach ((array) $format_arr['#options'] as $key => $val) {
      if ($key == $format) {
        continue;
      }
      if ($profile = ckeditor_get_profile($key)) {
        $use_ckeditor = $key;
        break;
      }
    }
    if ($use_ckeditor === FALSE) {
      return $field;
    }
  }
  else {
    $ckeditor_in_default_format = TRUE;
  }
  if ($settings = ckeditor_profiles_compile($format)) {
    $ckeditor_on = $settings['default'] == 't' && $profile->settings['default'] == 't' ? TRUE : FALSE;
  }
  elseif ($settings = ckeditor_profiles_compile($use_ckeditor)) {
    $ckeditor_on = FALSE;
  }
  else {
    return $field;
  }

  // Attach the editor css.
  $field['#attached']['css'][] = drupal_get_path('module', 'ckeditor') . '/css/ckeditor.editor.css';
  if ($settings) {
    $textarea_id = $field['#id'];
    $class[] = 'ckeditor-mod';
    $_ckeditor_ids[] = $textarea_id;

    //settings are saved as strings, not booleans
    if ($settings['show_toggle'] == 't' && $show_toggle) {
      if ($add_fields_to_toggle !== FALSE) {
        if (is_array($add_fields_to_toggle)) {
          $toggle_fields = "['" . $textarea_id . "','" . implode("','", $add_fields_to_toggle) . "']";
        }
        else {
          $toggle_fields = "['" . $textarea_id . "','" . $add_fields_to_toggle . "']";
        }
      }
      else {
        $toggle_fields = "['{$textarea_id}']";
      }
      $wysiwyg_link = '';
      $wysiwyg_link .= "<a class=\"ckeditor_links\" style=\"display:none\" href=\"javascript:void(0);\" onclick=\"javascript:Drupal.ckeditorToggle({$toggle_fields},'" . str_replace("'", "\\'", t('Switch to plain text editor')) . "','" . str_replace("'", "\\'", t('Switch to rich text editor')) . "');\" id=\"switch_{$textarea_id}\">";
      $wysiwyg_link .= $ckeditor_on ? t('Switch to plain text editor') : t('Switch to rich text editor');
      $wysiwyg_link .= '</a>';

      // Make sure to append to #suffix so it isn't completely overwritten
      $suffix .= $wysiwyg_link;
    }
    $editor_local_path = ckeditor_path('local');
    $editor_url_path = ckeditor_path('url');
    if (!$is_running) {

      // By default sessions are not started automatically for anonymous users.
      // Start one for editing content so that we had a consistent token that is used in XSS filter.
      if (isset($field['#entity']) && !empty($field['#entity']->created) && empty($user->uid)) {
        drupal_session_start();
        $_SESSION['ckeditor_anonymous_user'] = true;
        drupal_page_is_cacheable(FALSE);
      }
      if (!$ckeditor_in_default_format) {
        $load_method = 'ckeditor_basic.js';
        $load_time_out = 0;
      }
      elseif (isset($profile->settings['ckeditor_load_method'])) {
        $load_method = $profile->settings['ckeditor_load_method'];
        $load_time_out = $profile->settings['ckeditor_load_time_out'];
      }
      if ($editor_local_path != '<URL>') {
        drupal_add_js('window.CKEDITOR_BASEPATH = "' . ckeditor_path('relative') . '/"', array(
          'type' => 'inline',
          'weight' => -100,
        ));
      }
      drupal_add_js(ckeditor_module_path('url') . '/includes/ckeditor.utils.js', array(
        'type' => 'file',
        'scope' => 'footer',
      ));
      $preprocess = FALSE;
      if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
        $preprocess = TRUE;
      }
      if ($editor_local_path == '<URL>') {
        drupal_add_js($editor_url_path . '/ckeditor.js', array(
          'type' => 'external',
          'scope' => 'footer',
        ));
      }
      else {
        if (isset($load_method) && file_exists($editor_local_path . '/' . $load_method)) {
          drupal_add_js($editor_url_path . '/' . $load_method, array(
            'type' => 'file',
            'scope' => 'footer',
            'preprocess' => $preprocess,
          ));
          if ($load_method == 'ckeditor_basic.js') {
            drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $load_time_out . ';', array(
              'type' => 'inline',
              'scope' => 'footer',
            ));
            drupal_add_js(array(
              'ckeditor' => array(
                'load_timeout' => TRUE,
              ),
            ), 'setting');
          }
        }
        else {
          drupal_add_js($editor_url_path . '/ckeditor.js', array(
            'type' => 'file',
            'scope' => 'footer',
            'preprocess' => $preprocess,
          ));
        }
      }
      $ckeditor_url = ckeditor_path('relative');
      if ($ckeditor_url == '<URL>') {
        $ckeditor_url = ckeditor_path('url');
      }
      $ckeditor_url .= '/';
      drupal_add_js(array(
        'ckeditor' => array(
          'module_path' => ckeditor_module_path('relative'),
          'editor_path' => $ckeditor_url,
        ),
      ), 'setting');
      if (module_exists('paging')) {
        drupal_add_js(array(
          'ckeditor' => array(
            'pagebreak' => TRUE,
          ),
        ), 'setting');
      }
      if (module_exists('linktocontent_node')) {
        drupal_add_js(array(
          'ckeditor' => array(
            'linktocontent_node' => TRUE,
          ),
        ), 'setting');
      }
      if (module_exists('linktocontent_menu')) {
        drupal_add_js(array(
          'ckeditor' => array(
            'linktocontent_menu' => TRUE,
          ),
        ), 'setting');
      }
      if (module_exists('pagebreak')) {
        drupal_add_js(array(
          'ckeditor' => array(
            'pagebreak' => TRUE,
          ),
        ), 'setting');
      }
      if (module_exists('smart_paging')) {
        drupal_add_js(array(
          'ckeditor' => array(
            'pagebreak' => TRUE,
          ),
        ), 'setting');
      }
      drupal_add_js(array(
        'ckeditor' => array(
          'ajaxToken' => drupal_get_token('ckeditorAjaxCall'),
          'xss_url' => url('ckeditor/xss'),
        ),
      ), 'setting');
      $is_running = TRUE;
    }
    drupal_add_js(array(
      'ckeditor' => array(
        'theme' => $theme,
      ),
    ), 'setting');
    if (!empty($settings)) {
      drupal_add_js(array(
        'ckeditor' => array(
          'elements' => array(
            $textarea_id => $format,
          ),
        ),
      ), 'setting');
    }
    if (!empty($ckeditor_on)) {
      drupal_add_js(array(
        'ckeditor' => array(
          'autostart' => array(
            $textarea_id => $ckeditor_on,
          ),
        ),
      ), 'setting');
    }

    //[#1473010]
    if (isset($settings['scayt_sLang'])) {
      drupal_add_js(array(
        'ckeditor' => array(
          'scayt_language' => $settings['scayt_sLang'],
        ),
      ), 'setting');
    }
    elseif (!empty($field["#language"]) && $field["#language"] != LANGUAGE_NONE) {
      drupal_add_js(array(
        'ckeditor' => array(
          'scayt_language' => ckeditor_scayt_langcode($field["#language"]),
        ),
      ), 'setting');
    }

    // Pass Drupal's JS cache-busting string via settings along to CKEditor.
    drupal_add_js(array(
      'ckeditor' => array(
        'timestamp' => variable_get('css_js_query_string', '0'),
      ),
    ), 'setting');

    // Remember extra information and reuse it during "Preview"
    $processed_ids[$field['#id']]['suffix'] = $suffix;
    $processed_ids[$field['#id']]['class'] = $class;
    if (empty($field['#suffix'])) {
      $field['#suffix'] = $suffix;
    }
    else {
      $field['#suffix'] .= $suffix;
    }
    if (empty($field['#attributes']['class'])) {
      $field['#attributes']['class'] = $class;
    }
    else {
      $field['#attributes']['class'] = array_merge($field['#attributes']['class'], $class);
    }
  }
  return $field;
}