debut_media.install 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * Install file for debut media. Includes field and instance definitions.
  5. * Based on approaches from media gallery.
  6. */
  7. /**
  8. * Implements hook_install().
  9. *
  10. * Add "Tags", "Title", and "Description" fields to files.
  11. *
  12. * @see media_gallery_install().
  13. */
  14. function debut_media_install() {
  15. // Make sure the standard 'field_tags' field exists.
  16. _debut_media_ensure_vocabulary_tags();
  17. }
  18. /**
  19. * Implements hook_requirements().
  20. */
  21. function debut_media_requirements() {
  22. $requirements = array();
  23. $t = get_t();
  24. $required_fields = _debut_media_controlled_fields();
  25. // In addition to the fields we control, we also need the standard field_tags
  26. // that most sites will have gotten from their install profile.
  27. $required_fields['field_tags'] = array('type' => 'taxonomy_term_reference');
  28. foreach ($required_fields as $field_name => $field_definition) {
  29. $field = field_info_field($field_name);
  30. // If the field doesn't exist, we will create it on install.
  31. if (!$field) {
  32. continue;
  33. }
  34. if ($field['type'] != $field_definition['type']) {
  35. $requirements['existing_field_' . $field_name] = array(
  36. 'description' => $t("%field_name already exists and is not of type %type. Installation cannot continue. Please remove this field or change its type.", array('%field_name' => $field_name, '%type' => $field_definition['type'])),
  37. 'severity' => REQUIREMENT_ERROR,
  38. );
  39. }
  40. }
  41. return $requirements;
  42. }
  43. /**
  44. * Make sure the field_tags field exists and is of the right type.
  45. */
  46. function _debut_media_ensure_vocabulary_tags() {
  47. // Make sure the 'tags' vocabulary exists.
  48. $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
  49. if (!$vocabulary) {
  50. $description = st('Use tags to group articles on similar topics into categories.');
  51. $help = st('Enter a comma-separated list of words to describe your content.');
  52. $vocabulary = (object) array(
  53. 'name' => 'Tags',
  54. 'description' => $description,
  55. 'machine_name' => 'tags',
  56. 'help' => $help,
  57. );
  58. taxonomy_vocabulary_save($vocabulary);
  59. }
  60. }
  61. /**
  62. * Returns definitions for fields this module both creates and deletes.
  63. *
  64. * @see _media_gallery_controlled_fields().
  65. */
  66. function _debut_media_controlled_fields() {
  67. $fields = array(
  68. // Fields to create on media items.
  69. 'media_description' => array(
  70. 'field_name' => 'media_description',
  71. 'locked' => TRUE,
  72. 'type' => 'text_long',
  73. 'translatable' => TRUE,
  74. ),
  75. 'media_title' => array(
  76. 'field_name' => 'media_title',
  77. 'locked' => TRUE,
  78. 'type' => 'text',
  79. 'translatable' => TRUE,
  80. ),
  81. );
  82. return $fields;
  83. }
  84. /**
  85. * Implements hook_uninstall().
  86. */
  87. function debut_media_uninstall() {
  88. // Delete fields and instances.
  89. foreach (array_keys(_debut_media_controlled_fields()) as $field) {
  90. field_delete_field($field);
  91. }
  92. }