Skip to content

Instantly share code, notes, and snippets.

@krisahil
Last active September 18, 2020 15:39
Show Gist options
  • Save krisahil/11057840 to your computer and use it in GitHub Desktop.
Save krisahil/11057840 to your computer and use it in GitHub Desktop.
Drupal 6: Export all fields and their content types to CSV
<?php
$content_types = node_get_types();
$header = array(
'Source content type',
'Source content type machine name',
'Source field name',
'Source field machine name',
);
$sql_field_type_collector = "
SELECT `field_name`, `label`
FROM `content_node_field_instance` nfi
WHERE nfi.`type_name` = '%s'
";
$rows = array();
foreach (array_keys($content_types) as $type) {
$rows[] = array(
$content_types[$type]->name,
$type,
'---',
'---',
);
$sql_field_type_collector_result = db_query($sql_field_type_collector, $type);
while ($field = db_fetch_array($sql_field_type_collector_result)) {
$field_info = content_fields($field['field_name'], $type);
$rows[] = array(
$content_types[$type]->name,
$type,
$field_info['widget']['label'],
$field['field_name'],
);
}
}
$out_file = 'fields.csv';
$fp = fopen($out_file, 'w');
fputcsv($fp, $header);
foreach ($rows as $row) {
fputcsv($fp, $row);
}
fclose($fp);
@JacobDB
Copy link

JacobDB commented Sep 9, 2020

This looks like exactly what I'm looking for, but how do I actually use this code? Do I just pop it in the template.php?

@krisahil
Copy link
Author

@JacobDB, instead of putting it in template.php, I recommend using Devel module. You can use adhoc PHP at URL path /devel/php. Another alternative is to run this by Drush's php-script command.
Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment