rdf_example.install 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Install file for RDF Example module.
  5. *
  6. * To demonstrate hook_rdf_mapping, this module creates it's own node type. For
  7. * more information on creating node types, see Node Example.
  8. */
  9. /**
  10. * Implements hook_install().
  11. *
  12. * - Create photo, summary fields.
  13. * - Create photo, summary instances.
  14. *
  15. * @see node_type_set_defaults()
  16. * @see field_info_instance()
  17. * @see field_update_instance()
  18. * @see field_create_field()
  19. * @see field_create_instance()
  20. *
  21. * @ingroup rdf_example
  22. */
  23. function rdf_example_install() {
  24. // Use get_t() to get the name of our localization function for translation
  25. // during install, when t() is not available.
  26. $t = get_t();
  27. // Define the node type.
  28. $rdf_example = array(
  29. 'type' => 'recipe',
  30. 'name' => $t('Recipe'),
  31. 'base' => 'node_content',
  32. 'description' => $t('The recipe node is defined to demonstrate RDF mapping.'),
  33. );
  34. // Set additional defaults and save the content type.
  35. $content_type = node_type_set_defaults($rdf_example);
  36. node_type_save($content_type);
  37. // Create all the fields we are adding to our content type.
  38. // http://api.drupal.org/api/function/field_create_field/7
  39. foreach (_rdf_example_installed_fields() as $field) {
  40. field_create_field($field);
  41. }
  42. // Create all the instances for our fields.
  43. // http://api.drupal.org/api/function/field_create_instance/7
  44. foreach (_rdf_example_installed_instances() as $instance) {
  45. $instance['entity_type'] = 'node';
  46. $instance['bundle'] = $rdf_example['type'];
  47. field_create_instance($instance);
  48. }
  49. }
  50. /**
  51. * Return a structured array defining the fields created by this content type.
  52. *
  53. * @ingroup rdf_example
  54. */
  55. function _rdf_example_installed_fields() {
  56. $t = get_t();
  57. return array(
  58. 'recipe_photo' => array(
  59. 'field_name' => 'recipe_photo',
  60. 'cardinality' => 1,
  61. 'type' => 'image',
  62. ),
  63. 'recipe_summary' => array(
  64. 'field_name' => 'recipe_summary',
  65. 'cardinality' => 1,
  66. 'type' => 'text',
  67. 'settings' => array(
  68. 'max_length' => 500,
  69. ),
  70. ),
  71. );
  72. }
  73. /**
  74. * Return a structured array defining the instances for this content type.
  75. *
  76. * @ingroup rdf_example
  77. */
  78. function _rdf_example_installed_instances() {
  79. $t = get_t();
  80. return array(
  81. 'recipe_photo' => array(
  82. 'field_name' => 'recipe_photo',
  83. 'label' => $t('Photo of the prepared dish'),
  84. ),
  85. 'recipe_summary' => array(
  86. 'field_name' => 'recipe_summary',
  87. 'label' => $t('Short summary describing the dish'),
  88. 'widget' => array(
  89. 'type' => 'text_textarea',
  90. ),
  91. ),
  92. );
  93. }
  94. /**
  95. * Implements hook_uninstall().
  96. *
  97. * @ingroup rdf_example
  98. */
  99. function rdf_example_uninstall() {
  100. // Delete recipe content.
  101. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  102. $result = db_query($sql, array(':type' => 'recipe'));
  103. $nids = array();
  104. foreach ($result as $row) {
  105. $nids[] = $row->nid;
  106. }
  107. node_delete_multiple($nids);
  108. // Delete field instances for now.
  109. // Check status of http://drupal.org/node/1015846
  110. $instances = field_info_instances('node', 'recipe');
  111. foreach ($instances as $instance_name => $instance) {
  112. field_delete_instance($instance);
  113. }
  114. // Delete node type.
  115. node_type_delete('recipe');
  116. field_purge_batch(1000);
  117. }