显示用户在该用户个人资料中显示为字段的所有节点?
问题描述:
我有一个内容类型为“项目”的网站,其中有一个领域为“项目经理”,需要一个用户。是否有一种很好的方式来显示用户在该用户个人资料中显示为该项目的项目经理的所有项目?显示用户在该用户个人资料中显示为字段的所有节点?
更新:
这是我在user_profile.tpl.php至今
...
function my_module_user_view($account, $view_mode, $langcode) {
$my_field = 'field_pm';
$uid = $account->uid; // The uid of the user being viewed.
$query = "SELECT entity_id FROM {field_data_{$my_field}} WHERE {$my_field}_target_id = :uid AND bundle='user'";
$args = array(':uid' => $uid,);
$result = db_query($query, $args);
$nids = $result->fetchCol();
if (count($nids)) {
$projects = node_load_multiple($nids); // Load all projects where that user is a PM
foreach ($projects as $project) {
$account->content['field_pm_projects'][0]['#markup'] = $project->title;
}
}
}
?>
<div class="profile"<?php print $attributes; ?>>
<?php print render($user_profile); ?>
<?php print $account->content['field_pm_projects']; ?></span>
</div>
答
最简单的方法是使用EntityFieldQuery来实现你想要的结果。 这里是你如何能做到这:
global $user;
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'projects')
->propertyCondition('status', 1)
->propertyCondition('uid', $user->uid
->fieldCondition('field_<YOUR_CUSTOM_FIELD>', '<COLUMN_NAME>', '<VALUE>', '=')
->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
$nodes = node_load_multiple(array_keys($result['node']));
}
有关EntityFieldQuery更多信息,请按以下网址链接.. https://www.drupal.org/node/1343708
希望这将解决您的problem.GL
谢谢!有效! 我只是补充说,使用“全局$用户”可以让您当前登录的用户,但分配“$ user = user_load(arg(1))”将成为当前用户配置文件的用户。 –
另外,是否有一个很好的方法来搜索多个领域(用户可能是下午或开发人员..)谢谢! –