You are here

media_ckeditor.module in Media CKEditor 7.2

Same filename and directory in other branches
  1. 7 media_ckeditor.module

Primarily Drupal hooks.

File

media_ckeditor.module
View source
<?php

/**
 * @file
 * Primarily Drupal hooks.
 */

/**
 * Implements hook_element_info_alter().
 */
function media_ckeditor_element_info_alter(&$types) {
  $types['text_format']['#pre_render'][] = 'media_ckeditor_pre_render_text_format';
  $types['media']['#pre_render'][] = 'media_ckeditor_pre_render_media';
}

/**
 * Adds CKEditor-specific JavaScript.
 */
function media_ckeditor_pre_render_text_format($element) {

  // filter_process_format() copies properties to the expanded 'value' child
  // element.
  if (!isset($element['format'])) {
    return $element;
  }
  $field =& $element['value'];
  $settings = array(
    'field' => $field['#id'],
  );
  if (!isset($field['#value'])) {
    return $element;
  }

  // Add CKEditor-specific JS.
  $element['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'media_ckeditor') . '/js/plugins/media/library.js',
    'type' => 'file',
    'scope' => 'footer',
    // It's important that this javascript fire after CKEditor.
    'weight' => 20,
  );

  // Communicate to javascript whether we will be displaying fully rendered
  // file entities in the WYSIWYG.
  $element['#attached']['js'][] = array(
    'data' => array(
      'media_ckeditor' => array(
        'fully_rendered_files' => _media_ckeditor_fully_rendered_files_in_wysiwyg(),
        'labels' => array(
          'settings' => t('Media settings'),
          'add' => t('Add media'),
        ),
      ),
    ),
    'type' => 'setting',
  );
  return $element;
}

/**
 * Implements hook_form_ID_alter().
 */
function media_ckeditor_form_media_wysiwyg_format_form_alter(&$form, $form_state) {

  // Add our overrides to the media format form javascript.
  $form['#attached']['js'][] = drupal_get_path('module', 'media_ckeditor') . '/js/media_ckeditor.format_form.js';
}

/**
 * Implements hook_media_browser_params_alter().
 */
