추가 사용자 프로필 필드에 액세스


9

다음을 사용하여 Drupal 7의 12 개 이상의 새로운 필드를 사용자 프로필에 추가했습니다.

구성-> 사람-> 계정 설정-> 필드 관리 (admin / config / people / accounts / fields)

이 값들에 어떻게 접근 할 수 있습니까? 그들은 $ user의 일부가 아닌 것 같습니다

성공하지 않고 시도 :

global $user;
var_dump($user);

답변:


13

내가 찾은 것 같아 추가 단계가 하나 있습니다.

global $user;
$user_full = user_load($user->uid); // user_load(uid) returns the complete array
var_dump($user_full);

이제 $ user_full을 통해 사용자 정의 프로파일 필드에 액세스 할 수 있습니다


1

패널은 또한 관계를 통해이 작업을 매우 쉽게 수행 할 수 있습니다.


0

나는 이것이 대안 솔루션이라고 생각하기 때문에 /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;
  }
}

0

이상한 이유 때문에 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객체를 다시 호출하는 대신 일을 처리하는 또 다른 방법 .


0

Drupal 7 코어로 사용자 데이터 (사용자 정의 필드 포함)를로드 할 수 있습니다

$user = entity_load($entity_type = "user", $ids = Array($user->uid), $conditions = array(), $reset = FALSE);

Drupal 7> API> 엔티티로드 에서 자세한 내용

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.