0

In my custom theme I am developing, I have a form API setting in theme-settings.php that lets a user choose one of two options with a select menu as such:

 $form['txtr_settings']['general_settings']['choose_slideshow'] = array(
        '#type' => 'select',
        '#title' => t('Choose a slideshow'),
        '#default_value' => theme_get_setting('choose_slideshow'),
        '#options' => array(
            'basic' => t('Basic'),
            'fancy' => t('Fancy'),
        ),
    );

Then in my theme's page.tpl.php I want the value the user has selected to be part of an if else statement.

Right now I have this code which does not seem to work:

<?php if (theme_get_setting('choose_slideshow') == 'basic')
            include(dirname(__FILE__) . "/slideshow_alpha.inc");
        else
            include(dirname(__FILE__) . "/slideshow_beta.inc");
  ?>

I know if I have a simple single checkbox (checked = yes or '1' I can do something like this:

<?php if (theme_get_setting('choose_slideshow') == 1)

... but in this case I really want to show the user both options and have them choose one or the other in the select menu.

If I use either option on it's own without the if / else, each works fine so I think it's a question of my theme_get_setting value. I am not really sure if this is a Drupal question or a PHP question that I should ask over on Stack Exchange.

4
  • Try using drupal_get_path('theme', 'theme_name') in place of dirname(__FILE__)
    – Clive
    Commented Aug 23, 2012 at 21:50
  • @Clive, hmm.. that's not the issue at hand, that part is working fine. I've read documentation and (dirname(__FILE__) is certainly acceptable and it works in the case of doing a php include... Commented Aug 23, 2012 at 21:58
  • Sorry should have paid more attention there. What's actually in theme_get_setting('choose_slideshow') at that point, have you printed it out to the screen?
    – Clive
    Commented Aug 23, 2012 at 22:08
  • well that's darned odd, once I echoed choose_slideshow as a simple body class (added some code to template.php) to see what the value was, after that it all started working but I don't know why... Commented Aug 23, 2012 at 22:44

1 Answer 1

0

The way to really do this was to create variables in template.php using a preprocess function and take some of the logic out of page.tpl.php and to instead use the actual variables created. Based on this, my updated code that's working is:

template.php

function my_theme_preprocess_page(&$variables) {
 // create variables for each slideshow
  $variables['slideshow_alpha'] = (theme_get_setting('choose_slideshow') == 'alpha');
  $variables['slideshow_beta'] = (theme_get_setting('choose_slideshow') == 'beta');
}

... and then in page.tpl.php

 <?php if ($slideshow_alpha): ?>
    <?php require_once dirname(__FILE__) . '/slideshow_alpha.inc'; ?>
  <?php else: ?>
    <?php require_once dirname(__FILE__) . '/slideshow_beta.inc'; ?>
  <?php endif; ?>

It's important to note that what helped me figure much of the code updates out were running it through PAReview.sh. However, I did not want to set up the module and there is an online version which works great but your code needs to be in a git repo on drupal.org so you would have to set up a simple sandbox project to test your code. The online version is on ventral.org and it's what drupal.org uses to review projects. The new updated code also does not throw any errors with the code sniffer an the old code did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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