You are here

function media_format_form in D7 Media 7

Form callback used when embedding media.

Allows the user to pick a format for their media file. Can also have additional params depending on the media type.

1 string reference to 'media_format_form'
media_menu in ./media.module
Implement of hook_menu().

File

includes/media.filter.inc, line 525
Functions related to the WYSIWYG editor and the media input filter.

Code

function media_format_form($form, $form_state, $file) {
  $form = array();
  $form['#media'] = $file;
  $view_modes = media_get_wysiwyg_allowed_view_modes($file);
  $formats = $options = array();
  foreach ($view_modes as $view_mode => $view_mode_info) {

    //@TODO: Display more verbose information about which formatter and what it does.
    $options[$view_mode] = $view_mode_info['label'];
    $element = media_get_file_without_label($file, $view_mode, array(
      'wysiwyg' => TRUE,
    ));

    // Make a pretty name out of this.
    $formats[$view_mode] = drupal_render($element);
  }

  // Add the previews back into the form array so they can be altered.
  $form['#formats'] =& $formats;
  if (!count($formats)) {
    throw new Exception('Unable to continue, no available formats for displaying media.');
    return;
  }
  $default_view_mode = media_variable_get('wysiwyg_default_view_mode');
  if (!isset($formats[$default_view_mode])) {
    $default_view_mode = key($formats);
  }

  // Add the previews by reference so that they can easily be altered by
  // changing $form['#formats'].
  $settings['media']['formatFormFormats'] =& $formats;
  $form['#attached']['js'][] = array(
    'data' => $settings,
    'type' => 'setting',
  );

  // Add the required libraries, JavaScript and CSS for the form.
  $form['#attached']['library'][] = array(
    'media',
    'media_base',
  );
  $form['#attached']['library'][] = array(
    'system',
    'form',
  );
  $form['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/media.format_form.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'media') . '/css/media-format-form.css';
  $form['heading'] = array(
    '#type' => 'markup',
    '#prefix' => '<h1 class="title">',
    '#suffix' => '</h1>',
    '#markup' => t('Embedding %filename', array(
      '%filename' => $file->filename,
    )),
  );
  $preview = media_get_thumbnail_preview($file);
  $form['preview'] = array(
    '#type' => 'markup',
    '#title' => check_plain(basename($file->uri)),
    '#markup' => drupal_render($preview),
  );

  // These will get passed on to WYSIWYG
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('options'),
  );
  $form['options']['format'] = array(
    '#type' => 'select',
    '#title' => t('Current format is'),
    '#options' => $options,
    '#default_value' => $default_view_mode,
  );

  // Similar to a form_alter, but we want this to run first so that media.types.inc
  // can add the fields specific to a given type (like alt tags on media).
  // If implemented as an alter, this might not happen, making other alters not
  // be able to work on those fields.
  // @TODO: We need to pass in existing values for those attributes.
  drupal_alter('media_format_form_prepare', $form, $form_state, $file);
  if (!element_children($form['options'])) {
    $form['options']['#attributes'] = array(
      'style' => 'display:none',
    );
  }
  return $form;
}