Theming with Profile2
User-profile.tpl.php
If you are designing a custom template for your user profile pages and are using the Profile2 module, you probably are looking to add your custom fields to the user-profile.tpl.php template. Here is how this can be accomplished.
This assumes that you have a Profile2 profile type called my-profile. You may easily load your fields into your custom user-profile.tpl.php by printing them like this:
<?php print render($user_profile['profile_my-profile']['view']['profile2'][1]['field_example']); ?>Where field_example is the machine name of your field. This example is setup for most fields such as text fields. Image fields can be rendered differently.
Node.tpl.php
To add the fields to the node.tpl.php template, we must first call the profile2 object like so:
<?php
$uid = user_load($node->uid);
$myprofile = profile2_load_by_user($uid, 'my-profile');
?>Then we can print fields like this:
<?php if (isset($portfolio->field_example['und'][0]['value'])): ?>
<?php print ($portfolio->field_example['und'][0]['value']); ?>
<?php endif; ?>As always you can view all printable items via the print_r function.
Pass the theme path to javascript
Description
This extends Drupal.settings allowing you to get the theme path in a javascript file
Steps
Add a preprocess function to your theme's template.php
Using drupal_add_js we extend Drupal.settings in order to make it a variable that returns the path to the theme
p.s. don't copy the php tags into template.php just what's inside.
<?php
function theme_preprocess_page(&$variables) {
drupal_add_js('jQuery.extend(Drupal.settings, { "pathToTheme": "' . path_to_theme() . '" });', 'inline');
}
?>Pass the theme path to javascript
Description
This extends Drupal.settings allowing you to get the theme path in a javascript file
Steps
Add a preprocess function to your theme's template.php
Using drupal_add_js we extend Drupal.settings in order to make it a variable that returns the path to the theme
p.s. don't copy the php tags into template.php just what's inside.
<?php
function theme_preprocess_page(&$variables) {
drupal_add_js('jQuery.extend(Drupal.settings, { "pathToTheme": "' . path_to_theme() . '" });', 'inline');
}
?>HowTo: Copy the form buttons on node creation pages to the top
Background:
I have a custom content type with a lot of form elements (close to 100) and scrolling down to the bottom got a bit irritating when all you wanted to do it update or edit a node of this content type.
So I wanted to copy the form action buttons to the top (but still keep them at the bottom).
Here is how I did it:
1) Create a custom module
1a) In your sites/default/modules/ folder create a new folder CUSTOM_MODULE*
1b) Create the mandatory CUSTOM_MODULE.info and CUSTOM_MODULE.module files.
1c) Fill out the CUSTOM_MODULE.info file with some basic info. eg:
name = Custom node form modify module
description = Custom module to duplicate the form buttons on node creation forms and place them above the form items.
package = Custom
core = 7.x
version = 0.11d) Copy the following code into the CUSTOM_MODULE.module file. I'll then go through the code to explain what it does.
/**
* Implementation of HOOK_form_alter()
*/
function CUSTOM_MODULE_form_alter(&$form, $form_state, $form_id) {
/**
* Copy the action buttons (submit, preview, etc ..)
* and place them at the top of the form
*/
if(!empty($form['actions'])) {
foreach($form['actions'] as $name => $button) {
// I DEFINE EACH BUTTON SEPERATELY BECAUSE I WISH TO DEFINE THE ORDER
Read moreHowTo: Add Google Webmasters Tools verification meta tag via the themes template.php file.
This guide refers to the meta tag verification method available for Google Webmaster Tools and these instructions are specific for Drupal 7.
You can also add any other meta tag you require using this guide.
edit: Since writing this guide a module has popped up that does all this for you. You can find the Site verify module here. However, if you require a more complex approach - see the first comment for an example - or just want to learn a bit more about how it works or simply prefer the DIY approach then please do read on:
What you require:
- Google Webmasters Tools Account - sign up here: https://www.google.com/webmasters/tools/
- Custom theme (or a copy of a supplied theme) in either /sites/default/themes/ or /sites/all/themes/
- Google Webmaster Tools Verification Code (meta tag)
1) Make sure you have your Google Webmaster Tools Verification Code meta tag. It looks something like this:<meta name="google-site-verification" content="abcdefghijklmnopqrstuvwxyz0123456789" />
Theming blocks individually, by region, or by module
Designers can create multiple tpl.php files for blocks based on the specific block, the module that created the block, or the region that the block appears in.
Template suggestions
Drupal 7
In Drupal 7 template files are searched in the following order:
- block--block--[block-id].tpl.php
- block--[module]--[delta].tpl.php
- block--[module].tpl.php
- block--[region].tpl.php
- block.tpl.php
If the block delta key uses a hyphen, (-), replace this with an underscore (_).
Drupal 7 Template (Theme Hook) Suggestions
Drupal 5 & 6
In Drupal 5 and 6, template files are searched in the following order:
- block-[module]-[delta].tpl.php
- block-[module].tpl.php
- block-[region].tpl.php
- block.tpl.php
For example, the user login block has a delta of '0'. If you put it in the left sidebar, when it's rendered, PHPTemplate will search for the following templates in descending order:
- block-user-0.tpl.php
- block-user.tpl.php
- block-left.tpl.php
- block.tpl.php
Finding module and delta
You can find the block's module and delta by looking at the html source of a page: each block's main DIV has the following classes and IDs: