Last active
August 29, 2015 14:16
-
-
Save amcgowanca/425f0df358a81c8cbe37 to your computer and use it in GitHub Desktop.
Drupal 7: Restrict available options for View Mode field when creating new for Bean instances
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 | |
*/ | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
* | |
* Retrieves the view mode information for the Bean type and makes available | |
* as an option only those view modes which apply to this Bean by having | |
* custom settings. This ensures the view mode form element is not populated | |
* with irrelevant view modes that do not necessarily apply to this bean. | |
*/ | |
function MODULE_form_bean_form_alter(&$form, &$form_state) { | |
$options = array(); | |
$bean_info = $form['#entity']->entityInfo(); | |
$view_mode_settings = field_view_mode_settings('bean', $form['#entity']->type); | |
foreach ($view_mode_settings as $view_mode => $settings) { | |
if ($settings['custom_settings'] && isset($bean_info['view modes'][$view_mode])) { | |
$options[$view_mode] = $bean_info['view modes'][$view_mode]['label']; | |
} | |
} | |
$form['view_mode']['#options'] = $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just what I needed. Thanks!