You are here

function media_form_file_entity_admin_file_alter in D7 Media 7.4

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

Implements hook_form_FORM_ID_alter().

This alter enhances the default admin/content/file page, addding JS and CSS. It also makes modifications to the thumbnail view by replacing the existing checkboxes and table with thumbnails.

File

./media.module, line 1304
Media API

Code

function media_form_file_entity_admin_file_alter(&$form, $form_state) {
  if (!empty($form_state['values']['operation'])) {

    // The form is being rebuilt because an operation requiring confirmation
    // We don't want to be messing with it in this case.
    return;
  }

  // Add the "Add file" local action, and notify users if they have files
  // selected and they try to switch between the "Thumbnail" and "List" local
  // tasks.
  $path = drupal_get_path('module', 'media');
  $form['#attributes']['class'][] = 'file-entity-admin-file-form';
  $form['#attached']['js'][] = $path . '/js/media.admin.js';
  $form['#attached']['css'][] = $path . '/css/media.css';

  // By default, this form contains a table select element called "files". For
  // the 'thumbnails' tab, Media generates a thumbnail for each file and
  // replaces the tableselect with a grid of thumbnails.
  if (arg(3) == 'thumbnails') {
    if (empty($form['admin']['files']['#options'])) {

      // Display empty text if there are no files.
      $form['admin']['files'] = array(
        '#markup' => '<p>' . $form['admin']['files']['#empty'] . '</p>',
      );
    }
    else {
      $files = file_load_multiple(array_keys($form['admin']['files']['#options']));
      $form['admin']['files'] = array(
        '#tree' => TRUE,
        '#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul id="media-browser-library-list" class="media-list-thumbnails">',
        '#suffix' => '</ul></div>',
      );
      foreach ($files as $file) {
        $preview = media_get_thumbnail_preview($file, TRUE);
        $form['admin']['files'][$file->fid] = array(
          '#type' => 'checkbox',
          '#title' => '',
          '#prefix' => '<li>' . drupal_render($preview),
          '#suffix' => '</li>',
        );
      }
    }
  }
}