function media_ckeditor_media_browser_params_alter(&$stored_params) {

  // We add this custom param when the media dialog is invoked in library.js.
  if (isset($stored_params['id']) && $stored_params['id'] == 'media_wysiwyg') {

    // Set the default browser params from settings form if not already set.
    if (empty($stored_params['enabledPlugins'])) {
      $stored_params['enabledPlugins'] = variable_get('media_wysiwyg_wysiwyg_browser_plugins', array());
    }
    if (empty($stored_params['file_directory'])) {
      $stored_params['file_directory'] = variable_get('media_wysiwyg_wysiwyg_upload_directory', '');
    }
    if (empty($stored_params['types'])) {
      $stored_params['types'] = variable_get('media_wysiwyg_wysiwyg_allowed_types', array(
        'audio',
        'image',
        'video',
        'document',
      ));
    }
    if (empty($stored_params['file_extensions'])) {
      $stored_params['file_extensions'] = variable_get('media_wysiwyg_wysiwyg_allowed_file_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function media_ckeditor_form_field_ui_field_edit_form_alter(&$form, &$form_state) {

  // Add a checkbox that marks this field as needing an "Insert" button.
  if (!empty($form['#instance']['widget']['module'])) {
    if ('media' === $form['#instance']['widget']['module']) {
      $form['instance']['settings']['wysiwyg_insert'] = array(
        '#type' => 'checkbox',
        '#title' => t('Show button for inserting files into WYSIWYG editor'),
        '#description' => t('If checked, then an "Insert" button will be available to put attached files into the active WYSIWYG editor.'),
        '#default_value' => isset($form['#instance']['settings']['wysiwyg_insert']) ? $form['#instance']['settings']['wysiwyg_insert'] : FALSE,
      );
    }
  }
}

/**
 * Pre-render callback for media elements.
 */
function media_ckeditor_pre_render_media($element) {

  // If the media widget has been configured to have an Insert button, we'll
  // communicate that information through javascript settings.
  if (isset($element['#entity_type']) && isset($element['#field_name']) && isset($element['#bundle'])) {
    $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  }
  else {
    $instance = FALSE;
  }

  // Build JavaScript data to be attached to the element.
  $data['media_ckeditor']['image_properties'] = variable_get('media_ckeditor_image_properties', FALSE);
  if (!empty($instance['settings']['wysiwyg_insert'])) {
    $data['media_ckeditor']['wysiwyg_insert'][$element['#field_name']] = TRUE;
  }
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => $data,
  );
  return $element;
}

/**
 * Helper function to tell whether we should render full entities in WYSIWYGs.
 */
function _media_ckeditor_fully_rendered_files_in_wysiwyg() {
  if (variable_get('media_ckeditor_fully_rendered_files', FALSE) && user_access('view overridden file entities in wysiwyg')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_menu().
 */
function media_ckeditor_menu() {
  $items = array();

  // Add our AJAX-friendly route for rendering the file entities for use in
  // the WYSIWYG editor.
  $items['media/rendered-in-wysiwyg'] = array(
    'page callback' => 'media_ckeditor_rendered_file_in_wysiwyg',
    'access callback' => '_media_ckeditor_fully_rendered_files_in_wysiwyg',
    'file' => 'includes/media_ckeditor.pages.inc',
    'theme callback' => 'ajax_base_page_theme',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * A permission to allow users to view overridden files in the WYSIWYG.
 */
function media_ckeditor_permission() {
  return array(
    'view overridden file entities in wysiwyg' => array(
      'title' => t('View overridden file entities in wysiwyg'),
      'description' => t('When users are embedding files in WYSIWYGs, the file entities will be displayed with whatever overrides the user may have specified in the Media popup.'),
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function media_ckeditor_form_media_admin_config_browser_alter(&$form, &$form_state) {

  // Add our optional settings to the Media Browser admin form.
  $form['media_ckeditor'] = array(
    '#type' => 'fieldset',
    '#title' => t('Ckeditor WYSIWYG configuration'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 50,
  );
  $form['media_ckeditor']['media_ckeditor_fully_rendered_files'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display fully rendered files in WYSIWYG'),
    '#description' => t('Check this box to render files in the WYSIWYG dynamically, to reflect any field overrides the user may have specified. Note that this requires the user has the corresponding permission: "View overridden file entities in wysiwyg"'),
    '#default_value' => variable_get('media_ckeditor_fully_rendered_files', FALSE),
  );
  $form['media_ckeditor']['media_ckeditor_image_properties'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow Image Properties configuration'),
    '#description' => t('Check this box to allow images inserted with the Media module to be resized and edited using the Image Properties menu option. Otherwise, resizing options should be provided as different image view modes.'),
    '#default_value' => variable_get('media_ckeditor_image_properties', FALSE),
  );
}

/**
 * Implements hook_media_wysiwyg_token_to_markup_alter().
 */
function media_ckeditor_media_wysiwyg_token_to_markup_alter(&$element, $tag_info, $settings) {

  // Check to see if files embedded in WYSIWYG should be fully rendered. If so,
  // replace the simpler Media placeholder with a fully rendered file entity.
  if (!empty($settings['wysiwyg']) && _media_ckeditor_fully_rendered_files_in_wysiwyg()) {
    $file = $tag_info['file'];
    $file->override['wysiwyg'] = TRUE;
    $view_mode = $tag_info['view_mode'];
    $fields = !empty($settings['fields']) ? $settings['fields'] : array();
    $element = media_ckeditor_render_file_with_overrides($file, $view_mode, $fields);
  }
}

/**
 * Helper function to get a fully rendered file entity with optional overrides.
 */
function media_ckeditor_render_file_with_overrides($file, $view_mode, $fields = array()) {
  foreach ($fields as $field_name => $value) {
    if (isset($file->{$field_name})) {
      $file->{$field_name} = $value;
    }
  }
  $file_array = array(
    $file->fid => $file,
  );
  $build = file_entity_metadata_view_file($file_array, $view_mode);

  // Manually remove contextual links.
  if (!empty($build['file'][$file->fid]['#contextual_links'])) {
    unset($build['file'][$file->fid]['#contextual_links']);
  }
  return $build;
}

/**
 * Implements hook_preprocess_file_entity().
 */
function media_ckeditor_preprocess_file_entity(&$vars) {

  // If we are rendering this as a fully rendered file entity in a WYSIWYG,
  // set $page to TRUE so that the title doesn't display.
  // @see file_entity/file_entity.tpl.php.
  if (!empty($vars['override']['wysiwyg']) && _media_ckeditor_fully_rendered_files_in_wysiwyg()) {
    $vars['page'] = TRUE;
  }
}

Functions

Namesort descending Description
media_ckeditor_element_info_alter Implements hook_element_info_alter().
media_ckeditor_form_field_ui_field_edit_form_alter Implements hook_form_FORM_ID_alter().
media_ckeditor_form_media_admin_config_browser_alter Implements hook_form_FORM_ID_alter().
media_ckeditor_form_media_wysiwyg_format_form_alter Implements hook_form_ID_alter().
media_ckeditor_media_browser_params_alter Implements hook_media_browser_params_alter().
media_ckeditor_media_wysiwyg_token_to_markup_alter Implements hook_media_wysiwyg_token_to_markup_alter().
media_ckeditor_menu Implements hook_menu().
media_ckeditor_permission A permission to allow users to view overridden files in the WYSIWYG.
media_ckeditor_preprocess_file_entity Implements hook_preprocess_file_entity().
media_ckeditor_pre_render_media Pre-render callback for media elements.
media_ckeditor_pre_render_text_format Adds CKEditor-specific JavaScript.
media_ckeditor_render_file_with_overrides Helper function to get a fully rendered file entity with optional overrides.
_media_ckeditor_fully_rendered_files_in_wysiwyg Helper function to tell whether we should render full entities in WYSIWYGs.