You are here

function better_formats_filter_process_format in Better Formats 7

Same name and namespace in other branches
  1. 8 better_formats.module \better_formats_filter_process_format()

Process callback for form elements that have a text format selector attached.

This callback runs after filter_process_format() and performs additional modifications to the form element.

See also

filter_process_format()

1 string reference to 'better_formats_filter_process_format'
better_formats_element_info_alter in ./better_formats.module
Implements of hook_element_info_alter().

File

./better_formats.module, line 82
Enhances Drupal's core text format settings.

Code

function better_formats_filter_process_format($element) {

  // Before we make any modifications to the element, record whether or not
  // filter_process_format() has determined that (for security reasons) the
  // user is not allowed to make any changes to this field. (This will happen
  // if the user does not have permission to use the currently-assigned text
  // format.)
  $access_denied_for_security = isset($element['format']['#access']) && !$element['format']['#access'];

  // Now hide several parts of the element for cosmetic reasons (depending on
  // the permissions of the current user).
  $show_selection = TRUE;
  if (isset($element['#entity_type'])) {
    $show_selection = user_access('show format selection for ' . $element['#entity_type']);
  }
  $show_tips = user_access('show format tips');
  $show_tips_link = user_access('show more format tips link');
  if (!$show_selection) {
    $element['format']['format']['#access'] = FALSE;
  }
  if (!$show_tips) {
    $element['format']['guidelines']['#access'] = FALSE;
  }
  if (!$show_tips_link) {
    $element['format']['help']['#access'] = FALSE;
  }

  // If the element represents a field attached to an entity, we may need to
  // adjust the allowed text format options. However, we don't want to touch
  // this if filter_process_format() has determined that (for security reasons)
  // the user is not allowed to make any changes; in that case, Drupal core
  // will hide the format selector and force the field to be saved with its
  // current values, and we should not do anything to alter that process.
  if (isset($element['#entity_type']) && !$access_denied_for_security) {
    $instance_info = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
    $bf = isset($instance_info['settings']['better_formats']) ? $instance_info['settings']['better_formats'] : NULL;

    // Need to only do this on create forms.
    if (!empty($element['#entity']) && !empty($element['#entity_type'])) {
      list($eid, $vid, $bundle) = entity_extract_ids($element['#entity_type'], $element['#entity']);
      if (empty($eid) && isset($bf) && !empty($bf['default_order_toggle']) && !empty($bf['default_order_wrapper']['formats'])) {
        $order = $bf['default_order_wrapper']['formats'];
        uasort($order, 'better_formats_text_format_sort');
        $options = array();
        foreach ($order as $id => $weight) {
          if (isset($element['format']['format']['#options'][$id])) {
            $options[$id] = $element['format']['format']['#options'][$id];
          }
        }
        $element['format']['format']['#options'] = $options;
        $options_keys = array_keys($options);
        $element['format']['format']['#default_value'] = array_shift($options_keys);
      }
    }
    if (isset($bf) && !empty($bf['allowed_formats_toggle']) && !empty($bf['allowed_formats'])) {

      // Filter the list of available formats to those allowed on this field.
      $allowed_fields = array_filter($bf['allowed_formats']);
      $options =& $element['format']['format']['#options'];
      $options = array_intersect_key($options, $allowed_fields);

      // If there is only one allowed format, deny access to the text format
      // selector for cosmetic reasons, just like filter_process_format() does.
      if (count($options) == 1) {
        $element['format']['format']['#access'] = FALSE;
        $show_selection = FALSE;
      }

      // If there are no allowed formats, we need to deny access to the entire
      // field, since it doesn't make sense to add or edit content that does
      // not have a text format.
      if (empty($options)) {
        $element['#access'] = FALSE;
      }
      elseif (!isset($options[$element['format']['format']['#default_value']])) {

        // If there is no text in the field, it is safe to automatically assign
        // a new default format. We pick the first available option to be
        // consistent with what filter_default_format() does.
        if (!isset($element['value']['#default_value']) || $element['value']['#default_value'] === '') {
          $formats = array_keys($options);
          $element['format']['format']['#default_value'] = reset($formats);
        }
        else {
          $element['format']['format']['#required'] = TRUE;
          $element['format']['format']['#default_value'] = NULL;

          // Force access to the format selector (it may have been denied
          // previously for cosmetic reasons).
          $element['format']['#access'] = TRUE;
          $element['format']['format']['#access'] = TRUE;
        }
      }
    }
  }

  // If the user is not supposed to see the text format selector, hide all
  // guidelines except those associated with the default format. We need to do
  // this at the end, since the above code may have altered the default format.
  if (!$show_selection && isset($element['format']['format']['#default_value'])) {
    foreach (element_children($element['format']['guidelines']) as $format) {
      if ($format != $element['format']['format']['#default_value']) {
        $element['format']['guidelines'][$format]['#access'] = FALSE;
      }
    }
  }

  // Hide the entire text format fieldset if the user is not supposed to see
  // anything inside it.
  if (!$show_selection && !$show_tips && !$show_tips_link) {
    $element['format']['#type'] = 'container';
  }
  return $element;
}