| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: features.content.inc,v 1.1.2.19 2010/08/04 22:39:55 yhahn Exp $ 3 4 /** 5 * Implementation of hook_features_api(). 6 */ 7 function content_features_api() { 8 return array( 9 'content' => array( 10 'name' => t('CCK'), 11 'default_hook' => 'content_default_fields', 12 'default_file' => FEATURES_DEFAULTS_INCLUDED, 13 'feature_source' => TRUE, 14 ), 15 ); 16 } 17 18 /** 19 * Implementation of hook_features_export_options(). 20 */ 21 function content_features_export_options() { 22 $options = array(); 23 foreach (node_get_types('names') as $type => $type_name) { 24 $content_info = content_types($type); 25 if (!empty($content_info['fields'])) { 26 foreach ($content_info['fields'] as $field) { 27 $field_name = isset($field['widget']['label']) ? $field['widget']['label'] : $field['field_name']; 28 $options[content_features_identifier($field)] = "{$type_name}: {$field_name}"; 29 } 30 } 31 } 32 return $options; 33 } 34 35 /** 36 * Implementation of hook_features_export(). 37 */ 38 function content_features_export($data, &$export, $module_name = '') { 39 features_include_defaults('content'); 40 $pipe = array(); 41 42 // The content_default_fields() hook integration is provided by the 43 // features module so we need to add it as a dependency. 44 $export['dependencies']['features'] = 'features'; 45 46 // Collect a field to module map 47 $map = features_get_default_map('content', NULL, 'content_features_identifier'); 48 foreach ($data as $instance) { 49 // If this field is already provided by another module remove the field. 50 // We do not add the other module as a dependency as the field-providing 51 // module may be extending the content type in question. 52 if (isset($map[$instance]) && $map[$instance] != $module_name) { 53 if (isset($export['features']['content'][$instance])) { 54 unset($export['features']['content'][$instance]); 55 } 56 } 57 // If the field has not yet been exported, add it 58 else { 59 $split = explode('-', $instance); 60 $type_name = $split[0]; 61 $field_name = $split[1]; 62 $field = content_fields($field_name, $type_name); 63 64 if ($field) { 65 // Add field item instance for later export. 66 $export['features']['content'][$instance] = $instance; 67 68 // Add module which provides field. 69 $export['dependencies'][$field['module']] = $field['module']; 70 71 // Add module which provides field widget. 72 $export['dependencies'][$field['widget']['module']] = $field['widget']['module']; 73 74 // Add modules which provide display. 75 foreach (array('teaser', 'full') as $display) { 76 $formatter = _content_get_formatter($field['display_settings'][$display]['format'], $field['type']); 77 $export['dependencies'][$formatter['module']] = $formatter['module']; 78 79 // TODO make this logic more generic, for now though we just handle 80 // the imagecache presets. 81 if ($formatter['module'] == 'imagecache') { 82 $format = $field['display_settings'][$display]['format']; 83 84 $parts = explode('_', $format); 85 $style = array_pop($parts); 86 $presetname = implode('_', $parts); 87 88 $pipe[$formatter['module']][] = $presetname; 89 } 90 } 91 } 92 } 93 } 94 95 return $pipe; 96 } 97 98 /** 99 * Implementation of hook_features_export_render(). 100 */ 101 function content_features_export_render($module, $data) { 102 $translatables = $code = array(); 103 104 $code[] = ' $fields = array();'; 105 $code[] = ''; 106 foreach ($data as $instance) { 107 $instance = explode('-', $instance); 108 $type_name = $instance[0]; 109 $field_name = $instance[1]; 110 if ($field = content_fields($field_name, $type_name)) { 111 unset($field['columns']); 112 unset($field['locked']); 113 unset($field['db_storage']); 114 $field_identifier = features_var_export(content_features_identifier($field)); 115 $field_export = features_var_export($field, ' '); 116 117 $code[] = " // Exported field: {$field_name}"; 118 $code[] = " \$fields[{$field_identifier}] = {$field_export};"; 119 $code[] = ""; 120 121 // Add any labels to translatables array. 122 if (!empty($field['widget']['label'])) { 123 $translatables[] = $field['widget']['label']; 124 } 125 } 126 } 127 if (!empty($translatables)) { 128 $code[] = features_translatables_export($translatables, ' '); 129 } 130 $code[] = ' return $fields;'; 131 $code = implode("\n", $code); 132 return array('content_default_fields' => $code); 133 } 134 135 /** 136 * Implementation of hook_features_revert(). 137 */ 138 function content_features_revert($module) { 139 content_features_rebuild($module); 140 } 141 142 /** 143 * Implementation of hook_features_rebuild(). 144 * Rebuilds CCK field definitions from code defaults. 145 */ 146 function content_features_rebuild($module) { 147 if ($fields = features_get_default('content', $module)) { 148 module_load_include('inc', 'content', 'includes/content.crud'); 149 content_clear_type_cache(TRUE); 150 151 foreach ($fields as $field) { 152 // We use content_field_instance_read() here so that we can get inactive 153 // fields too. We can't just pass $field to it as the fnc will add every 154 // item of the array to the WHERE clause. 155 $param = array('field_name' => $field['field_name'], 'type_name' => $field['type_name']); 156 $existing_instance = content_field_instance_read($param, TRUE); 157 158 if ($existing_instance) { 159 // If this existing instance is inactive, we need to activate it before 160 // running content_field_instance_update(). 161 if (!$existing_instance['widget_active']) { 162 db_query("UPDATE {". content_instance_tablename() ."} SET widget_active = 1 WHERE field_name = '%s' AND type_name = '%s'", $field['field_name'], $field['type_name']); 163 // We need to clear the type cache again, unfortunately. 164 content_clear_type_cache(TRUE); 165 } 166 167 content_field_instance_update($field, FALSE); 168 } 169 else { 170 // Summary: content_field_instance_create() will fall back to prior 171 // instance values for weight, label, desc if not set. 172 // See http://drupal.org/node/686240#comment-2786162 173 $field['weight'] = $field['widget']['weight']; 174 $field['label'] = $field['widget']['label']; 175 $field['description'] = $field['widget']['description']; 176 content_field_instance_create($field, FALSE); 177 } 178 variable_set('menu_rebuild_needed', TRUE); 179 } 180 } 181 } 182 183 /** 184 * Callback for generating the field exportable identifier for a field. 185 */ 186 function content_features_identifier($field) { 187 return isset($field['type_name'], $field['field_name']) ? "{$field['type_name']}-{$field['field_name']}" : FALSE; 188 } 189 190 /** 191 * Helper function: retrieve default fields by node type. 192 */ 193 function content_features_fields_default($node_type, $reset = FALSE) { 194 static $content_default_fields; 195 if (!isset($content_default_fields) || $reset) { 196 foreach (features_get_default('content') as $field) { 197 $content_default_fields[$field['type_name']][] = $field['field_name']; 198 } 199 } 200 return isset($content_default_fields[$node_type]) ? $content_default_fields[$node_type] : array(); 201 } 202 203 /** 204 * Helper function: retrieve normal fields by node type. 205 */ 206 function content_features_fields_normal($node_type) { 207 $normal_fields = array(); 208 $content_info = content_types($node_type); 209 if (!empty($content_info['fields'])) { 210 $normal_fields = array_keys($content_info['fields']); 211 } 212 return $normal_fields; 213 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |