You are here

function asset_widget_pre_render_text_format in Asset 7

Text format element pre_render function.

1 string reference to 'asset_widget_pre_render_text_format'
asset_widget_element_info_alter in modules/asset_widget/asset_widget.module
Implements hook_element_info_alter().

File

modules/asset_widget/asset_widget.module, line 165
Code for the Asset widget module.

Code

function asset_widget_pre_render_text_format($element) {

  // Cache attaching by element id.
  static $attached = array();
  if (empty($attached[$element['#id']])) {
    global $user;

    // If $element['#format'] is empty, it means that field don't have input formats enabled.
    if (!empty($element['#format'])) {
      if (!empty($element['format']['format']['#options'])) {

        // Cache allowed formats in one element. Not static, because drupal_add_js will write it properly.
        $formats_asset_plugin = array();
        $classes_added = FALSE;
        static $additional_classes = array();
        foreach ($element['format']['format']['#options'] as $format => $name) {

          // Skip plain text.
          if ($format == 'plain_text') {
            continue;
          }

          // Load format.
          if ($format = filter_format_load($format)) {

            // Check user access.
            $allowed_roles = filter_get_roles_by_format($format);
            foreach ($user->roles as $id => $role) {
              if (!empty($allowed_roles[$id])) {

                // Check that asset plugin is enabled.
                module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
                if ($ckeditor_profile = ckeditor_get_profile($format->format)) {
                  if (!empty($ckeditor_profile->settings['loadPlugins']['asset'])) {

                    // Build some additional classes for textarea.
                    // We always have all asset types allowed in wysiwyg. So we can cache it.
                    if (empty($additional_classes)) {
                      $additional_classes['match-field'] = 'match-field';
                      if ($asset_types = asset_widget_get_assets_types()) {
                        foreach ($asset_types as $asset_type) {
                          if (empty($additional_classes[$asset_type])) {
                            $additional_classes[$asset_type] = "match-{$asset_type}";
                          }
                        }
                      }
                    }

                    // Avoid multiple classes adding to the element.
                    if (empty($classes_added)) {

                      // Add classes and attach widget.
                      _asset_widget_element_attach_asset_widget($element, array(), $additional_classes);
                      $classes_added = TRUE;
                    }

                    // Store allowed format.
                    $formats_asset_plugin[$format->format] = $format->format;
                  }
                }
              }
            }
          }
        }

        // Store allowed formats and additional classes to js.
        drupal_add_js(array(
          'assetWidget' => array(
            'allowedFormats' => $formats_asset_plugin,
            'additionalClasses' => $additional_classes,
          ),
        ), 'setting');
      }

      // Store element id.
      $attached[$element['#id']] = $element['#id'];
    }
  }
  return $element;
}