<?php

/**
 * @file
 * Provides the ability to edit content using a WYSIWYG editor.
 */

/**
 * Implements hook_element_info_alter().
 *
 * Simplify the content editing UI by removing filter format selection and tips.
 */
function commons_wysiwyg_element_info_alter(&$type) {
  $type['text_format']['#pre_render'][] = 'commons_wysiwyg_process_filter_format';
}

/**
 * Callback function to remove format selection and tips from text_formats.
 *
 * @see http://drupal.org/node/1949552
 */
function commons_wysiwyg_process_filter_format($element) {
  // Verify that the element has a filter format.
  if (!isset($element['format'])) {
    return $element;
  }

  $field = &$element['value'];

  // Verify that the element can contain a value.
  if (!isset($field['#value'])) {
    return $element;
  }

  global $user;
  $account = $user;

  // Privileged users should be able to view and modify filter formats.
  if (user_access('administer filters', $account)) {
    return $element;
  }

  // Determine which filter formats a user has access to.
  $formats = filter_formats($account);

  // If the user is given a choice between formats, default to filtered_html if
  // it is available.
  if (count($formats) >= 2 && array_key_exists('filtered_html', $formats)) {
    $element['format']['#access'] = FALSE;
    $element['#format'] = 'filtered_html';
  }

  return $element;
}
