显示用户在该用户配置文件中显示为字段的所有节点?

时间:2016-02-29 17:57:27

标签: drupal drupal-7 user-profile hook-form-alter

我有一个内容类型为“Projects”的网站,其中包含一个“项目管理器”字段,用于访问用户。有没有一种方法可以显示用户在该用户的个人资料中显示为该项目的项目经理的所有项目?

更新

以下是我目前在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>

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用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

希望这能解决你的问题.GL

答案 1 :(得分:0)

您可以使用Views模块轻松实现此目的,因此您无需编写任何代码。通过在页面链接中选择uid,创建节点在项目经理字段上查看和过滤。将视图放在用户页面上,你就可以了。