Last active
August 29, 2015 14:01
-
-
Save netsensei/595557df1407a24494c4 to your computer and use it in GitHub Desktop.
Command All Field Labels. A Drush command that allows you to hide/show field instance labels in bulk from the command line.
This file contains 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 | |
* Command All Field Labels | |
* | |
* Field labels can be pesky to maintain. Take control back over your field | |
* instance labels. Set field instance labels in bulk with this drush command. | |
* | |
* Show a single field instance label for a specific view mode | |
* $ drush cafl body node|article|full above | |
* | |
* Hide a single field instance label for a specific view mode | |
* $ drush cafl body node|article|full hidden | |
* | |
* Show a field instance label shared across multiple bundles, for a view mode | |
* $ drush cafl body node|*|full above | |
* | |
* Show a field instance label for a specific bundle, on all enabled view modes | |
* $ drush cafl body node|article|* above | |
*/ | |
/** | |
* Implements hook_drush_command(). | |
*/ | |
function cafl_drush_command() { | |
$items = array(); | |
$items['cafl'] = array( | |
'arguments' => array( | |
'field_name' => 'The field name', | |
'context' => 'Reference to the instance context ie. node|article|full', | |
'state' => 'Position of the label: either hidden or above', | |
), | |
'callback' => 'drush_cafl', | |
); | |
return $items; | |
} | |
/** | |
* Drush command comamnd all field labels | |
*/ | |
function drush_cafl($field_name, $context, $state) { | |
list($entity_type, $bundle, $view_mode) = explode('|', $context); | |
$entity_info = entity_get_info($entity_type); | |
if (!in_array($state, array('hidden', 'above'))) { | |
return drush_set_error(dt('State should be either hidden or above')); | |
} | |
if ($entity_info) { | |
$bundles = array(); | |
if ($bundle != '*') { | |
if (isset($entity_info['bundles'][$bundle])) { | |
$bundles = array($bundle => $entity_info['bundles'][$bundle]); | |
} | |
} | |
else { | |
$bundles = $entity_info['bundles']; | |
} | |
foreach ($bundles as $bundle => $bundle_info) { | |
$view_modes = array(); | |
$view_mode_settings = field_view_mode_settings($entity_type, $bundle); | |
$view_modes = ($view_mode != '*') ? array($view_mode) : array_keys($view_mode_settings); | |
foreach ($view_modes as $view_mode) { | |
$actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) | |
? $view_mode : 'default'); | |
$instance = field_read_instance($entity_type, $field_name, $bundle); | |
if ($instance) { | |
$display = &$instance['display'][$actual_mode]; | |
$display['label'] = $state; | |
field_update_instance($instance); | |
$current_context = $entity_type . '|' . $bundle . '|' . $actual_mode; | |
drush_log(dt('Label: \'@state\' for instance \'@field\' on \'@context\'' | |
, array('@state' => $state, '@field' => $field_name, '@context' => | |
$current_context)), 'success'); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment