【问题标题】:Display all nodes for which user appears as a field on that user's profile?显示在该用户的个人资料中显示为字段的用户的所有节点?
【发布时间】:2016-02-29 17:57:27
【问题描述】:

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

更新:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 谢谢!有效!我只想添加使用“global $user”获取当前登录的用户,但分配“$user = user_load(arg(1))”将是当前用户配置文件的用户。
    • 另外,有没有搜索多个字段的好方法(用户可能是 pm 或开发人员..)谢谢!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 2014-05-06
      • 2021-09-04
      • 2020-12-14
      • 2019-11-29
      • 2019-09-18
      • 1970-01-01
      • 2020-02-07
      • 2021-12-15
      相关资源
      最近更新 更多