function better_formats_filter_process_format

cis7 better_formats.module better_formats_filter_process_format($element)
cle7 better_formats.module better_formats_filter_process_format($element)
elmsmedia7 better_formats.module better_formats_filter_process_format($element)
icor7 better_formats.module better_formats_filter_process_format($element)
meedjum_blog7 better_formats.module better_formats_filter_process_format($element)
mooc7 better_formats.module better_formats_filter_process_format($element)

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 sites/all/modules/ulmus/better_formats/better_formats.module
Implements of hook_element_info_alter().

File

sites/all/modules/ulmus/better_formats/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;
        $element['format']['format']['#default_value'] = array_shift(array_keys($options));
      }
    }

    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;
      }
      // Otherwise, if the current default format is no longer one of the
      // allowed options, a new default format must be assigned.
      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);
        }
        // Otherwise, it is unsafe to automatically assign a new default format
        // (since this will display the content in a way that was not
        // originally intended and might be dangerous, e.g. if the content
        // contains an attempted XSS attack). A human must explicitly decide
        // which new format to assign, so we force the field to be required but
        // with no default value, similar to what filter_process_format() does.
        // Although filter_process_format() limits this functionality to users
        // with the 'administer filters' permission, we can allow it for any
        // user here since we know that the user already has permission to use
        // the current format; thus, there is no danger of exposing unformatted
        // text (for example, raw PHP code) that they are otherwise not allowed
        // to see.
        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;
}