답변:
나는 이것이 대안 솔루션이라고 생각하기 때문에 /programming/8124089/get-value-of-custom-user-field-in-drupal-7-template 에서 내 대답을 다시 게시하고 있습니다. 이 예는 기본 사용자 이름 대신 field_real_name과 같은 것을 사용하는 방법을 보여줍니다.
전처리 기능을 사용하는 경우 전역 $user객체 를 가져올 필요가 없습니다 . $variables['name']내가 호출 한 사용자 정의 필드 에있는 $ variables 배열의 필드를 변경할 수 있습니다 field_real_name. $variables배열에 액세스 할 수 있으므로 다음과 같이 사용자 정보를 얻을 수 있습니다. uid와 연관된 정보를로드합니다 ( template_preprocess_username 참조 ).
function mythemename_preprocess_username(&$variables) {
$account = user_load($variables['account']->uid);
...more code will go here in a moment
}
당신이 dpm($account)(또는 kpr($account)당신이 개발을 사용하지 않는 경우) 글로벌 $user객체 를 사용하지 않고 모든 사용자 정보에 액세스 할 수 있음을 알 수 있습니다 .
그런 다음의 출력을 변경할 수 있습니다 $variables['name']귀하의 수 field_real_name로서 다음과 같습니다 :
function mythemename_preprocess_username(&$variables) {
// Load user information with user fields
$account = user_load($variables['account']->uid);
// See if user has real_name set, if so use that as the name instead
$real_name = $account->field_real_name[LANGUAGE_NONE][0]['safe_value'];
if (isset($real_name)) {
$variables['name'] = $real_name;
}
}
이상한 이유 때문에 Drupal 7의 프로파일 필드는 예전과 다릅니다. 그러나 사용자 프로필 개체는 추가 프로필 필드를 배열 요소로 액세스 할 수있게합니다. 예를 들면 다음과 같습니다.
$profile->field_fieldname['und'][0]['value']
사용할 수 없지만 다음과 같이 다시 쓰면 작동합니다.
$user_profile['field_fieldname']['#object']->field_fieldname['und'][0]['value'];
그래서 나는 단순히 내 코드에서 다음을 수행했습니다.
/*
* Create simplified variables as shortcuts for all fields.
* Use these variables for read access lateron.
*/
$firstname = $user_profile['field_first_name']['#object']
->field_first_name['und'][0]['value'];
$middlename = $user_profile['field_middle_name']['#object']
->field_middle_name['und'][0]['value'];
$surname = $user_profile['field_surname']['#object']
->field_surname['und'][0]['value'];
$image = $user_profile['field_user_picture']['#object']
->field_user_picture['und'][0]['uri'];
$user객체를 다시 호출하는 대신 일을 처리하는 또 다른 방법 .
Drupal 7 코어로 사용자 데이터 (사용자 정의 필드 포함)를로드 할 수 있습니다
$user = entity_load($entity_type = "user", $ids = Array($user->uid), $conditions = array(), $reset = FALSE);
Drupal 7> API> 엔티티로드 에서 자세한 내용