1

I am using theme settings in my Drupal 7 theme to provide a theme option as follows:

$form['layout']['body_image_position'] = array(
  '#type' => 'select', 
  '#title' => t('How should Body Image be Positioned'), 
  '#options' => array(
        'body_image_position_right' => t('Image on Right'),
        'body_image_position_bottom' => t('Image on Left'),
        'body_image_position_left' => t('Image Below'),
         ),
  '#default_value' => 'body_image_position_right',

);

I then use theme_get_setting('body_image_position') to call the value of the setting.

Using this command returns the 'machine name' of the setting (e.g. 'body_image_position_right '). But I want the 'friendly name' (e.g. 'Image on Right'). How can I do this?

  • If you are not showing the value to the user, it is better to use the machine name. That value doesn't change when the language used for the interface is changed. If you just to know which option has been selected using the friendly name, you should compare it (for example) with t('Image on Right'), not with 'Image on Right'. – kiamlaluno Jan 2 '13 at 12:32
2

The easiest way would be to use a helper function to provide your positions which you can then reference back to. For example

// In the form function
$form['layout']['body_image_position'] = array(
  '#type' => 'select', 
  '#title' => t('How should Body Image be Positioned'), 
  '#options' => MYTHEME_position_settings()
  '#default_value' => 'body_image_position_right'
);

function MYTHEME_position_settings() {
  return array(
    'body_image_position_right' => t('Image on Right'),
    'body_image_position_bottom' => t('Image on Left'),
    'body_image_position_left' => t('Image Below'),
  );
}

// In the retrieval function
$key = theme_get_setting('body_image_position');
$settings = MYTHEME_position_settings();
$friendly_name = $settings[$key];

The form submission will essentially ignore the label for checkboxes/radios/select lists as it only deals with the key so you'll need to implement some sort of mechanism like this to get at your original labels.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.