You are here

vimeo_video_uploader.module in Vimeo Video Uploader 8

File

vimeo_video_uploader.module
View source
<?php

/**
 * @file
 * Module file for the Vimeo Video Uploader.
 */
$vendor_path = str_replace('modules/', '', dirname(dirname(__DIR__)) . '/vendor/autoload.php');
include_once $vendor_path;
use Vimeo\Vimeo;
use Vimeo\Exceptions\VimeoUploadException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function vimeo_video_uploader_help($path) {
  switch ($path) {
    case 'help.page.vimeo_video_uploader':
      $output = '<p>' . t('Visit <a href="https://www.drupal.org/node/2726121">Documentation</a> for instructions') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_permission().
 */
function vimeo_video_uploader_permission() {
  return array(
    'access vimeo uploader' => array(
      'title' => t('Administer Vimeo Video Uploader'),
    ),
  );
}

/**
 * Implements hook_node_presave().
 */
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;
      }
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function vimeo_video_uploader_node_delete($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');
  if ($node
    ->bundle() == $content_type && !empty($node
    ->id())) {
    $field_embeddedvideo = $node
      ->get('field_embeddedvideo')->value;
    $video_id_val = substr($field_embeddedvideo, 17);
    if (!empty($video_id_val)) {
      $lib_ = new Vimeo($client_id, $client_secret, $access_token);
      try {
        $lib_
          ->request('/videos/' . $video_id_val, array(), 'DELETE');
      } catch (VimeoUploadException $e) {
        redirect_path('admin/content');
        $error_message = $e
          ->getMessage();
        drupal_set_message(t('Not Authorized...!' . $error_message), 'error');
        return;
      }
    }
  }
}
function redirect_path($path) {
  $response = new RedirectResponse($path);
  $response
    ->send();
  return;
}

/**
 * Implements hook_form_alter().
 */
function vimeo_video_uploader_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('vimeo_video_uploader.settings');
  $content_type = $config
    ->get('values.content_type_select');
  $form_type_edit_id = 'node_' . $content_type . '_edit_form';
  $form_type_add_id = 'node_' . $content_type . '_form';
  if ($form_id == $form_type_edit_id) {
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      $form['#attached']['library'][] = 'vimeo_video_uploader/uploader';

      //attach js/css library file
      $nid = $node
        ->id();
      $node = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->load($nid);
      $field_embeddedvideo = $node
        ->get('field_embeddedvideo')->value;
      $video_id = preg_replace('/[^0-9]/', '', $field_embeddedvideo);
      if (!empty($video_id)) {
        $xml_data = simplexml_load_file("http://vimeo.com/api/v2/video/{$video_id}.xml");
        if (!empty($xml_data)) {
          $xml_data = json_decode(json_encode($xml_data, true));
          if ($xml_data->video) {
            $thumbnail_image = $xml_data->video->thumbnail_small;
            $form['field_vimeo_file_browse']['#suffix'] = '<p class="video-thumbnail-box"><a href="' . $field_embeddedvideo . '" target="_blank"><img src="' . $thumbnail_image . '"></a><a id="delete_link" >Delete</a></p>';
          }
        }
      }
    }
  }
  if ($from_id = $form_type_add_id) {
    $form['#attached']['library'][] = 'vimeo_video_uploader/uploader';

    //attach js/css library file

    // $form['field_embeddedvideo']['#access'] = FALSE;
  }
}

Functions

Namesort descending Description
redirect_path
vimeo_video_uploader_form_alter Implements hook_form_alter().
vimeo_video_uploader_help Implements hook_help().
vimeo_video_uploader_node_delete Implements hook_node_delete().
vimeo_video_uploader_node_presave Implements hook_node_presave().
vimeo_video_uploader_permission Implements hook_permission().