You are here

function media_browser in D7 Media 7

Same name and namespace in other branches
  1. 7.4 includes/media.browser.inc \media_browser()
  2. 7.2 includes/media.browser.inc \media_browser()
  3. 7.3 includes/media.browser.inc \media_browser()

@file Media Browser page callback

3 string references to 'media_browser'
media_browser_js in includes/media.browser.inc
Helper function to define browser javascript.
media_element_info in ./media.module
Implements hook_element_info().
media_menu in ./media.module
Implement of hook_menu().

File

includes/media.browser.inc, line 7
Media Browser page callback

Code

function media_browser($selected = NULL) {
  $output = array();
  $output['#attached']['library'][] = array(
    'media',
    'media_browser_page',
  );
  $params = drupal_get_query_parameters();

  // Filter out everything except a whitelist of known safe options.
  // Backported http://cgit.drupalcode.org/media/commit/?id=86cefbb
  // To mitigate https://www.drupal.org/forum/newsletters/security-advisories-for-contributed-projects/2017-05-10/media-moderately-critical.
  $safe_options = array(
    'activePlugins',
    'fid',
    'id',
    'multiselect',
    'options',
    'plugins',
    'render',
    'types',
  );
  $params = array_intersect_key($params, array_flip($safe_options));
  array_walk_recursive($params, '_media_recursive_check_plain');
  $params = media_set_browser_params($params);

  // If one or more files have been selected, the browser interaction is now
  // complete. Return empty page content to the dialog which now needs to close,
  // but populate Drupal.settings with information about the selected files.
  if (isset($params['fid'])) {
    $fids = is_array($params['fid']) ? $params['fid'] : array(
      $params['fid'],
    );
    if (!is_numeric($fids[0])) {
      throw new Exception('Error selecting media, fid param is not an fid or an array of fids');
    }
    $files = file_load_multiple($fids);
    foreach ($files as $file) {
      media_browser_build_media_item($file);
    }
    $setting = array(
      'media' => array(
        'selectedMedia' => array_values($files),
      ),
    );
    drupal_add_js($setting, 'setting');
    return $output;
  }

  // Normal browser operation.
  foreach (module_implements('media_browser_plugin_info') as $module) {
    foreach (module_invoke($module, 'media_browser_plugin_info') as $key => $plugin_data) {
      $plugins[$key] = $plugin_data + array(
        '#module' => $module,
        '#weight' => 0,
      );
      $plugins[$key]['#weight'] += count($plugins) / 1000;
    }
  }

  // Only the plugins in this array are loaded.
  if (!empty($params['enabledPlugins'])) {
    $plugins = array_intersect_key($plugins, array_fill_keys($params['enabledPlugins'], 1));
  }
  elseif (!empty($params['disabledPlugins'])) {
    $plugins = array_diff_key($plugins, array_fill_keys($params['disabledPlugins'], 1));
  }
  foreach ($plugins as $key => &$plugin) {
    $plugin_output = module_invoke($plugin['#module'], 'media_browser_plugin_view', $key, $params);
    if (!empty($plugin_output) && is_array($plugin_output)) {
      $plugin += $plugin_output;
    }
    else {
      unset($plugins[$key]);
      continue;
    }
  }

  // Allow modules to change the tab names or whatever else they want to change
  // before we render.  Perhaps this should be an alter on the theming function
  // that we should write to be making the tabs.
  drupal_alter('media_browser_plugins', $plugins);
  $tabs = array();

  // List of tabs to render.
  $settings = array(
    'media' => array(
      'browser' => array(),
    ),
  );
  $browser_settings =& $settings['media']['browser'];

  //@todo: replace with Tabs module if it gets upgraded.
  foreach (element_children($plugins, TRUE) as $key) {
    $plugin =& $plugins[$key];

    //Add any JS settings
    $browser_settings[$key] = isset($plugin['#settings']) ? $plugin['#settings'] : array();

    // If this is a "ajax" style tab, add the href, otherwise an id.
    $href = isset($plugin['#callback']) ? $plugin['#callback'] : "#media-tab-{$key}";
    $tabs[] = "<a href='{$href}'><span>{$plugin['#title']}</span></a>";

    // Create a div for each tab's content.
    $plugin['#prefix'] = <<<EOS
    <div class="media-browser-tab" id="media-tab-{<span class="php-variable">$key</span>}">
EOS;
    $plugin['#suffix'] = <<<EOS
    </div>
    <!-- End #media-tab-{<span class="php-variable">$key</span>} -->
EOS;
  }
  drupal_add_js($settings, 'setting');
  $output['tabset'] = array(
    '#prefix' => '<div id="media-browser-tabset">',
    '#suffix' => '</div>',
  );
  $output['tabset']['list'] = array(
    '#markup' => '<ul><li>' . implode('</li><li>', $tabs) . '</li></ul>',
  );
  $output['tabset']['plugins'] = $plugins;
  return $output;
}