You are here

function insert_element_process in Insert 7

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

Process function for insert-enabled fields.

1 string reference to 'insert_element_process'
insert_element_info in ./insert.module
Implements hook_element_info().

File

./insert.module, line 249
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;

  // Bail out early if the needed properties aren't available. This happens
  // most frequently when editing a field configuration.
  if (!isset($element['#entity_type'])) {
    return $element;
  }
  $item = $element['#value'];
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  $widget_settings = $instance['widget']['settings'];
  $widget_type = $instance['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_default_scheme(),
    );
    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');
  }

  // Load the file if it's not entirely loaded.
  if ($element['fid']['#value'] && !isset($item['filename'])) {
    if ($loaded_file = file_load($element['fid']['#value'])) {
      $item = array_merge((array) $loaded_file, $item);
    }
  }
  if (isset($item['filename'])) {
    $insert_styles = !empty($widget_settings['insert_styles']['<all>']) ? drupal_map_assoc(array_keys(insert_styles())) : array_filter((array) $widget_settings['insert_styles']);
    $default = !empty($instance['widget']['settings']['insert_default']) ? $widget_settings['insert_default'] : 'auto';
    if (!isset($insert_styles[$default])) {
      $insert_styles[$default] = $default;
    }
    $file = file_load($item['fid']);
    $info = @pathinfo($file->uri);
    $audioExtensions = variable_get('insert_file_extensions_audio', unserialize(INSERT_DEFAULT_CONFIG)['insert_file_extensions_audio']);
    $videoExtensions = variable_get('insert_file_extensions_video', unserialize(INSERT_DEFAULT_CONFIG)['insert_file_extensions_video']);
    foreach ($insert_styles as $style_name => $enabled) {
      if ($enabled && ($style = insert_style_load($style_name)) && ($style_name !== 'video' || in_array($info['extension'], $videoExtensions)) && ($style_name !== 'audio' || in_array($info['extension'], $audioExtensions))) {
        $element['insert_templates'][$style_name] = array(
          '#type' => 'hidden',
          '#value' => insert_content($item, $style, $instance['widget'], $element),
          '#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
          '#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
          '#attributes' => array(
            'class' => array(
              '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' => array(
            'insert-filename',
          ),
        ),
      );
    }
    $element['insert'] = array(
      '#theme' => 'insert_widget',
      '#type' => 'markup',
      '#options' => $style_options,
      '#widget' => $instance['widget'],
      '#weight' => -3,
      '#default_value' => $default,
    );
  }
  return $element;
}