theme

Drupal Theme by pieces. Menu links (Primary and Secondary)

Julia 13/04/2010

В этом сниппете из цикла Theme by pieces о главном меню.

в page.tpl.php:

<?php if ($primary_menu): print $primary_menu; endif; ?>
<?php if ($secondary_menu): print $secondary_menu; endif; ?>

В template.php в функции
mytheme_preprocess_page(&$vars, $hook):

Стандартный вариант:

<?php
if (!empty($vars['primary_links'])) {
   
$vars['primary_menu'] = theme('links_primary', $vars['primary_links'], array('class' => 'bookmarks'));
  }
?>

Дополнительный шаблон

Для сложной верстки, когда не хватает стандартных тегов ul/li,
например, когда меню имеет круглые края.
В этом случае удобнее включить дополнительный шаблон.

<?php

?>

Plain links

For example for Secondary links:

_preprocess_page(&$vars, $hook)

<?php
if (!empty($vars['secondary_links'])) {
   
$vars['secondary_menu'] = theme('links_secondary', $vars['secondary_links'], array('class' => 'static-menu'));
  }
?>
<?php
/**.
* Implementation of hook_theme.
*
* Register custom theme functions.
*/

function renlife_theme() {
  return array(       
    
'links_secondary' => array(
     
'arguments' => array(
       
'links' => NULL,
       
'attributes' => array('class' => 'links'),
       
'settings' => array(
         
'delimiter' => '',
         
'leftcab' => NULL,
         
'rightcab' => NULL,
        )
      ),
     
'template' => 'links-plain',
     )   
  );
}
?>

Для plain меню, когда разметка меню не содержит ul/li,
но содержит разделители между пунктами тип "|",
используем дополнительный шаблон
plain

<?php
/**
* This snippet changes links layout.
*/
$output = '';

if (

count($links) > 0) {

 

$output .= '<div'. drupal_attributes($attributes) .'>';
 
 
// Display the right cap of the 'button bar'.
 
if (!empty($settings['leftcab'])) {
   
$output .= $settings['leftcab'];
  }

 

// Build the list of themed links.
 
$link_list = array();
  foreach (
$links as $link) {
    if (isset(
$link['href'])) {
     
// Pass in $link as $options, they share the same keys.
     
$link_list[] = l($link['title'], $link['href'], $link);
    }
    else if (!empty(
$link['title'])) {
     
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
     
if (empty($link['html'])) {
       
$link['title'] = check_plain($link['title']);
      }
     
$span_attributes = '';
      if (isset(
$link['attributes'])) {
       
$span_attributes = drupal_attributes($link['attributes']);
      }
     
$link_list[] = '<span'. $span_attributes .'>'. $link['title'] .'</span>';
    }
  }

 

// Add delimiter between the links.
 
if (!empty($settings['delimiter'])) {
   
$output .= implode($settings['delimiter'], $link_list);
  }
  else {
   
$output .= implode('', $link_list);
  }

 

// Display the right cap of the 'button bar'.
 
if (!empty($settings['rightcab'])) {
   
$output .= $settings['rightcab'];
  }
 
 
$output .= '</div>';
}

print

$output;
?>

Drupal. How to use an image field from the profile node instead of user picture (avatar)

Julia 19/01/2010

I've recently had to make default pictures for user pictures,
but they had to be defferent for different user roles.

I used the module content_profile
and created two profile content types for Student and Teacher roles.
So I decided to change a user picture derectly in my theme.

I change the preprocess function mytheme_preprocess_user_picture from the user module in the template.php file:

<?php
function mytheme_preprocess_user_picture(&$variables) {

$variables['picture'] = '';
 
   
$account = $variables['account'];
   
   
$user = user_load(array('uid' => $account->uid));   
   
$roles = $user->roles;

   

$profile = '';   
    if (
is_array($roles)) {     
      if (
array_search('student', $roles)) {
         
$profile = 'studentprofile';
      } else if (
array_search('sensei', $roles)) {
         
$profile = 'profile';
      }
    }
   
    if (!empty(
$profile)) {
     
$node_profile = content_profile_load($profile, $account->uid);
     
$field_photo = content_fields('field_photo', $profile);
     
     
$picture = content_view_field($field_photo, $node_profile);
    } else {
   
       
// from default preprocess_user_picture:
       
if (!empty($account->picture) && file_exists($account->picture)) {
         
$picture = file_create_url($account->picture);
        }
        else if (
variable_get('user_picture_default', '')) {
         
$picture = variable_get('user_picture_default', '');
        }
   
        if (isset(
$picture)) {
         
$alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
         
$variables['picture'] = theme('image', $picture, $alt, $alt, '', FALSE);
          if (!empty(
$account->uid) && user_access('access user profiles')) {
           
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
           
$variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
          }
        }
       
//
       
   
}
   
$variables['picture'] = $picture;

}

?>