drupal输出一个用户名的问题 用drupal的主题函数theme输出_PHP教程

WBOY
Libérer: 2016-07-14 10:10:13
original
789 Les gens l'ont consulté

 今天遇到了drupal输出一个用户名的问题,使用的是drupal的主题函数theme输出的,于是查资料搜索,摸索出来以下一些内容:


  1、theme('username', array('account' => $log))调用函数, 参数1为hook,  参数2为参数

        代码比较多不展出了,查看地址点击打开链接


  2、执行期间回去找模版的预处理函数和处理函数分别是 template_preprocess_username(&$variables)和template_process_username(&$variables)  这两个函数在theme.inc中
 

[php]
/**
 * Preprocesses variables for theme_username().
 *
 * Modules that make any changes to variables like 'name' or 'extra' must insure
 * that the final string is safe to include directly in the output by using
 * check_plain() or filter_xss().
 *
 * @see template_process_username()
 */ 
function template_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; 
  } 
 
  // 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')); 

 
/**
 * Processes variables for theme_username().
 *
 * @see template_preprocess_username()
 */ 
function template_process_username(&$variables) { 
  // Finalize the link_options array for passing to the l() function.  
  // This is done in the process phase so that attributes may be added by  
  // modules or the theme during the preprocess phase.  
  if (isset($variables['link_path'])) { 
    // $variables['attributes_array'] contains attributes that should be applied  
    // regardless of whether a link is being rendered or not.  
    // $variables['link_attributes'] contains attributes that should only be  
    // applied if a link is being rendered. Preprocess functions are encouraged  
    // to use the former unless they want to add attributes on the link only.  
    // If a link is being rendered, these need to be merged. Some attributes are  
    // themselves arrays, so the merging needs to be recursive.  
    $variables['link_options']['attributes'] = array_merge_recursive($variables['link_attributes'], $variables['attributes_array']); 
  } 

/**
 * Preprocesses variables for theme_username().
 *
 * Modules that make any changes to variables like 'name' or 'extra' must insure
 * that the final string is safe to include directly in the output by using
 * check_plain() or filter_xss().
 *
 * @see template_process_username()
 */
function template_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;
  }

  // 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'));
}

/**
 * Processes variables for theme_username().
 *
 * @see template_preprocess_username()
 */
function template_process_username(&$variables) {
  // Finalize the link_options array for passing to the l() function.
  // This is done in the process phase so that attributes may be added by
  // modules or the theme during the preprocess phase.
  if (isset($variables['link_path'])) {
    // $variables['attributes_array'] contains attributes that should be applied
    // regardless of whether a link is being rendered or not.
    // $variables['link_attributes'] contains attributes that should only be
    // applied if a link is being rendered. Preprocess functions are encouraged
    // to use the former unless they want to add attributes on the link only.
    // If a link is being rendered, these need to be merged. Some attributes are
    // themselves arrays, so the merging needs to be recursive.
    $variables['link_options']['attributes'] = array_merge_recursive($variables['link_attributes'], $variables['attributes_array']);
  }
}
 

  3、处理完这些变量后接下来就到了主题函数了,这里实现了最终html的拼装与输出 theme_username($variables);

 

[php]
function theme_username($variables) { 
  if (isset($variables['link_path'])) { 
    // We have a link path, so we should generate a link using l().  
    // Additional classes may be added as array elements like  
    // $variables['link_options']['attributes']['class'][] = 'myclass';  
    $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']); 
  } 
  else { 
    // Modules may have added important attributes so they must be included  
    // in the output. Additional classes may be added as array elements like  
    // $variables['attributes_array']['class'][] = 'myclass';  
    $output = '' . $variables['name'] . $variables['extra'] . ''; 
  } 
  return $output; 
'>

function theme_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';
    $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '' . $variables['name'] . $variables['extra'] . '';
  }
  return $output;
}
 
'>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477543.htmlTechArticle今天遇到了drupal输出一个用户名的问题,使用的是drupal的主题函数theme输出的,于是查资料搜索,摸索出来以下一些内容: 1、theme(username,...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal