You are here

media_upload.module in D7 Media 6

This module provides integration for Drupal's upload module with the Media module

File

media_upload/media_upload.module
View source
<?php

/**
 * @file
 * This module provides integration for Drupal's upload module
 * with the Media module
 */

/**
 * Implementation of hook_media_register
 *
 * @return array
 *   Form array items
*/
function media_upload_media_register() {
  $items = array();

  // Register User files display
  $items['media_upload_user_files'] = array(
    'name' => t('User files'),
    'uri' => 'file',
    'kind' => 'resource',
    'types' => '*',
    'description' => t("Displays all of current user's files."),
    'callbacks' => array(
      'resource' => 'media_upload_user_files_select',
    ),
    'fields' => array(
      'attachments',
      'filefield',
    ),
  );

  // Register Add/Upload file functionality
  $items['media_upload_resource_add_file'] = array(
    'name' => 'Media Upload add file',
    'description' => t("Add new file using Drupal's Upload module"),
    'kind' => 'resource',
    'callbacks' => array(
      'resource' => 'media_upload_resource_add_file',
    ),
    'fields' => array(
      'attachments',
      'filefield',
    ),
  );

  // Register Embed file functionality
  $items['media_upload_resource_embed_file'] = array(
    'name' => 'Media Upload embed file',
    'description' => t("Embed file URI using Drupal's Upload module"),
    'uri' => 'file',
    'kind' => 'resource',
    'types' => '*',
    'callbacks' => array(
      'resource' => 'media_upload_resource_embed_file',
    ),
    'fields' => array(
      'attachments',
      'filefield',
    ),
  );

  // Register form formatter functionality
  $items['media_upload_formatter_form'] = array(
    'name' => 'Media Upload form formatter',
    'description' => "Media upload form formatter",
    'kind' => 'formatter',
    'callbacks' => array(
      'formatter' => 'media_upload_formatter_form',
    ),
    'fields' => array(
      'attachments',
      'filefield',
    ),
  );

  // Register action functionality
  // TODO: This may not be needed depending on our implementation of hook_media_action [see #480166]
  $items['media_upload_action'] = array(
    'name' => 'Media Upload action handler',
    'description' => t("Called to perform various actions such as attaching a file to a node"),
    'kind' => 'action',
    'types' => '*',
    'callbacks' => array(
      'action' => 'media_upload_action',
    ),
    'fields' => array(
      'attachments',
      'filefield',
    ),
  );
  return $items;
}

/**
 * Implementation of callback media_upload_media_user_files_select().
 *
 * TODO: We'll need to investigate performance mechanisms (lazy loading etc.)
 * rather than dumping ALL files in a single go.
 *
 * @param string $node_type
 * @param string $field
 * @param int $uid
 */
function media_upload_user_files_select($node_type, $field, $uid) {
  $files = array();
  $results = db_query('SELECT filepath, filename FROM {files} WHERE uid = %d', $uid);
  while ($file = db_fetch_array($results)) {
    $files[] = array(
      'uri' => $file['filepath'],
      'filename' => $file['filename'],
      'meta' => '',
    );
  }
  $return = array(
    t('My files') => array(
      t('Local') => media_resource_display_user_files_form($files, t('Your files')),
    ),
  );
  return $return;
}

/**
 * Format Media Upload form
 *
 * @param string $node_type
 * @param string $field
 * @param string $file_extension
 * @param string $uri
 * @return array
 *   Return a form array.
*/
function media_upload_formatter_form($node_type, $field, $file_extension, $uri) {
  if ($uri) {
    $form['display'] = array(
      '#type' => 'item',
      '#value' => l($uri, $uri),
    );
    $form['list'] = array(
      '#type' => 'checkbox',
      '#title' => 'List file',
      '#default_value' => TRUE,
    );
  }
  return $form;
}

/**
 * Implementation of registration media_upload_resource_add_file
 *
 * @param string $node_type
 *   drupal node type
 * @param string $field
 *   field name that is being overridden
 * @param int $uid
 *   user id
 */
function media_upload_resource_add_file($node_type, $field, $uid) {
  return array(
    t('Add files') => array(
      t('New file') => media_resource_display_upload_form(),
    ),
  );
}

/**
 * Implementation of registration media_upload_resource_embed_file
 *
 * @param string $node_type
 * @param string $field
 * @param int $uid
 */
function media_upload_resource_embed_file($node_type, $field, $uid) {
  return array(
    t('Add files') => array(
      t('Embeded file') => _media_upload_display_embed_form(),
    ),
  );
}

/**
 * Display the upload form for the tab
 *
 * @return array
 *   Form array containing a textarea
 */
function _media_upload_display_embed_form() {
  $form['media_upload']['upload'] = array(
    '#type' => 'textarea',
    '#title' => t('Upload your file'),
  );
  return $form;
}

/**
 * Implementation of hook_media_action
 *
 * TODO: Implement and document
 */
function media_upload_media_action(&$form, $form_state, $op = '') {
  dsm("In the Media Upload action handler. Implement switch and carry out tasks.");
  dsm($op);
  dsm($form_state);
  dsm($form);
}

Functions

Namesort descending Description
media_upload_formatter_form Format Media Upload form
media_upload_media_action Implementation of hook_media_action
media_upload_media_register Implementation of hook_media_register
media_upload_resource_add_file Implementation of registration media_upload_resource_add_file
media_upload_resource_embed_file Implementation of registration media_upload_resource_embed_file
media_upload_user_files_select Implementation of callback media_upload_media_user_files_select().
_media_upload_display_embed_form Display the upload form for the tab