You are here

function tinymce_process_textarea in TinyMCE 5

Same name and namespace in other branches
  1. 5.2 tinymce.module \tinymce_process_textarea()
  2. 6.2 tinymce.module \tinymce_process_textarea()
  3. 6 tinymce.module \tinymce_process_textarea()

Attach tinymce to a textarea

File

./tinymce.module, line 75
Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.

Code

function tinymce_process_textarea($element) {
  static $is_running = FALSE;
  global $user;
  static $profile_name;

  //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
  $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);

  // Since tinymce_config() makes a db hit, only call it when we're pretty sure
  // we're gonna render tinymce.
  if (!$profile_name) {
    $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($user->roles))));
    if (!$profile_name) {
      return $element;
    }
  }
  $profile = tinymce_profile_load($profile_name);
  $init = tinymce_config($profile);
  $init['elements'] = 'edit-' . $textarea_name;
  if (_tinymce_page_match($profile)) {

    // Merge user-defined TinyMCE settings.
    $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);

    // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
    if (count($init) < 1) {
      return $element;
    }
    $settings = array();
    foreach ($init as $k => $v) {
      $v = is_array($v) ? implode(',', $v) : $v;

      // Don't wrap the JS init in quotes for boolean values or functions.
      if (strtolower($v) != 'true' && strtolower($v) != 'false' && $v[0] != '{') {
        $v = '"' . $v . '"';
      }
      $settings[] = $k . ' : ' . $v;
    }
    $tinymce_settings = implode(",\n    ", $settings);
    $enable = t('enable rich-text');
    $disable = t('disable rich-text');
    $tinymce_invoke = <<<EOD

  tinyMCE.init({
    {<span class="php-variable">$tinymce_settings</span>}
  });

EOD;
    $tinymce_gz_invoke = <<<EOD

  tinyMCE_GZ.init({
    {<span class="php-variable">$tinymce_settings</span>}
  });

EOD;
    $js_toggle = <<<EOD

  function mceToggle(id, linkid) {
    element = document.getElementById(id);
    link = document.getElementById(linkid);
    img_assist = document.getElementById('img_assist-link-'+ id);

    if (tinyMCE.getEditorId(element.id) == null) {
      tinyMCE.addMCEControl(element, element.id);
      element.togg = 'on';
      link.innerHTML = '{<span class="php-variable">$disable</span>}';
      link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
      if (img_assist)
        img_assist.innerHTML = '';
      link.blur();
    }
    else {
      tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.id));
      element.togg = 'off';
      link.innerHTML = '{<span class="php-variable">$enable</span>}';
      link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
      if (img_assist)
        img_assist.innerHTML = img_assist_default_link;
      link.blur();
    }
  }

EOD;
    $status = tinymce_user_get_status($user, $profile);

    // note we test for string == true because we save our settings as strings
    $link_text = $status == 'true' ? $disable : $enable;
    $img_assist_link = $status == 'true' ? 'yes' : 'no';
    $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
    $wysiwyg_link = <<<EOD
<script type="text/javascript">
  img_assist = document.getElementById('img_assist-link-edit-{<span class="php-variable">$textarea_name</span>}');
  if (img_assist) {
    var img_assist_default_link = img_assist.innerHTML;
    if ('{<span class="php-variable">$img_assist_link</span>}' == 'yes') {
      img_assist.innerHTML = tinyMCE.getEditorId('edit-{<span class="php-variable">$textarea_name</span>}') == null ? '' : img_assist_default_link;
    }
    else {
      img_assist.innerHTML = tinyMCE.getEditorId('edit-{<span class="php-variable">$textarea_name</span>}') == null ? img_assist_default_link : '';
    }
  }
  if (typeof(document.execCommand) == 'undefined') {
    img_assist.innerHTML = img_assist_default_link;
    document.write('<div style="font-size:x-small">{<span class="php-variable">$no_wysiwyg</span>}</div>');
  }
  else {
    document.write("<div><a href=\\"javascript:mceToggle('edit-{<span class="php-variable">$textarea_name</span>}', 'wysiwyg4{<span class="php-variable">$textarea_name</span>}');\\" id=\\"wysiwyg4{<span class="php-variable">$textarea_name</span>}\\">{<span class="php-variable">$link_text</span>}</a></div>");
  }
</script>
EOD;

    // We only load the TinyMCE js file once per request
    if (!$is_running) {
      $is_running = TRUE;
      $tinymce_mod_path = drupal_get_path('module', 'tinymce');
      if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager')) {

        // if tinymce imagemanager is installed
        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/jscripts/mcimagemanager.js');
      }
      if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager')) {

        // if tinymce filemanager is installed
        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/jscripts/mcfilemanager.js');
      }

      // TinyMCE Compressor 1.0.9 and greater
      if (file_exists($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.js')) {
        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.js');
        drupal_add_js($tinymce_gz_invoke, 'inline');
      }
      elseif (file_exists($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
      }
      else {

        // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
        drupal_add_js($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/tiny_mce.js');
      }
      drupal_add_js($js_toggle, 'inline');

      // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
      drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
    }

    // Load a TinyMCE init for each textarea.
    if ($init) {
      drupal_add_js($tinymce_invoke, 'inline');
    }

    //settings are saved as strings, not booleans
    if ($profile->settings['show_toggle'] == 'true') {

      // Make sure to append to #suffix so it isn't completely overwritten
      $element['#suffix'] .= $wysiwyg_link;
    }

    // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
    $element['#resizable'] = FALSE;
  }
  return $element;
}