You are here

function vimeo_video_uploader_node_presave in Vimeo Video Uploader 8

Same name and namespace in other branches
  1. 8.4 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()
  2. 7.2 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()
  3. 7 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()

Implements hook_node_presave().

File

./vimeo_video_uploader.module, line 44

Code

function vimeo_video_uploader_node_presave(Drupal\Core\Entity\EntityInterface $node) {
  $config = \Drupal::config('vimeo_video_uploader.settings');
  $client_id = $config
    ->get('values.client_id');
  $client_secret = $config
    ->get('values.client_secret');
  $access_token = $config
    ->get('values.access_token');
  $content_type = $config
    ->get('values.content_type_select');
  $lib = new Vimeo($client_id, $client_secret, $access_token);
  if (!empty($node->field_vimeo_file_browse)) {
    $field_embeddedvideo = $node
      ->get('field_embeddedvideo')->value;
    $video_title = $node
      ->get('title')->value;
    $video_desc = $node
      ->get('body')->value;
    $field_vimeo_file_browse = $node
      ->get('field_vimeo_file_browse')
      ->getValue();
    if ($node
      ->bundle() == $content_type && empty($node
      ->id()) && !empty($field_vimeo_file_browse)) {
      try {
        $file_uri = $node
          ->get('field_vimeo_file_browse')->entity->uri->value;
        $video_id_link = $lib
          ->upload(\Drupal::service('file_system')
          ->realpath($file_uri), [
          'name' => 'untitled',
          'privacy' => [
            'view' => 'anybody',
          ],
        ]);
        $video_id = explode('/', $video_id_link);

        //#video id sample /videos/255895935
        $video_id = end($video_id);
        if (!empty($video_id)) {
          $lib
            ->request('/videos/' . $video_id, array(
            'name' => $video_title,
          ), 'PATCH');
          $video_url = 'http://vimeo.com/' . $video_id;
          $node->field_embeddedvideo->value = $video_url;
        }
        file_delete($field_vimeo_file_browse[0]['target_id']);
      } catch (VimeoUploadException $e) {
        $error_message = $e
          ->getMessage();
        drupal_set_message(t('Not Authorized...!' . $error_message), 'error');
        return;
      }
    }
    elseif ($node
      ->bundle() == $content_type && !empty($node
      ->id())) {
      try {
        if (!empty($field_vimeo_file_browse)) {
          $file_uri = $node
            ->get('field_vimeo_file_browse')->entity->uri->value;
          $lib = new Vimeo($client_id, $client_secret, $access_token);
          $video_id_ = substr($field_embeddedvideo, 17);
          $lib
            ->request('/videos/' . $video_id_, array(), 'DELETE');
          $video_id_link = $lib
            ->upload(\Drupal::service('file_system')
            ->realpath($file_uri), [
            'name' => 'untitled',
            'privacy' => [
              'view' => 'anybody',
            ],
          ]);
          $video_id = substr($video_id_link, 8);
          $lib
            ->request('/videos/' . $video_id, array(
            'name' => $video_title,
          ), 'PATCH');
          $video_url = 'http://vimeo.com/' . $video_id;
          $node->field_embeddedvideo->value = $video_url;
          file_delete($field_vimeo_file_browse[0]['target_id']);
        }
      } catch (VimeoUploadException $e) {
        $error_message = $e
          ->getMessage();
        drupal_set_message(t('Not Authorized...!' . $error_message), 'error');
        return;
      }
    }
    elseif ($node
      ->bundle() == $content_type && !empty($node
      ->id()) && empty($field_vimeo_file_browse)) {
      try {
        $lib = new Vimeo($client_id, $client_secret, $access_token);
        $video_id = substr($field_embeddedvideo, 17);
        $video_title = $node
          ->get('title')->value;
        $lib
          ->request('/videos/' . $video_id, array(
          'name' => $video_title,
        ), 'PATCH');
      } catch (VimeoUploadException $e) {
        $error_message = $e
          ->getMessage();
        drupal_set_message(t('Not Authorized...!' . $error_message), 'error');
        return;
      }
    }
  }
}