You are here

function linkit_pre_render_editor_element in Linkit 7.3

Add Linkit settings to pages where we have an editor element.

1 string reference to 'linkit_pre_render_editor_element'
linkit_element_info_alter in ./linkit.module
Implements hook_element_info_alter().

File

./linkit.module, line 1086
Main file for Linkit module.

Code

function linkit_pre_render_editor_element($element) {
  global $user;

  // Attach the linkit_base library.
  $element['#attached']['library'][] = array(
    'linkit',
    'base',
  );
  $all_formats = array_keys(filter_formats($user));
  $all_profiles = linkit_profile_editor_load_all();
  $profiles_formats = array();
  $use_imce = FALSE;

  // Initialize the list of profiles formats.
  foreach ($all_formats as $format) {
    $profiles_formats[$format] = array(
      'profile' => NULL,
      'enabled_profiles' => array(),
    );
  }

  // First we need to figure out which formats that the current user can access
  // have active Linkit profiles for them.
  foreach ($all_profiles as $profile) {
    if (empty($profile->disabled) && is_array($profile->data['text_formats'])) {
      foreach (array_filter($profile->data['text_formats']) as $text_format) {
        if (in_array($text_format, $all_formats)) {
          if (empty($profiles_formats[$text_format]['profile'])) {
            $profiles_formats[$text_format]['profile'] = $profile->name;
          }
          $profiles_formats[$text_format]['enabled_profiles'][$profile->name] = TRUE;
          if (isset($profile->data['imce']) && $profile->data['imce'] == 1) {
            $use_imce = TRUE;
          }
        }
      }
    }
  }
  if (!empty($profiles_formats)) {
    $field_js = array(
      'data' => array(
        'linkit' => array(
          'formats' => $profiles_formats,
        ),
      ),
      'type' => 'setting',
    );

    // Configure Linkit to have an IMCE selector.
    if (module_invoke('imce', 'access') && $use_imce) {
      $field_js['data']['linkit']['IMCEurl'] = url('imce', array(
        'query' => array(
          'app' => 'Linkit|sendto@Drupal.linkit.IMCECallback',
          'absolute' => TRUE,
        ),
      ));

      // We will only serv public files with IMCE.
      $field_js['data']['linkit']['publicFilesDirectory'] = variable_get('file_public_path', conf_path() . '/files');
    }
    $element['#attached']['js'][] = $field_js;
    if (isset($element['#format']) && isset($element['#pre_render'])) {
      if (in_array('ckeditor_pre_render_text_format', $element['#pre_render'])) {
        foreach ($profiles_formats as $format => $profile) {
          if ($format == $element['#format']) {

            // Attach the linkit_ckeditor library.
            $element['#attached']['library'][] = array(
              'linkit',
              'ckeditor',
            );
            break;
          }
        }
      }
    }
  }
  return $element;
}