View source
<?php
$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;
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:
}
}
function vimeo_video_uploader_permission() {
return array(
'access vimeo uploader' => array(
'title' => t('Administer Vimeo Video Uploader'),
),
);
}
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 = 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;
}
}
}
}
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;
}
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';
$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';
}
}