video_embed_field.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the video_embed_field module.
  5. */
  6. /**
  7. * Install file for video_embed_field module
  8. * @author jcaldwell
  9. */
  10. /**
  11. * Implements hook_field_schema().
  12. */
  13. function video_embed_field_field_schema($field) {
  14. switch ($field['type']) {
  15. case 'video_embed_field':
  16. $columns = array(
  17. 'video_url' => array(
  18. 'type' => 'varchar',
  19. 'length' => 512,
  20. 'default' => '',
  21. ),
  22. 'thumbnail_path' => array(
  23. 'type' => 'varchar',
  24. 'length' => 512,
  25. 'default' => '',
  26. ),
  27. 'video_data' => array(
  28. 'type' => 'blob',
  29. 'not null' => FALSE,
  30. 'size' => 'big',
  31. 'serialize' => TRUE,
  32. ),
  33. 'embed_code' => array(
  34. 'type' => 'varchar',
  35. 'length' => 1024,
  36. 'default' => '',
  37. ),
  38. 'description' => array(
  39. 'type' => 'text',
  40. 'not null' => FALSE,
  41. ),
  42. );
  43. $indexes = array();
  44. break;
  45. }
  46. return array(
  47. 'columns' => $columns,
  48. 'indexes' => $indexes,
  49. );
  50. }
  51. /**
  52. * Implements hook_schema().
  53. */
  54. function video_embed_field_schema() {
  55. $schema['vef_video_styles'] = array(
  56. 'description' => 'Stores video embed styles.',
  57. 'export' => array(
  58. 'key' => 'name',
  59. 'identifier' => 'video_embed_style',
  60. 'default hook' => 'default_video_embed_styles',
  61. 'api' => array(
  62. 'owner' => 'video_embed_field',
  63. 'api' => 'default_video_embed_styles',
  64. 'minimum_version' => 1,
  65. 'current_version' => 1,
  66. ),
  67. ),
  68. 'fields' => array(
  69. 'name' => array(
  70. 'description' => 'The machine-readable option set name.',
  71. 'type' => 'varchar',
  72. 'length' => 255,
  73. 'not null' => TRUE,
  74. ),
  75. 'title' => array(
  76. 'description' => 'The human-readable title for this option set.',
  77. 'type' => 'varchar',
  78. 'length' => 255,
  79. 'not null' => TRUE,
  80. ),
  81. 'data' => array(
  82. 'description' => 'The configuration data for the style.',
  83. 'type' => 'blob',
  84. 'size' => 'big',
  85. 'not null' => TRUE,
  86. 'serialize' => TRUE,
  87. ),
  88. ),
  89. 'primary key' => array('name'),
  90. );
  91. return $schema;
  92. }
  93. /**
  94. * Implements hook_uninstall().
  95. */
  96. function video_embed_field_uninstall() {
  97. variable_del('video_embed_field_youtube_api_key');
  98. }
  99. /**
  100. * Adds an optional description form.
  101. */
  102. function video_embed_field_update_7000() {
  103. // Get the list of fields of type 'video_embed_field'.
  104. $video_embed_fields = array();
  105. foreach (field_info_fields() as $field_name => $field_info) {
  106. if ($field_info['type'] == 'video_embed_field') {
  107. $video_embed_fields[$field_name] = field_read_field($field_name);
  108. }
  109. }
  110. foreach ($video_embed_fields as $field) {
  111. if ($field['deleted']) {
  112. $table = "field_deleted_data_{$field['id']}";
  113. $revision_table = "field_deleted_revision_{$field['id']}";
  114. }
  115. else {
  116. $table = "field_data_{$field['field_name']}";
  117. $revision_table = "field_revision_{$field['field_name']}";
  118. }
  119. $column = $field['field_name'] . '_description';
  120. db_add_field($table, $column, array('type' => 'text', 'not null' => FALSE));
  121. db_add_field($revision_table, $column, array('type' => 'text', 'not null' => FALSE));
  122. }
  123. return t('Additional columns added.');
  124. }
  125. /**
  126. * Adds video style storage table.
  127. */
  128. function video_embed_field_update_7001() {
  129. if (!db_table_exists('vef_video_styles')) {
  130. $schema = video_embed_field_schema();
  131. db_create_table('vef_video_styles', $schema['vef_video_styles']);
  132. }
  133. return t('Video styles storage table created.');
  134. }
  135. /**
  136. * Adds field for storing the path to the video thumbnail.
  137. */
  138. function video_embed_field_update_7002() {
  139. // Get the list of fields of type 'video_embed_field'.
  140. $video_embed_fields = array();
  141. foreach (field_info_fields() as $field_name => $field_info) {
  142. if ($field_info['type'] == 'video_embed_field') {
  143. $video_embed_fields[$field_name] = field_read_field($field_name);
  144. }
  145. }
  146. foreach ($video_embed_fields as $field) {
  147. if ($field['deleted']) {
  148. $table = "field_deleted_data_{$field['id']}";
  149. $revision_table = "field_deleted_revision_{$field['id']}";
  150. }
  151. else {
  152. $table = "field_data_{$field['field_name']}";
  153. $revision_table = "field_revision_{$field['field_name']}";
  154. }
  155. $column = $field['field_name'] . '_thumbnail_path';
  156. db_add_field($table, $column, array(
  157. 'type' => 'varchar',
  158. 'length' => 512,
  159. 'default' => '',
  160. ));
  161. db_add_field($revision_table, $column, array(
  162. 'type' => 'varchar',
  163. 'length' => 512,
  164. 'default' => '',
  165. ));
  166. }
  167. return t('Thumbnail column added.');
  168. }
  169. /**
  170. * Enables inline colorbox support if colorbox is installed.
  171. *
  172. * [NO LONGER NEEDED - This update hook does nothing]
  173. */
  174. function video_embed_field_update_7003() {
  175. }
  176. /**
  177. * Enables colorbox load support if colorbox is installed.
  178. */
  179. function video_embed_field_update_7004() {
  180. variable_set('colorbox_load', 1);
  181. }
  182. /**
  183. * Adds data column to field database.
  184. */
  185. function video_embed_field_update_7005() {
  186. // Get the list of fields of type 'video_embed_field'.
  187. $video_embed_fields = array();
  188. foreach (field_info_fields() as $field_name => $field_info) {
  189. if ($field_info['type'] == 'video_embed_field') {
  190. $video_embed_fields[$field_name] = field_read_field($field_name);
  191. }
  192. }
  193. foreach ($video_embed_fields as $field) {
  194. if ($field['deleted']) {
  195. $table = "field_deleted_data_{$field['id']}";
  196. $revision_table = "field_deleted_revision_{$field['id']}";
  197. }
  198. else {
  199. $table = "field_data_{$field['field_name']}";
  200. $revision_table = "field_revision_{$field['field_name']}";
  201. }
  202. $column = $field['field_name'] . '_video_data';
  203. db_add_field($table, $column, array(
  204. 'type' => 'blob',
  205. 'not null' => FALSE,
  206. 'size' => 'big',
  207. 'serialize' => TRUE,
  208. ));
  209. db_add_field($revision_table, $column, array(
  210. 'type' => 'blob',
  211. 'not null' => FALSE,
  212. 'size' => 'big',
  213. 'serialize' => TRUE,
  214. ));
  215. }
  216. return t('Data column added. Please clear cache.');
  217. }
  218. /**
  219. * Updates vef_video_styles table structure.
  220. */
  221. function video_embed_field_update_7006() {
  222. // Convert the table structure.
  223. db_drop_field('vef_video_styles', 'vsid');
  224. db_add_primary_key('vef_video_styles', array('name'));
  225. db_drop_unique_key('vef_video_styles', 'name');
  226. db_add_field('vef_video_styles', 'title', array(
  227. 'type' => 'varchar',
  228. 'length' => 255,
  229. 'not null' => TRUE,
  230. 'default' => '',
  231. ));
  232. // Update title and name values.
  233. $result = db_select('vef_video_styles', 'vef')
  234. ->fields('vef', array('name'))
  235. ->execute();
  236. foreach ($result as $record) {
  237. // Set current name as title.
  238. db_update('vef_video_styles')
  239. ->fields(array(
  240. 'title' => $record->name,
  241. ))
  242. ->condition('name', $record->name)
  243. ->execute();
  244. // Update name to fit with machine_name constraints.
  245. $new_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($record->name));
  246. if ($new_name != $record->name) {
  247. // Check if new name already exists in the database.
  248. $counter = 1;
  249. $base_name = $new_name;
  250. while (TRUE) {
  251. $result = db_select('vef_video_styles', 'vef')
  252. ->fields('vef', array('name'))
  253. ->condition('name', $new_name)
  254. ->execute();
  255. if ($result->rowCount()) {
  256. $new_name = $base_name . '_' . $counter;
  257. }
  258. else {
  259. db_update('vef_video_styles')
  260. ->fields(array(
  261. 'name' => $new_name,
  262. ))
  263. ->condition('name', $record->name)
  264. ->execute();
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. return t('Database schema updated successfully');
  271. }
  272. /**
  273. * Update youtube "hd" URL deprecated parameter.
  274. */
  275. function video_embed_field_update_7007() {
  276. drupal_get_schema('vef_video_styles', TRUE);
  277. ctools_include('export');
  278. $styles = ctools_export_load_object('vef_video_styles');
  279. foreach ($styles as $style) {
  280. if (isset($style->data['youtube']['hd'])) {
  281. if ($style->data['youtube']['hd']) {
  282. $style->data['youtube']['vq'] = 'hd720';
  283. }
  284. else {
  285. $style->data['youtube']['vq'] = 'large';
  286. }
  287. unset($style->data['youtube']['hd']);
  288. ctools_export_crud_save('vef_video_styles', $style);
  289. }
  290. }
  291. return t('Parameter hd has been converted to vq.');
  292. }
  293. /**
  294. * Updates naming of 'node' formatter setting to 'content'.
  295. */
  296. function video_embed_field_update_7008() {
  297. $instances = field_info_instances();
  298. foreach ($instances as $entity_type) {
  299. foreach ($entity_type as $bundle) {
  300. foreach ($bundle as $instance) {
  301. $field_info = field_info_field($instance['field_name']);
  302. if ($field_info['type'] == 'video_embed_field') {
  303. $update = FALSE;
  304. foreach ($instance['display'] as &$display) {
  305. if ($display['type'] == 'video_embed_field_thumbnail') {
  306. if ($display['settings']['image_link'] == 'node') {
  307. $display['settings']['image_link'] = 'content';
  308. $update = TRUE;
  309. }
  310. if ($display['settings']['image_style'] == 'none') {
  311. $display['settings']['image_style'] = '';
  312. $update = TRUE;
  313. }
  314. }
  315. }
  316. if ($update) {
  317. field_update_instance($instance);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. return t("Updated 'node' setting to 'content'");
  324. }
  325. /**
  326. * Adds new Allowed Providers setting to existing instances.
  327. */
  328. function video_embed_field_update_7009() {
  329. $allowed_providers = array_keys(video_embed_get_handlers());
  330. $instances = field_info_instances();
  331. foreach ($instances as $entity_type) {
  332. foreach ($entity_type as $bundle) {
  333. foreach ($bundle as $instance) {
  334. $field_info = field_info_field($instance['field_name']);
  335. if ($field_info['type'] == 'video_embed_field') {
  336. $instance['settings']['allowed_providers'] = $allowed_providers;
  337. field_update_instance($instance);
  338. }
  339. }
  340. }
  341. }
  342. return t('Updated default instance settings');
  343. }
  344. /**
  345. * Update styles with empty class parameter.
  346. */
  347. function video_embed_field_update_7010() {
  348. drupal_get_schema('vef_video_styles', TRUE);
  349. ctools_include('export');
  350. $styles = ctools_export_load_object('vef_video_styles');
  351. foreach ($styles as $style) {
  352. foreach ($style->data as &$provider) {
  353. if (!isset($provider['class'])) {
  354. $provider['class'] = '';
  355. }
  356. }
  357. ctools_export_crud_save('vef_video_styles', $style);
  358. }
  359. return 'Parameter class added to existing styles';
  360. }