Last active
July 8, 2021 09:54
-
-
Save robballou/a7aa247aa7bdfb3a1b2c to your computer and use it in GitHub Desktop.
Example stub-content function for Drupal Migrate with YAML files
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 | |
/** | |
* Implements hook_drush_command(). | |
*/ | |
function example_drush_command() { | |
$commands = array(); | |
$commands['stub-content'] = array( | |
'callback' => 'drush_stub_content', | |
'description' => 'Output YAML for a content type', | |
'aliases' => array('ssc'), | |
'examples' => array( | |
), | |
'arguments' => array( | |
'type' => 'The bundle name', | |
'entity_type' => 'The entity type. Defaults to node', | |
'count' => 'The number of items', | |
), | |
'options' => array( | |
'count' => array( | |
'description' => 'Number of items to create', | |
'default' => 1, | |
), | |
'include-id' => array( | |
'description' => 'Include an auto-generated ID in the id field', | |
), | |
'include-title' => array( | |
'description' => 'Include an auto-generated title in the title field', | |
) | |
), | |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, | |
); | |
return $commands; | |
} | |
/** | |
* List fields in an entity/bundle | |
* | |
* @param string $type | |
* A string for the bundle. | |
* @param string $entity_type | |
* The entity type for this bundle. Defaults to node. | |
*/ | |
function drush_stub_content($type = NULL, $entity_type = 'node') { | |
// if no types are passed, output types | |
if (!$type) { | |
return drush_typeinfo_list(); | |
} | |
$type_info = entity_get_info($entity_type); | |
if (!isset($type_info['bundles'][$type])) { | |
drush_log(t('The requested type does not exist. Try: drush typeinfo-list'), 'error'); | |
return; | |
} | |
$label = $type_info['bundles'][$type]['label']; | |
$field_info = field_info_instances($entity_type, $type); | |
$fields = array( | |
'id', | |
'title' | |
); | |
foreach ($field_info as $field => $info) { | |
array_push($fields, $field); | |
} | |
$output = ''; | |
foreach (range(1, drush_get_option('count', 1)) as $item) { | |
$output .= "---\n"; | |
foreach ($fields as $field) { | |
$value = ''; | |
if (drush_get_option('include-id') && $field === 'id') { | |
$value = $item; | |
} | |
elseif (drush_get_option('include-title') && $field === 'title') { | |
$value = ucfirst(str_replace('_', ' ', $type)) . ' ' . $item; | |
} | |
$output .= " $field: $value\n"; | |
} | |
} | |
print $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment