From #1400200: Clean up browser plugins, it would be nice to have a media browser plugin that can basically fetch whatever is output at a certain menu path and shove it into a browser tab.


/**
 * @file
 * Definition of MediaBrowserMenuPath.
 */

/**
 * Media browser plugin for Media Internet sources.
 */
class MediaBrowserMenuPath extends MediaBrowserPlugin {
  /**
   * Implements MediaBrowserPluginInterface::access().
   */
  public function access($account = NULL) {
    $item = $this->getMenuItem($this->info['path'], $account);
    return !empty($item) && $item['access'];
  }

  /**
   * Implements MediaBrowserPlugin::view().
   */
  public function view() {
    return menu_execute_active_handler($this->info['path'], FALSE);
  }

  private function impersonateUser($new_user = NULL) {
    global $user;
    $user_original = &drupal_static(__FUNCTION__, array());

    if (!isset($new_user)) {
      if (!empty($user_original)) {
        // Restore the previous user from the stack.
        $user = array_pop($user_original);

        // Re-enable session saving if we are no longer impersonating a user.
        if (empty($user_original)) {
          drupal_save_session(TRUE);
        }
      }
    }
    else {
      // Push the original user onto the stack and prevent session saving.
      $user_original[] = $user;
      drupal_save_session(FALSE);

      if (is_numeric($new_user)) {
        $user = user_load($new_user);
      }
      else {
        $user = is_object($new_user) ? $new_user : (object) $new_user;
      }
    }

    return $user;
  }

  private function revertUser() {
    return $this->impersonateUser();
  }

  private function getMenuItem($path, $account = NULL) {
    $original_static = drupal_static('menu_get_item');
    drupal_static_reset('menu_get_item');
    $this->impersonateUser($account);

    $item = menu_get_item($this->info['path']);

    $this->revertUser();
    $static = &drupal_static('menu_get_item');
    $static = $original_static;

    return $item;
  }
}
'
  $info['test'] = array(
    'title' => 'Test',
    'weight' => 50,
    'class' => 'MediaBrowserMenuPath',
    'path' => 'admin/content/file/import',
  );

Comments

Dave Reid’s picture

Status: Active » Closed (won't fix)