You are here

function insert_element_process in Insert 6

Same name and namespace in other branches
  1. 7 insert.module \insert_element_process()

Process function for insert-enabled fields.

1 string reference to 'insert_element_process'
insert_elements in ./insert.module
Implementation of hook_elements().

File

./insert.module, line 178
Allows insertion of files, images, and other media directly into the body field by using an "Insert" button next to the uploaded file.

Code

function insert_element_process($element) {
  static $js_added;
  $item = $element['#value'];
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $widget_settings = $field['widget'];
  $widget_type = $field['widget']['type'];

  // Bail out of Insert is not enabled on this field.
  if (empty($widget_settings['insert'])) {
    return $element;
  }

  // Add base settings only once.
  if (!isset($js_added)) {
    $js_added = array();
    $settings = array(
      'fileDirectoryPath' => file_directory_path(),
    );
    drupal_add_js(array(
      'insert' => $settings,
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'insert') . '/insert.js');
  }

  // Add settings for this widget only once.
  if (!isset($js_added[$widget_type])) {
    $js_added[$widget_type] = TRUE;
    $insert_widget = insert_widget_load($widget_type);
    $insert_settings = array(
      'maxWidth' => $widget_settings['insert_width'],
      'wrapper' => $insert_widget['wrapper'],
      'fields' => $insert_widget['fields'],
    );
    drupal_add_js(array(
      'insert' => array(
        'widgets' => array(
          $widget_type => $insert_settings,
        ),
      ),
    ), 'setting');
  }
  if ($element['fid']['#value']) {
    $insert_styles = !empty($widget_settings['insert_styles']['<all>']) ? drupal_map_assoc(array_keys(insert_styles())) : array_filter((array) $widget_settings['insert_styles']);
    $default = !empty($widget_settings['insert_default']) ? $widget_settings['insert_default'] : 'auto';
    if (!isset($insert_styles[$default])) {
      $insert_styles[$default] = $default;
    }
    foreach ($insert_styles as $style_name => $enabled) {
      if ($enabled && ($style = insert_style_load($style_name))) {
        $element['insert_templates'][$style_name] = array(
          '#type' => 'hidden',
          '#value' => insert_content($item, $style, $field['widget']),
          '#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
          '#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
          '#attributes' => array(
            'class' => 'insert-template',
          ),
        );
        $style_options[$style_name] = $style['label'];
      }

      // Always provide a file name property.
      $element['insert_filename'] = array(
        '#type' => 'hidden',
        '#value' => $item['filename'],
        '#id' => $element['#id'] . '-insert-filename',
        '#name' => $element['#name'] . '[insert_filename]',
        '#attributes' => array(
          'class' => 'insert-filename',
        ),
      );
    }
    $element['insert'] = array(
      '#theme' => 'insert_widget',
      '#type' => 'markup',
      '#options' => $style_options,
      '#widget' => $field['widget'],
      '#weight' => -1,
      '#default_value' => $default,
    );
  }
  return $element;
}