You are here

function media_element_process in D7 Media 7.2

Same name and namespace in other branches
  1. 7.4 media.module \media_element_process()
  2. 7 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 725
Media API

Code

function media_element_process($element, &$form_state, $form) {
  ctools_include('modal');
  ctools_include('ajax');
  ctools_modal_add_js();

  // Append the '-upload' to the #id so the field label's 'for' attribute
  // corresponds with the textfield element.
  $original_id = $element['#id'];
  $element['#id'] .= '-upload';
  $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0;

  // Set some default element properties.
  $element['#file'] = $fid ? file_load($fid) : FALSE;
  $element['#tree'] = TRUE;
  $ajax_settings = array(
    'path' => 'media/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
    'wrapper' => $original_id . '-ajax-wrapper',
    'effect' => 'fade',
  );

  // Set up the buttons first since we need to check if they were clicked.
  $element['attach_button'] = array(
    '#name' => implode('_', $element['#parents']) . '_attach_button',
    '#type' => 'submit',
    '#value' => t('Attach'),
    '#validate' => array(),
    '#submit' => array(
      'media_file_submit',
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#ajax' => $ajax_settings,
    '#attributes' => array(
      'class' => array(
        'attach',
      ),
    ),
    '#weight' => -1,
  );
  $element['preview'] = array(
    'content' => array(),
    '#prefix' => '<div class="preview">',
    '#suffix' => '</div>',
    '#ajax' => $ajax_settings,
    '#weight' => -10,
  );

  // Substitute the JS preview for a true file thumbnail once the file is
  // attached.
  if ($fid && $element['#file']) {
    $element['preview']['content'] = media_get_thumbnail_preview($element['#file']);
  }

  // The file ID field itself.
  $element['upload'] = array(
    '#name' => 'media[' . implode('_', $element['#parents']) . ']',
    '#type' => 'textfield',
    '#title' => t('Enter the ID of an existing file'),
    '#title_display' => 'invisible',
    '#field_prefix' => t('File ID'),
    '#size' => $element['#size'],
    '#theme_wrappers' => array(),
    '#attributes' => array(
      'class' => array(
        'upload',
      ),
    ),
    '#weight' => -9,
  );
  $element['browse_button'] = array(
    '#type' => 'link',
    '#href' => '',
    '#title' => t('Browse'),
    '#attributes' => array(
      'class' => array(
        'button',
        'browse',
        'element-hidden',
      ),
    ),
    '#options' => array(
      'fragment' => FALSE,
      'external' => TRUE,
    ),
    '#weight' => -8,
  );

  // Force the progress indicator for the remove button to be either 'none' or
  // 'throbber', even if the upload button is using something else.
  $ajax_settings['progress']['type'] = 'throbber';
  $ajax_settings['progress']['message'] = NULL;
  $ajax_settings['effect'] = 'none';
  $element['edit'] = array(
    '#type' => 'link',
    '#href' => 'media/' . $fid . '/edit/nojs',
    '#title' => t('Edit'),
    '#attributes' => array(
      'class' => array(
        // Required for CTools modal to work.
        'ctools-use-modal',
        'ctools-modal-media-file-edit',
        'button',
        'edit',
      ),
    ),
    '#weight' => 20,
    '#access' => $element['#file'] ? file_entity_access('update', $element['#file']) : FALSE,
  );

  // If we have parent entity form/source langcodes, pass them in query. They
  // will be used in

  /* @see media_file_edit_modal() */
  if (!empty($element['#media_parent_entity_form_langcode'])) {
    $element['edit']['#options']['query']['media_parent_entity_form_langcode'] = $element['#media_parent_entity_form_langcode'];
    if (!empty($element['#media_parent_entity_source_langcode'])) {
      $element['edit']['#options']['query']['media_parent_entity_source_langcode'] = $element['#media_parent_entity_source_langcode'];
    }
  }
  $element['remove_button'] = array(
    '#name' => implode('_', $element['#parents']) . '_remove_button',
    '#type' => 'submit',
    '#value' => t('Remove'),
    '#validate' => array(),
    '#submit' => array(
      'media_file_submit',
    ),
    '#limit_validation_errors' => array(
      $element['#parents'],
    ),
    '#ajax' => $ajax_settings,
    '#attributes' => array(
      'class' => array(
        'remove',
      ),
    ),
    '#weight' => 0,
  );
  $element['fid'] = array(
    '#type' => 'hidden',
    '#value' => $fid,
    '#attributes' => array(
      'class' => array(
        'fid',
      ),
    ),
  );

  // Media browser attach code.
  $element['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/media.js';

  // IDs of form elements are 'unstable' in Drupal because of drupal_html_id
  // add a class for our Javascript instead.
  $element_js_class = drupal_html_class('js-media-element-' . $element['#id']);
  $element['upload']['#attributes']['class'][] = $element_js_class;

  // Cache the media options and pass the cache ID as a JavaScript setting.
  $cid = drupal_get_token(drupal_random_bytes(32));
  cache_set('media_options:' . $cid, $element['#media_options']['global'], 'cache_form', REQUEST_TIME + 21600);
  $element['browse_button']['#attached']['js'] = array(
    array(
      'type' => 'setting',
      'data' => array(
        'media' => array(
          'elements' => array(
            '.' . $element_js_class => array(
              'global' => array(
                'options' => $cid,
              ),
            ),
          ),
        ),
      ),
    ),
  );
  $element['#attached']['library'][] = array(
    'media',
    'media_browser',
  );
  $element['#attached']['library'][] = array(
    'media',
    'media_browser_settings',
  );

  // Prefix and suffix used for Ajax replacement.
  $element['#prefix'] = '<div id="' . $original_id . '-ajax-wrapper">';
  $element['#suffix'] = '</div>';
  return $element;
}