-
Notifications
You must be signed in to change notification settings - Fork 0
/
varbase_youtube_import.module
executable file
·94 lines (90 loc) · 3.89 KB
/
varbase_youtube_import.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* @file
* Varbase YouTube Import hook implementations.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements form_alter().
*/
function varbase_youtube_import_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
if ($form_id == 'feeds_feed_youtube_channel_form') {
$form['plugin']['fetcher']['source']['#description'] = t('This field should contain a youtube channel link.');
$form['plugin']['fetcher']['source']['#title'] = t('YouTube URL');
}
elseif ($form_id == 'feeds_feed_youtube_playlist_form') {
$form['plugin']['fetcher']['source']['#description'] = t('This field should contain a youtube playlist link.');
$form['plugin']['fetcher']['source']['#title'] = t('YouTube URL');
}
}
/**
* Implements entity_presave().
*
* This function will used to replace the youtube playlist/channel
* link to their RSS feed link.
*/
function varbase_youtube_import_entity_presave(EntityInterface $entity) {
if ($entity->getEntityType()->id() == 'feeds_feed') {
if ($entity->get('type')->target_id == 'youtube_channel') {
if (strpos($entity->get('source')->value, 'googleapis') == FALSE) {
$api = $entity->get('field_api_key')->value;
$link = $entity->get('source')->value;
$pos = strpos($link, '/channel/');
$link_id = substr($link, $pos + 9);
$feed_link = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&type=video&channelId=' . $link_id . '&maxResults=50&key=' . $api;
$entity->set('source', $feed_link, TRUE);
}
else {
$field_api = $entity->get('field_api_key')->value;
$link = $entity->get('source')->value;
$key_pos = strpos($link, '&key=');
$token_pos = strpos($link, '&pageToken=');
$old_api = substr($link, $key_pos + 5);
if ($old_api !== $field_api) {
if (!(strpos($entity->get('source')->value, 'pageToken'))) {
$feed_link = substr($entity->get('source')->value, 0, $key_pos);
$feed_link = $feed_link . '&key=' . $field_api;
$entity->set('source', $feed_link, TRUE);
}
else {
$feed_link = substr($entity->get('source')->value, 0, $key_pos);
$token = substr($entity->get('source')->value, $token_pos + 11);
$feed_link = $feed_link . '&key=' . $field_api . '&pageToken=' . $token;
$entity->set('source', $feed_link, TRUE);
}
}
}
}
elseif ($entity->get('type')->target_id == 'youtube_playlist') {
if (strpos($entity->get('source')->value, 'googleapis') == FALSE) {
$api = $entity->get('field_api_key')->value;
$link = $entity->get('source')->value;
$pos = strpos($link, 'list=');
$link_id = substr($link, $pos + 5);
$feed_link = 'https://www.googleapis.com/youtube/v3/playlistItems?order=date&part=snippet&playlistId=' . $link_id . '&maxResults=50&key=' . $api;
$entity->set('source', $feed_link, TRUE);
}
else {
$field_api = $entity->get('field_api_key')->value;
$link = $entity->get('source')->value;
$key_pos = strpos($link, '&key=');
$token_pos = strpos($link, '&pageToken=');
$old_api = substr($link, $key_pos + 5);
if ($old_api !== $field_api) {
if (!(strpos($entity->get('source')->value, 'pageToken'))) {
$feed_link = substr($entity->get('source')->value, 0, $key_pos);
$feed_link = $feed_link . '&key=' . $field_api;
$entity->set('source', $feed_link, TRUE);
}
else {
$feed_link = substr($entity->get('source')->value, 0, $key_pos);
$token = substr($entity->get('source')->value, $token_pos + 11);
$feed_link = $feed_link . '&key=' . $field_api . '&pageToken=' . $token;
$entity->set('source', $feed_link, TRUE);
}
}
}
}
}
}