You are here

function media_youtube_add in Media: YouTube 7.2

Provides a form for adding media items from YouTube search.

1 string reference to 'media_youtube_add'
MediaYouTubeBrowser::view in includes/MediaYouTubeBrowser.inc
Implements MediaBrowserPlugin::view().

File

./media_youtube.module, line 109
Provides a stream wrapper and formatters appropriate for accessing and displaying YouTube videos.

Code

function media_youtube_add($form, &$form_state = array()) {
  if (strlen(variable_get('media_youtube_api_key', '')) == 0) {
    $form = array();
    $form['message'] = array(
      '#markup' => t('YouTube Data API v3 is required. You can obtain your key ' . '<a href="https://developers.google.com/youtube/v3/getting-started">' . 'here</a>. Then you need to put it in the configurations page click <a href="@url"> here ' . '</a> to go the configuration page.', array(
        '@url' => url('admin/config/media/media-youtube'),
      )),
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    return $form;
  }
  module_load_include('inc', 'media', 'includes/media.browser');

  // Our search term can come from the form, or from the pager.
  $term = isset($form_state['input']['search']) ? $form_state['input']['search'] : (isset($_GET['search']) ? $_GET['search'] : '');
  $form['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Search'),
    '#description' => t('Input a phrase or tags to search.'),
    '#default_value' => $term,
  );
  $form['apply'] = array(
    '#type' => 'button',
    '#value' => t('Apply'),
  );

  // This is our half-assed pager.
  $page = isset($_GET['page-yt']) ? $_GET['page-yt'] : 0;
  if (isset($form_state['input']['search'])) {

    // Reset the pager when we press apply.
    $page = 0;
  }
  if (!empty($term)) {
    $search = media_youtube_video_search(array(
      'q' => $term,
      'max-results' => 12,
      'start-index' => $page * 12 + 1,
    ));
  }
  $form['videos']['#prefix'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails">';
  $form['videos']['#suffix'] = '</ul><div id="status"></div></div></div>';
  $empty = FALSE;
  $files = array();
  if (!isset($search->items)) {
    $empty = TRUE;
  }
  else {

    // $search['entry'] is different depending on whether there is a single
    // result or multiple results. So normalise it.
    $videos = $search->items;
    foreach ($videos as $video) {
      if ($video->id->kind == "youtube#video") {
        try {
          $uri = 'youtube://v/' . $video->id->videoId;
        } catch (Exception $e) {

          // Ignore invalid videos.
          continue;
        }

        // Create a temporary file object for our retrieved video.
        $file = file_uri_to_object($uri);
        $file->type = 'video';
        $file->filemime = 'video/youtube';
        if (!isset($file->fid)) {
          $file->fid = 0;
        }
        media_browser_build_media_item($file);
        $attributes = array(
          'data-uri' => $uri,
          'class' => array(
            'media-youtube-wrapper',
          ),
        );
        $form['videos'][$uri] = array(
          '#markup' => $file->preview,
          '#prefix' => '<li' . drupal_attributes($attributes) . '>',
          '#suffix' => '</li>',
        );
        $files[$uri] = $file;
      }
    }
  }
  if (!count($files)) {
    $empty = TRUE;
  }
  if ($empty) {
    $form['empty'] = array(
      '#markup' => '<div class="empty-message">' . t('No videos match your search criteria. Please try again.') . '</div>',
    );
  }
  $query = $_GET;
  if ($term !== '') {
    $query['search'] = $term;
  }
  $dest = $query['q'];
  unset($query['q']);
  $prev = $next = '';
  if ($page) {
    $query['page-yt'] = $page - 1;
    $prev = l(t('previous'), $dest, array(
      'query' => $query,
    ));
  }
  $query['page-yt'] = $page + 1;
  if (!$empty) {
    $next = l(t('next'), $dest, array(
      'query' => $query,
    ));
  }
  $form['pager'] = array(
    '#markup' => $prev . ' ' . $next,
  );
  $form['submitted-video'] = array(
    '#type' => 'hidden',
    '#default_value' => FALSE,
  );

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

  // Add media browser javascript and CSS.
  drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/media-youtube.browser.js');

  // @TODO: Remove deprecated library js and css. They're removed in Media,
  // so let's comment out for now.
  // drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.library.js');
  // drupal_add_css(drupal_get_path('module', 'media') . '/js/plugins/media.library.css');
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}