“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
– Brad Bowman
Roger Black’s Advice for Young Designers
“On the skillset side, what you’ve got to do as a young person entering the field, is really equip yourself with some basic development skills. You have to be able to understand HTML and CSS. You have to know what JavaScript can do and what it can’t do. You don’t necessarily have to write cool JavaScript transitions, or even in CSS, but you’ve got to know what you can do in each. And you’ve got to be able to design so that if you have a more adept coder working with you, they know you know what you’re talking about.”
– Roger Black
Creative People Romanticize Mistakes
“Creative people romanticize mistakes and process. But there is no process if you don’t start. No one crumples a blank sheet of paper.”
– Frank Chimero, How to Have an Idea
How to use @font-face to avoid faux-italic and bold browser styles
Did you know that if you declare a custom font using
@font-face, the browser will try to fake the bold and
italic styles if it can’t find them? This is a clever little
feature that avoids a scenario where a themer specifies a font and
is then confused that bold and italics don’t work, but it can be
very confusing if you actually have a bold or italic version of
the font. In this article, I’ll walk you through how to properly
load your font files to avoid the browser’s faux-italic and
faux-bold styles.
How to Add Theme Settings for Drupal 7
You know that list of checkboxes on the theme settings page that let you turn on and off parts of the theme like the logo or slogan? Well, you can add your own options to that list really easily in Drupal 7. In D6, this was kind of a pain, because you had to write all sorts of functions to save and load your settings to the database and handle everything properly. In D7, that’s all done through the Form API, so the heavy lifting is done for you. All you need to do is tell it to add some form fields, and what the new setting is called!
You’ll need to create a theme-settings.php file in
your theme, and add this code to it:
1 2 3 4 5 6 7 8 | <?phpfunction themename_form_system_theme_settings_alter(&$form, &$form_state) { $form['theme_settings']['your_option'] = array( '#type' => 'checkbox', '#title' => t('Your Option'), '#default_value' => theme_get_setting('your_option'), );} |
-
['theme_settings']is the existing fieldset to add your option to. You can leave it off, but by pointing it to thetheme_settingsgroup, it’ll be added to the existing list of checkbox display options for your theme. Handy! -
['your_option']is the name of your new option. -
#typetells Drupal what kind of form element to create. -
#titleis the title (or in this case, label) text for the form element. -
#default_valuetells Drupal where to find the initial setting for the form element.
We set #default_value to
theme_get_setting('your_option'), which tells Drupal
to look for this setting in the database. But here’s the brilliant
part — if it can’t find that setting in the database, it will
check in your theme’s .info file! So add this line:
1 | settings[your_option] = 1 |
Now your theme will use the default setting unless the admin overrides it on the theme settings page.
You’ll want to actually do something with this setting, so here’s the PHP call to load it.
1 | theme_get_setting('your_option'); |
You can call this in from any of your .tpl.php files.
For a simple checkbox option like this, you can build an
if() statement around it, like so:
1 2 3 | <?php if (theme_get_setting('your_option')): ?> <!-- Your code here! --><?php endif; ?> |
In this example, we added a checkbox, but you can add just about any form elements you can think of, including radio buttons and text input fields. What sort of options will you add to your theme?
References
- theme_get_setting() and THEME_settings() improvements in Drupal 7
- Drupal 7 form API reference
- Advanced theme settings – this is for D6, but it’s still helpful