You are here

function media_element_process in D7 Media 7

Same name and namespace in other branches
  1. 7.4 media.module \media_element_process()
  2. 7.2 media.module \media_element_process()
  3. 7.3 media.module \media_element_process()

#process callback for the media form element.

1 string reference to 'media_element_process'
media_element_info in ./media.module
Implements hook_element_info().

File

./media.module, line 929
Media API

Code

function media_element_process(&$element, &$form_state, $form) {
  $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;
  $file = file_load($fid);
  $path = drupal_get_path('module', 'media');
  $element['title'] = array(
    '#type' => 'item',
    '#title' => $element['#title'],
    '#markup' => '',
    '#description' => $element['#description'],
    '#required' => $element['#required'],
  );

  //@TODO: This should be loaded from the JS in case of a failed form submission.
  $markup = '';
  if (!empty($file)) {
    $preview = media_get_thumbnail_preview($file);
    $markup = drupal_render($preview);
  }
  $element['preview'] = array(
    '#type' => 'item',
    '#markup' => $markup,
    '#prefix' => '<div class="preview launcher">',
    '#suffix' => '</div><a class="button launcher" href="#">' . media_variable_get('field_select_media_text') . '</a><a class="button remove" href="#">' . media_variable_get('field_remove_media_text') . '</a>',
  );

  /**
   * This section handles fields on media when media is added as a field.
   * It is pretty unpolished, so hiding it for now.
   */

  //  $element['more_fields_wrapper'] = array(
  //    '#type' => 'fieldset',
  //    '#collapsible' => TRUE,
  //    '#collapsed' => TRUE,
  //    '#title' => t('Meta-data'),
  //  );
  //
  //  $element['more_fields_wrapper']['edit'] = array(
  //    '#type' => 'markup',
  //    '#markup' => l(t('Edit'), 'media/' . $fid . '/edit', array('query' => array('render' => 'media-popup'), 'attributes' => array('class'=> array('media-edit-link')))),
  //  );
  //
  //  // Oh god, there must be a better way to add a wrapper.
  //  $parents = $element['#parents'];
  //  array_push($parents, 'more_fields');
  //
  //  if ($file) {
  //    $element['more_fields_wrapper']['more_fields'] = file_view($file, 'media_preview');
  //    unset($element['more_fields_wrapper']['more_fields']['file']);
  //  }

  //@HACK: @todo: this is so I can find it in media.js without putting every field in a settings variable.

  // If I make it hidden (which it should be) it will go to the top of the form... I know this sucks.
  // This is hidden in media.css
  $element['fid'] = array(
    '#type' => 'textfield',
    '#default_value' => $fid,
    '#attributes' => array(
      'class' => array(
        'fid',
      ),
    ),
  );

  // Media browser attach code.
  $element['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/media.js';
  $setting = array();
  $setting['media']['elements'][$element['#id']] = $element['#media_options'];
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => $setting,
  );

  // hmm... Might need to think about this.
  // All settings would likely apply to all media in a multi-value, but what about passing the existing fid?
  module_load_include('inc', 'media', 'includes/media.browser');
  media_attach_browser_js($element);
  return $element;

  // @todo: make this work for file and image fields
}