Skip to content

Instantly share code, notes, and snippets.

@amcgowanca
Last active August 29, 2015 14:16
Show Gist options
  • Save amcgowanca/425f0df358a81c8cbe37 to your computer and use it in GitHub Desktop.
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
<?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;
}
@sherakama
Copy link

Just what I needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment