video_embed_field.install 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * Adds an optional description form.
  95. */
  96. function video_embed_field_update_7000() {
  97. // Get the list of fields of type 'video_embed_field'.
  98. $video_embed_fields = array();
  99. foreach (field_info_fields() as $field_name => $field_info) {
  100. if ($field_info['type'] == 'video_embed_field') {
  101. $video_embed_fields[$field_name] = field_read_field($field_name);
  102. }
  103. }
  104. foreach ($video_embed_fields as $field) {
  105. if ($field['deleted']) {
  106. $table = "field_deleted_data_{$field['id']}";
  107. $revision_table = "field_deleted_revision_{$field['id']}";
  108. }
  109. else {
  110. $table = "field_data_{$field['field_name']}";
  111. $revision_table = "field_revision_{$field['field_name']}";
  112. }
  113. $column = $field['field_name'] . '_description';
  114. db_add_field($table, $column, array('type' => 'text', 'not null' => FALSE));
  115. db_add_field($revision_table, $column, array('type' => 'text', 'not null' => FALSE));
  116. }
  117. return t('Additional columns added.');
  118. }
  119. /**
  120. * Adds video style storage table.
  121. */
  122. function video_embed_field_update_7001() {
  123. if (!db_table_exists('vef_video_styles')) {
  124. $schema = video_embed_field_schema();
  125. db_create_table('vef_video_styles', $schema['vef_video_styles']);
  126. }
  127. return t('Video styles storage table created.');
  128. }
  129. /**
  130. * Adds field for storing the path to the video thumbnail.
  131. */
  132. function video_embed_field_update_7002() {
  133. // Get the list of fields of type 'video_embed_field'.
  134. $video_embed_fields = array();
  135. foreach (field_info_fields() as $field_name => $field_info) {
  136. if ($field_info['type'] == 'video_embed_field') {
  137. $video_embed_fields[$field_name] = field_read_field($field_name);
  138. }
  139. }
  140. foreach ($video_embed_fields as $field) {
  141. if ($field['deleted']) {
  142. $table = "field_deleted_data_{$field['id']}";
  143. $revision_table = "field_deleted_revision_{$field['id']}";
  144. }
  145. else {
  146. $table = "field_data_{$field['field_name']}";
  147. $revision_table = "field_revision_{$field['field_name']}";
  148. }
  149. $column = $field['field_name'] . '_thumbnail_path';
  150. db_add_field($table, $column, array(
  151. 'type' => 'varchar',
  152. 'length' => 512,
  153. 'default' => '',
  154. ));
  155. db_add_field($revision_table, $column, array(
  156. 'type' => 'varchar',
  157. 'length' => 512,
  158. 'default' => '',
  159. ));
  160. }
  161. return t('Thumbnail column added.');
  162. }
  163. /**
  164. * Enables inline colorbox support if colorbox is installed.
  165. *
  166. * [NO LONGER NEEDED - This update hook does nothing]
  167. */
  168. function video_embed_field_update_7003() {
  169. }
  170. /**
  171. * Enables colorbox load support if colorbox is installed.
  172. */
  173. function video_embed_field_update_7004() {
  174. variable_set('colorbox_load', 1);
  175. }
  176. /**
  177. * Adds data column to field database.
  178. */
  179. function video_embed_field_update_7005() {
  180. // Get the list of fields of type 'video_embed_field'.
  181. $video_embed_fields = array();
  182. foreach (field_info_fields() as $field_name => $field_info) {
  183. if ($field_info['type'] == 'video_embed_field') {
  184. $video_embed_fields[$field_name] = field_read_field($field_name);
  185. }
  186. }
  187. foreach ($video_embed_fields as $field) {
  188. if ($field['deleted']) {
  189. $table = "field_deleted_data_{$field['id']}";
  190. $revision_table = "field_deleted_revision_{$field['id']}";
  191. }
  192. else {
  193. $table = "field_data_{$field['field_name']}";
  194. $revision_table = "field_revision_{$field['field_name']}";
  195. }
  196. $column = $field['field_name'] . '_video_data';
  197. db_add_field($table, $column, array(
  198. 'type' => 'blob',
  199. 'not null' => FALSE,
  200. 'size' => 'big',
  201. 'serialize' => TRUE,
  202. ));
  203. db_add_field($revision_table, $column, array(
  204. 'type' => 'blob',
  205. 'not null' => FALSE,
  206. 'size' => 'big',
  207. 'serialize' => TRUE,
  208. ));
  209. }
  210. return t('Data column added. Please clear cache.');
  211. }
  212. /**
  213. * Updates vef_video_styles table structure.
  214. */
  215. function video_embed_field_update_7006() {
  216. // Convert the table structure.
  217. db_drop_field('vef_video_styles', 'vsid');
  218. db_add_primary_key('vef_video_styles', array('name'));
  219. db_drop_unique_key('vef_video_styles', 'name');
  220. db_add_field('vef_video_styles', 'title', array(
  221. 'type' => 'varchar',
  222. 'length' => 255,
  223. 'not null' => TRUE,
  224. 'default' => '',
  225. ));
  226. // Update title and name values.
  227. $result = db_select('vef_video_styles', 'vef')
  228. ->fields('vef', array('name'))
  229. ->execute();
  230. foreach ($result as $record) {
  231. // Set current name as title.
  232. db_update('vef_video_styles')
  233. ->fields(array(
  234. 'title' => $record->name,
  235. ))
  236. ->condition('name', $record->name)
  237. ->execute();
  238. // Update name to fit with machine_name constraints.
  239. $new_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($record->name));
  240. if ($new_name != $record->name) {
  241. // Check if new name already exists in the database.
  242. $counter = 1;
  243. $base_name = $new_name;
  244. while (TRUE) {
  245. $result = db_select('vef_video_styles', 'vef')
  246. ->fields('vef', array('name'))
  247. ->condition('name', $new_name)
  248. ->execute();
  249. if ($result->rowCount()) {
  250. $new_name = $base_name . '_' . $counter;
  251. }
  252. else {
  253. db_update('vef_video_styles')
  254. ->fields(array(
  255. 'name' => $new_name,
  256. ))
  257. ->condition('name', $record->name)
  258. ->execute();
  259. break;
  260. }
  261. }
  262. }
  263. }
  264. return t('Database schema updated successfully');
  265. }
  266. /**
  267. * Update youtube "hd" URL deprecated parameter.
  268. */
  269. function video_embed_field_update_7007() {
  270. drupal_get_schema('vef_video_styles', TRUE);
  271. ctools_include('export');
  272. $styles = ctools_export_load_object('vef_video_styles');
  273. foreach ($styles as $style) {
  274. if (isset($style->data['youtube']['hd'])) {
  275. if ($style->data['youtube']['hd']) {
  276. $style->data['youtube']['vq'] = 'hd720';
  277. }
  278. else {
  279. $style->data['youtube']['vq'] = 'large';
  280. }
  281. unset($style->data['youtube']['hd']);
  282. ctools_export_crud_save('vef_video_styles', $style);
  283. }
  284. }
  285. return t('Parameter hd has been converted to vq.');
  286. }
  287. /**
  288. * Updates naming of 'node' formatter setting to 'content'.
  289. */
  290. function video_embed_field_update_7008() {
  291. $instances = field_info_instances();
  292. foreach ($instances as $entity_type) {
  293. foreach ($entity_type as $bundle) {
  294. foreach ($bundle as $instance) {
  295. $field_info = field_info_field($instance['field_name']);
  296. if ($field_info['type'] == 'video_embed_field') {
  297. $update = FALSE;
  298. foreach ($instance['display'] as &$display) {
  299. if ($display['type'] == 'video_embed_field_thumbnail') {
  300. if ($display['settings']['image_link'] == 'node') {
  301. $display['settings']['image_link'] = 'content';
  302. $update = TRUE;
  303. }
  304. if ($display['settings']['image_style'] == 'none') {
  305. $display['settings']['image_style'] = '';
  306. $update = TRUE;
  307. }
  308. }
  309. }
  310. if ($update) {
  311. field_update_instance($instance);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. return t("Updated 'node' setting to 'content'");
  318. }
  319. /**
  320. * Adds new Allowed Providers setting to existing instances.
  321. */
  322. function video_embed_field_update_7009() {
  323. $allowed_providers = array_keys(video_embed_get_handlers());
  324. $instances = field_info_instances();
  325. foreach ($instances as $entity_type) {
  326. foreach ($entity_type as $bundle) {
  327. foreach ($bundle as $instance) {
  328. $field_info = field_info_field($instance['field_name']);
  329. if ($field_info['type'] == 'video_embed_field') {
  330. $instance['settings']['allowed_providers'] = $allowed_providers;
  331. field_update_instance($instance);
  332. }
  333. }
  334. }
  335. }
  336. return t('Updated default instance settings');
  337. }