Všechny funkce začínající slovem template_ nebo theme_ (např. theme_username) vytvářejí HTML výstup a je možné je upravit pomocí souboru template.php.
var_dump($variables);
kpr($variables);
Přesměrování uživatele na stránku Dashboard ihned po přihlášení:
/**
* Redirect user to a specific page after they login
*/
function MYTHEMENAME_user_login(&$edit, $account) {
$edit['redirect'] = 'admin/dashboard';
}/**
* Redirect user to a specific page after they login if they belong to a certain role
*/
function MYTHEMENAME_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset' || variable_get('login_destination_immediate_redirect', FALSE)) {
if(in_array('administrator',$account->roles)) {
drupal_goto('admin/dashboard');
}
else {
drupal_goto('user/'.$account->uid);
}
}
}Přepis výstupu polí
function MYTHEMENAME_preprocess_field(&$variables, $hook) {
//kpr($variables);
// For field type "boolean"
if ($element['#field_type'] == 'list_boolean') {
// Add CSS class and change in Value
if ($variables['element']['#items'][0]['value'] == 1) {
$variables['classes_array'][] = 'true';
$variables['items'][0]['#markup'] = t('Yes');
}
else {
$variables['classes_array'][] = 'false';
$variables['items'][0]['#markup'] = t('No');
}
}
// For all field except specific type and name
if($variables['element']['#field_type'] !== 'text_with_summary' &&
$variables['element']['#field_type'] !== 'text_long' &&
$variables['element']['#field_name'] !== 'field_price' {
// For each item
foreach ($variables['items'] as $delta => $item) {
if (!empty($variables['items'][$delta]['#markup'])) {
// Add tag <strong>
$variables['items'][$delta]['#markup'] = '<strong>'. $variables['items'][$delta]['#markup'] .'</strong>';
}
}
}
}Náhrada nulové ceny za slovo zdarma ve výpisu Views:
/**
* Replace price 0 to free
*/
function MYTHEMENAME_preprocess_views_view_fields(&$variables) {
//kpr($variables);
if($variables['fields']['field_price']->content == 0) {
$variables['fields']['field_price']->content = t('free');
}
}Úprava řetězce "Submitted by [name] on [date]":
<?php
function MYTHEMENAME_preprocess_node(&$variables) {
$variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));
// only username
//$variables['submitted'] = t('Submitted by !username', array('!username' => $variables['name']));
// only date
//$variables['submitted'] = t('Submitted on !datetime', array('!datetime' => $variables['date']));
// Drupal date formats (long, medium, short)
//$variables['date'] = format_date($variables['node']->created, 'short');
// Custom format types
//$variables['date'] = format_date($variables['node']->created, 'custom', 'F j, Y');
// Date Created -> Changed
//$variables['date'] = format_date($node->changed);
// Date Created -> Changed + custom username and date
/*$variables['date'] = format_date($node->changed);
if (variable_get('node_submitted_' . $node->type, TRUE)) {
$variables['display_submitted'] = TRUE;
$variables['submitted'] = t('Edited by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));
$variables['user_picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $node)) : '';
} else {
$variables['display_submitted'] = FALSE;
$variables['submitted'] = '';
$variables['user_picture'] = '';
}*/
}
?>Úprava délky uživatelského jména:
/**
* Implements hook_preprocess_username().
*/
function MYTHEMENAME_preprocess_username(&$variables) {
$account = $variables['account'];
$variables['extra'] = '';
if (empty($account->uid)) {
$variables['uid'] = 0;
if (theme_get_setting('toggle_comment_user_verification')) {
$variables['extra'] = ' (' . t('not verified') . ')';
}
}
else {
$variables['uid'] = (int) $account->uid;
$user = user_load($account->uid);
if (isset($user->field_fullname['und'][0]['value'])) {
$name = $variables['name_raw'] = $user->field_fullname ['und'][0]['safe_value'];
}
else {
// Set the name to a formatted name that is safe for printing and
// that won't break tables by being too long. Keep an unshortened,
// unsanitized version, in case other preprocess functions want to implement
// their own shortening logic or add markup. If they do so, they must ensure
// that $variables['name'] is safe for printing.
$name = $variables['name_raw'] = format_username($account);
}
}
if (drupal_strlen($name) > 20) {
//$name = drupal_substr($name, 0, 15) . '...';
}
$variables['name'] = check_plain($name);
$variables['profile_access'] = user_access('access user profiles');
$variables['link_attributes'] = array();
// Populate link path and attributes if appropriate.
if ($variables['uid'] && $variables['profile_access']) {
// We are linking to a local user.
$variables['link_attributes'] = array('title' => t('View user profile.'));
$variables['link_path'] = 'user/' . $variables['uid'];
}
elseif (!empty($account->homepage)) {
// Like the 'class' attribute, the 'rel' attribute can hold a
// space-separated set of values, so initialize it as an array to make it
// easier for other preprocess functions to append to it.
$variables['link_attributes'] = array('rel' => array('nofollow'));
$variables['link_path'] = $account->homepage;
$variables['homepage'] = $account->homepage;
}
// We do not want the l() function to check_plain() a second time.
$variables['link_options']['html'] = TRUE;
// Set a default class.
$variables['attributes_array'] = array('class' => array('username'));
}function theme_username($object) {
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
...např. na:
function MYTHEMENAME_username($object) {
if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 30) {
$name = drupal_substr($object->name, 0, 25) .'...';
}
...
Místo phptemplate je z hlediska výkonu vhodnější psát název tématu. 