An Event Apart Orlando: Special Edition 2014

My notes from An Event Apart Orlando: Special Edition 2014 at Disney’s Contemporary Resort in Walt Disney World.

Continue reading An Event Apart Orlando: Special Edition 2014

Accessing Drupal Site Name and Menus in Templates

When I am creating a custom Drupal site, I like to hard code a few things into the templates, such as the site name and menus. This prevents the client from doing something like accidentally moving the menu blocks into a different region, and it also gives me more control over the HTML markup.

In Drupal 7, there are two ways that I have found to access this system data within the templates. I am going to walk through an example of each method in which I will add the following elements to my page.tpl.php template file:

  • My site name
  • The main menu
  • A custom menu named “Secondary menu” (which has a machine name of “menu-secondary-menu”)

Method 1

The first method involves setting up some variables in the template.php file and then accessing those variables in the page.tpl.php file.

In the template.php file:

function newhopeks_preprocess_page(&$variables) {
    $variables['site_name'] = filter_xss_admin(variable_get('site_name', 'Default Site Name'));
    $variables['main_menu'] = menu_main_menu();
    $variables['secondary_menu'] = menu_navigation_links('menu-secondary-menu');
}

In the page.tpl.php file:

<h1><?php print $site_name; ?></h1>
<?php
    print theme('links__system_main_menu', array('links' => $main_menu));
    print theme('links__menu_secondary_menu', array('links' => $secondary_menu));
?>

Method 2

The second method bypasses the template.php file and accesses the data directly.

In the page.tpl.php file:

<h1><?php print filter_xss_admin(variable_get('site_name', 'Default Site Name')); ?></h1>
<?php
    print theme('links__system_main_menu', array('links' => menu_main_menu()));
    print theme('links__menu_secondary_menu', array('links' => menu_navigation_links('menu-secondary-menu')));
?>

Related Resources

EditorConfig

I just used EditorConfig for the first time on a project, and it seems like something that will be very useful, especially for team development. What is EditorConfig? Here’s the description from the website:

EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.

EditorConfig allows you to specify the type and size of tabs used on a project, as well as things like character set and line endings. In a supported editor1, the rules specified in the .editorconfig file will execute when saving the file, making sure the formatting of the file is consistent. It even allows you to configure file extensions separately for languages that have specific requirements that may differ from your default settings.

Using EditorConfig eliminates the need to modify your editor preferences when switching between projects. For example, I’ve always preferred hard tabs, but everyone on my team at work uses soft tabs. You can just add the .editorconfig to a git repository, and everyone who works on the project can be using consistent formatting.

Here is my current .editorconfig file:

# EditorConfig (http://editorconfig.org)

# Set this as the root EditorConfig file
root = true

# Default settings for all files
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

  1. My editor of choice has been Coda for the past couple years, but it does not yet have EditorConfig support. I’ve been trying out a few other editors, including TextMate and the new GitHub Atom editor. You can find a list of supported editors on the EditorConfig website.