features.node.inc 4.7 KB

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