You are here

function template_preprocess_media_views_view_media_browser in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 media.views.inc \template_preprocess_media_views_view_media_browser()
  2. 7.2 media.views.inc \template_preprocess_media_views_view_media_browser()

Display the view as a media browser.

File

./media.views.inc, line 63
Provide Views data and handlers for media.module

Code

function template_preprocess_media_views_view_media_browser(&$vars) {
  module_load_include('inc', 'media', 'includes/media.browser');

  // Load file objects for each View result.
  $fids = array();
  foreach ($vars['rows'] as $index => $row) {

    // The Search API module returns the row in a slightly different format,
    // so convert it to the format that the normal file_managed table returns.
    if (!empty($row->entity->fid)) {
      $vars['rows'][$index]->fid = $row->entity->fid;
    }
    $fids[$index] = $row->fid;
  }
  $files = file_load_multiple($fids);

  // Render the preview for each file.
  $params = media_get_browser_params();
  $view_mode = isset($params['view_mode']) ? $params['view_mode'] : 'preview';
  foreach ($vars['rows'] as $index => $row) {

    // If the view result is cached, then the result may include fids that no
    // longer exist.
    if (!isset($files[$row->fid])) {
      unset($vars['rows'][$index]);
      continue;
    }
    $file = $files[$row->fid];

    // Add url/preview to the file object.
    media_browser_build_media_item($file, $view_mode);
    $vars['rows'][$index] = $file;
    $vars['rows'][$index]->preview = $file->preview;
  }

  // Add the files to JS so that they are accessible inside the browser.
  drupal_add_js(array(
    'media' => array(
      'files' => array_values($files),
    ),
  ), 'setting');

  // Add the browser parameters to the settings and that this display exists.
  $params = media_get_browser_params();
  drupal_add_js(array(
    'media' => array(
      'browser' => array(
        'params' => media_get_browser_params(),
        'views' => array(
          $vars['view']->name => array(
            $vars['view']->current_display,
          ),
        ),
      ),
    ),
  ), 'setting');

  // Add classes and wrappers from the style plugin.
  $handler = $vars['view']->style_plugin;
  $class = explode(' ', $handler->options['class']);
  $class = array_map('drupal_clean_css_identifier', $class);
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
  $vars['class'] = implode(' ', $class);
  $vars['wrapper_class'] = implode(' ', $wrapper_class);
  $vars['wrapper_prefix'] = '<div class="' . implode(' ', $wrapper_class) . '">';
  $vars['wrapper_suffix'] = '</div>';
  $vars['list_type_prefix'] = '<' . $handler->options['type'] . ' id="media-browser-library-list" class="' . implode(' ', $class) . '">';
  $vars['list_type_suffix'] = '</' . $handler->options['type'] . '>';
  $vars['aria_role'] = isset($params['multiselect']) && $params['multiselect'] ? 'checkbox' : 'radio';

  // Run theming variables through a standard Views preprocess function.
  template_preprocess_views_view_unformatted($vars);

  // Add media browser JavaScript and CSS.
  drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
}