Created
July 1, 2021 14:54
-
-
Save eojthebrave/2970c474f7fa223d97d84decd609cd9b to your computer and use it in GitHub Desktop.
Output a CSV file listing all fields and properties for Drupal 7 entity types to help prepare for a migration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Create a CSV file with fields for every Drupal 7 entity type / bundle. | |
* | |
* When preparing for a Drupal 7 to Drupal 8/9 migration it can be helpful to | |
* perform some initial content analysis. This script creates CSV files that | |
* can be imported into Google Sheets etc. to help with this analysis. | |
* | |
* Example: https://docs.google.com/spreadsheets/d/1Ey1bFb6MJiUHUW3YVVavJiwIxoR_B_Nb1czLGDntO-s/edit?usp=sharing | |
* | |
* Usage: | |
* Copy this file to the root of your Drupal 7 and project and exucute the | |
* script via Drush (drush.org). | |
* | |
* drush scr entity_fields.php | |
* | |
* Will output a directory named 'entity_fields/' with a CSV file for each | |
* entity + bundle combo. | |
*/ | |
$directory = __DIR__ . '/entity_fields'; | |
if (!is_dir($directory)) { | |
mkdir($directory); | |
} | |
$types = entity_get_info(); | |
foreach ($types as $entity_type => $info) { | |
print 'Export info for ' . $info['label'] . ' (' . $entity_type .')' . PHP_EOL; | |
foreach ($info['bundles'] as $bundle => $bundle_info) { | |
$fields = []; | |
$fields[] = [ | |
'Machine name', 'Label', 'Type', 'Widget' | |
]; | |
$field_info = field_info_fields($entity_type, $bundle); | |
$instances = field_info_instances($entity_type, $bundle); | |
$extra_fields = field_info_extra_fields($entity_type, $bundle, 'form'); | |
// Base fields. | |
foreach ($info['base table field types'] as $name => $type) { | |
$fields[] = [ | |
'name' => $name, | |
'title' => $name, | |
'field_type' => $type, | |
'widget' => 'base field', | |
]; | |
} | |
// Field API fields. | |
foreach ($instances as $name => $instance) { | |
$field = field_info_field($instance['field_name']); | |
$fields[] = [ | |
'name' => $instance['field_name'], | |
'title' => $instance['label'], | |
'field_type' => $field_info[$name]['type'], | |
'widget' => $instance['widget']['type'], | |
]; | |
} | |
// Non-field elements. | |
foreach ($extra_fields as $name => $extra_field) { | |
$fields[] = [ | |
'name' => $name, | |
'title' => $extra_field['label'], | |
'field_type' => '', | |
'widget' => '', | |
]; | |
} | |
$filename = $directory . '/' . $entity_type . '-' . $bundle . '.csv'; | |
$fp = fopen($filename, 'w'); | |
foreach ($fields as $row) { | |
fputcsv($fp, $row); | |
} | |
fclose($fp); | |
print 'Wrote data to file: ' . $filename . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment