features.node.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function node_features_api() {
  6. return array(
  7. 'node' => array(
  8. 'name' => t('Content types'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'node_info',
  11. ),
  12. );
  13. }
  14. /**
  15. * Implements hook_features_export_options().
  16. */
  17. function node_features_export_options() {
  18. return node_type_get_names();
  19. }
  20. /**
  21. * Implements hook_features_export.
  22. */
  23. function node_features_export($data, &$export, $module_name = '') {
  24. $pipe = array();
  25. $map = features_get_default_map('node');
  26. foreach ($data as $type) {
  27. // Poll node module to determine who provides the node type.
  28. if ($info = node_type_get_type($type)) {
  29. // If this node type is provided by a different module, add it as a dependency
  30. if (isset($map[$type]) && $map[$type] != $module_name) {
  31. $export['dependencies'][$map[$type]] = $map[$type];
  32. }
  33. // Otherwise export the node type.
  34. elseif (in_array($info->base, array('node_content', 'features'))) {
  35. $export['features']['node'][$type] = $type;
  36. $export['dependencies']['node'] = 'node';
  37. $export['dependencies']['features'] = 'features';
  38. }
  39. $fields = field_info_instances('node', $type);
  40. foreach ($fields as $name => $field) {
  41. $pipe['field_instance'][] = "node-{$field['bundle']}-{$field['field_name']}";
  42. }
  43. }
  44. }
  45. return $pipe;
  46. }
  47. /**
  48. * Implements hook_features_export_render().
  49. */
  50. function node_features_export_render($module, $data, $export = NULL) {
  51. $elements = array(
  52. 'name' => TRUE,
  53. 'base' => FALSE,
  54. 'description' => TRUE,
  55. 'has_title' => FALSE,
  56. 'title_label' => TRUE,
  57. 'help' => TRUE,
  58. );
  59. $output = array();
  60. $output[] = ' $items = array(';
  61. foreach ($data as $type) {
  62. if ($info = node_type_get_type($type)) {
  63. // Force module name to be 'features' if set to 'node. If we leave as
  64. // 'node' the content type will be assumed to be database-stored by
  65. // the node module.
  66. $info->base = ($info->base === 'node') ? 'features' : $info->base;
  67. $output[] = " '{$type}' => array(";
  68. foreach ($elements as $key => $t) {
  69. if ($t) {
  70. $text = str_replace("'", "\'", $info->$key);
  71. $text = !empty($text) ? "t('{$text}')" : "''";
  72. $output[] = " '{$key}' => {$text},";
  73. }
  74. else {
  75. $output[] = " '{$key}' => '{$info->$key}',";
  76. }
  77. }
  78. $output[] = " ),";
  79. }
  80. }
  81. $output[] = ' );';
  82. $output[] = ' return $items;';
  83. $output = implode("\n", $output);
  84. return array('node_info' => $output);
  85. }
  86. /**
  87. * Implements hook_features_revert().
  88. *
  89. * @param $module
  90. * name of module to revert content for
  91. */
  92. function node_features_revert($module = NULL) {
  93. if ($default_types = features_get_default('node', $module)) {
  94. foreach ($default_types as $type_name => $type_info) {
  95. // Delete node types
  96. // We don't use node_type_delete() because we do not actually
  97. // want to delete the node type (and invoke hook_node_type()).
  98. // This can lead to bad consequences like CCK deleting field
  99. // storage in the DB.
  100. db_delete('node_type')
  101. ->condition('type', $type_name)
  102. ->execute();
  103. }
  104. node_types_rebuild();
  105. menu_rebuild();
  106. }
  107. }
  108. /**
  109. * Implements hook_features_disable().
  110. *
  111. * When a features module is disabled, modify any node types it provides so
  112. * they can be deleted manually through the content types UI.
  113. *
  114. * @param $module
  115. * Name of module that has been disabled.
  116. */
  117. function node_features_disable($module) {
  118. if ($default_types = features_get_default('node', $module)) {
  119. foreach ($default_types as $type_name => $type_info) {
  120. $type_info = node_type_load($type_name);
  121. $type_info->module = 'node';
  122. $type_info->custom = 1;
  123. $type_info->modified = 1;
  124. $type_info->locked = 0;
  125. node_type_save($type_info);
  126. }
  127. }
  128. }
  129. /**
  130. * Implements hook_features_enable().
  131. *
  132. * When a features module is enabled, modify any node types it provides so
  133. * they can no longer be deleted manually through the content types UI.
  134. *
  135. * @param $module
  136. * Name of module that has been enabled.
  137. */
  138. function node_features_enable($module) {
  139. if ($default_types = features_get_default('node', $module)) {
  140. foreach ($default_types as $type_name => $type_info) {
  141. // Ensure the type exists.
  142. if ($type_info = node_type_load($type_name)) {
  143. $type_info->module = $module;
  144. $type_info->custom = 0;
  145. $type_info->modified = 0;
  146. $type_info->locked = 1;
  147. node_type_save($type_info);
  148. }
  149. }
  150. }
  151. }