1

I have a site with a content type "Projects," which has a field "Project Manager" that takes a user. Is there a good way to display all Projects for which a user appears as the Project Manager for that Project on that user's profile?

Update:

Here is what I have so far in 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>
5
  • has a field "Project Manager" that takes a user is this an entity reference field? – No Sssweat Feb 24 '16 at 23:58
  • @NoSssweat: yup, it is.. – Jillian Hoenig Feb 25 '16 at 0:03
  • so you want to display all the projects the user is a project manager on their user profile page, right? – No Sssweat Feb 25 '16 at 0:17
  • exactly @NoSsseat – Jillian Hoenig Feb 25 '16 at 15:24
  • Check the function's signature views_embed_view, the first argument must be a view not a field. Keep in mind that it is not a good practice to implement hooks in .tpl files. – mchar Feb 26 '16 at 22:13
1

Add a contextual filter for that entity reference field and in its settings, provide default: uid from url. That will only show nodes where the uids match.

There are two options, "referenced" and "referencing".. I'm pretty sure you want referenced but try both.

5
  • Hm, I'm not editing a view though. I actually want to add an element to the user profile..Thank you for your response! – Jillian Hoenig Feb 25 '16 at 16:14
  • 1
    If you place the view as block on user pages, it will work at a basic level. I think you should add some more information to your opening post.. Solutions given so far answer the initial question. – Niall Murphy Feb 25 '16 at 16:57
  • @JillianHoenig you could use a view block not view page. – No Sssweat Feb 25 '16 at 18:33
  • I know. It's a complicated relationship though (I messed with a couple of configurations, with no success). Right now I am toying with db_select, by I'm pretty new to php :/ – Jillian Hoenig Feb 25 '16 at 18:50
  • Did you try the view i described above as a block? What functionality do you need with this? – Niall Murphy Feb 26 '16 at 3:32
0

If you are using entity reference for the Project Manager field, you could use (Entity)Reference Field Synchronization module and create another companion field ("Managed Projects") on the User profile. The module keeps them in sync so that if you add a username to the Project Manager field, the Managed Project field will populate automatically. The same would go in reverse. Views would be another option.

1
  • That sounds like a great module. There must be a way to do in programmatically, though.. – Jillian Hoenig Feb 25 '16 at 16:13
0

You have to utilize that reference field of yours. I'll try to give you a rough idea:

/**
 * Implements hook_user_view().
 */ 
function my_module_user_view($account, $view_mode, $langcode) {

  $my_field = 'field_my_field';
  $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; 
    }
  }
}

You will need a second field (field_pm_projects) to store that info or you may print them on the theme direct, it depends on what you want to do. The chosen hook/preprocess also depends on the implementation you want to follow.

5
  • wow, thank you for your detailed response. Is it possible to implement some variation of this code on user_profile file, and somehow draw the uid from url?? Again, thanks so so much. This issue has been driving me crazy o_O – Jillian Hoenig Feb 25 '16 at 21:52
  • Yes, you can implement that code wherever you like. I guess that that user_profile file is a template. I have updated my answer so to fit to what you said about the uid from url. All I did was to implement a different hook (hook_user_view) that always gives you the user entity being viewed. So no need for extracting the uid from the url. – mchar Feb 26 '16 at 9:43
  • Awesome. Thanks! I'm kinda new to this though... (and for future reference) would you mind explaining what all the variables are, to help me figure out what I need to substitute for them (eg $account, $view_mode, etc)...? – Jillian Hoenig Feb 26 '16 at 15:35
  • You don't have to substitute any variables, use them as they are. These variables are provided by the drupal's hook system (more). You may alter their names, though. The only variable you have to change is the $my_field with your field's name and you only know that name. – mchar Feb 26 '16 at 16:37
  • Would you mind taking a look at the code I've updated my question with and see if you can tell what's wrong. Thank you this has gotten me so much further along I think I'm nearly there.. – Jillian Hoenig Feb 26 '16 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.