By misc on
In a module that I am working at, I want to have to possibility to choose avaible view modes from a select list, but for now I only know how to get the label ("Human") name, but I want the machine name, so how do I do that? My code for now looks like this:
$available_view_modes = array();
//entity_get_info feteches the info of avaible view modes
$entity_info = entity_get_info();
if (empty($available_view_modes)) {
$entity_info = entity_get_info();
foreach ($entity_info['node']['view modes'] as $key => $view_mode) {
dpm($view_mode);
$available_view_modes[] = $view_mode['label'];
}
}
$form['view_mode'] = array(
'#type' => 'select',
'#description' => t("View modes."),
'#options' => $available_view_modes,
'#default_value' => !empty($conf['view_mode']) ? $conf['view_mode'] : '0',
);
Comments
This is how I solved it
Not so nice looking code, but it works, if somebody have a better solution, which I am sure of, please share :-)
// Mikke Schirén @ https://digitalist.se/
Get machine name of view modes
I solved it a bit differently because I wanted to have only the modes which are defined with the node type.
Then just use $view_modes in your form element.
Hope this helps someone.