Skip to content

Instantly share code, notes, and snippets.

@marioangulo
Created October 8, 2014 19:36
Show Gist options
  • Save marioangulo/a36b28410e2fdaf0bd8b to your computer and use it in GitHub Desktop.
Save marioangulo/a36b28410e2fdaf0bd8b to your computer and use it in GitHub Desktop.
Drush Field Info
<?php
/**
* @file
* Drush commands for displaying info on fields and field instances.
*
* To use, save this file as drush_field_config.drush.inc in your home directory's .drush/ folder.
*/
/**
* Implements hook_drush_command().
*/
function drush_field_config_drush_command() {
$items['field-config-base'] = array(
'description' => 'Print the configuration for a base field.',
'aliases' => array('fcb'),
'arguments' => array(
'field_name' => 'The machine name of the field.',
),
'options' => array(
'code' => 'Print the configuration as valid PHP code.',
),
);
$items['field-config-instance'] = array(
'description' => 'Print the configuration for a field instance.',
'aliases' => array('fci'),
'arguments' => array(
'field_name' => 'The machine name of the field.',
'bundle' => 'The name of the bundle (\'story\', etc) that the instance is on.',
'entity_type' => 'The type of entity (\'node\', etc) the instance is on. Defaults to \'node\'.',
),
'options' => array(
'display' => 'Show the display configuration instead of the other data.',
'code' => 'Print the configuration as valid PHP code.',
),
);
return $items;
}
function drush_drush_field_config_field_config_base($field_name = NULL) {
if (empty($field_name)) {
print 'Provide the field name, eg, drush fcb field_subtitle' . PHP_EOL;
return;
}
$config = field_info_field($field_name);
if (empty($config)) {
print 'That field does not exist.' . PHP_EOL;
}
$info = field_info_field_types($config['type']);
$settings = (empty($info['settings'])) ? array() : $info['settings'];
$useful_data = array(
'field_name',
'type',
'module',
'cardinality',
);
// Remove excess data.
foreach ($config as $key => $value) {
if ($key == 'settings') {
foreach (array_keys($config['settings']) as $setting) {
if (!isset($settings[$setting])) {
unset($config['settings'][$setting]);
}
}
}
elseif (!in_array($key, $useful_data)) {
unset($config[$key]);
}
}
if (drush_get_option('code', FALSE)) {
var_export($config);
}
else {
print_r($config);
}
}
function drush_drush_field_config_field_config_instance($field_name = NULL, $bundle = NULL, $entity_type = 'node') {
if (empty($bundle) || empty($field_name)) {
print 'Provide the field name and bundle that the instance is on, eg, drush fci field_subtitle article' . PHP_EOL;
return;
}
$config = field_info_instance($entity_type, $field_name, $bundle);
$useful_data = array(
'label',
'required',
'description',
'field_name',
'entity_type',
'bundle',
'widget',
'settings',
);
if (drush_get_option('display', FALSE)) {
$useful_data = array('display');
}
foreach ($config as $key => $value) {
if (!in_array($key, $useful_data)) {
unset($config[$key]);
}
}
if (drush_get_option('code', FALSE)) {
var_export($config);
}
else {
print_r($config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment