0

Getting a bit mixed up and need help with this. My restrictions are that I cannot use Entity Reference field (or any reference type field) - this is because Salesforce Suite isn't able to translate single strings to the array object normal reference fields use upon import and object creation.

I have two entity types - User and Content - which are dynamically created via the Salesforce Pull module. Both entities share a plain text field called SFID, which is the user's main ID#.

The Content nodes' SFID field value can match any User's SFID field value, and I want to create a view that lists nodes on the user's page related in this way.

Example:

USER 5
- SFID (text field) = 120

NODE A
- SFID (text field) = 120

NODE B
- SFID (text field) = 130

NODE C
- SFID (text field) = 120

On the page ...user/5/mynodes I need a list of the nodes whose SFID matches the user's SFID, so in the cases above, the page would list the following nodes:

- NODE A
- NODE C

Please help!

1

You need to make a View that lists nodes, containing a contextual filter that filters by SFID. Do that, and what remains is the problem of how to pass the user's SFID value to the contextual filter. There are many ways to do this, depending on how you're generating the user/%user/mynodes page.

Panels / Page Manager approach

My go-to recommendation for this sort of thing is to use Panels / Page Manager to generate the user/%user/mynodes page, and embed your view there. If you enable the Views Content module (bundled with CTools) you will have a new type of Views display available called "Content pane". Build your view using this type of display. In the view configuration when configuring this display there's an option for "Argument input". You'll probably want to choose "input on pane config". Then, when embedding the view in your page (using Page Manager) you'll be given the opportunity to plug in a token there from the ctools user context that represents the user's SFID.

Custom module alternative:

Since you said you can't use Panels, your best alternative is probably to create your own menu callback at user/%user/mynodes wherein you extract the SFID from the user object and return the view by calling views_embed_view().

This is greatly simplified, but here's the gist of it:

function mymodule_menu() {
 $items['user/%user/mynodes'] = array(
  'page callback' => 'mymodule_user_nodes',
  'page arguments' => array(1),
 );
 return $items;
}

function mymodule_user_nodes($account) {
 return views_embed_view('viewname', 'displayname', array($account->field_sfid['und'][0]['value']));
}

Note: I haven't tested that code, it's just off the top of my head. I may have forgotten something. Don't forget to add things like an access callback since I presume you'll want to limit access to this page.

Views Field View or Entity Views Attach (EVA) alternative

This is a little bit icky but should work. Either of the modules I listed (Views Field View or Entity Views Attach) will work for this approach. I'll specifically outline how to do this with Views Field View but you could adapt these methods to EVA if you want; it would be similar.

You will make 2 views:

  1. A view that lists users and accepts the user ID as a contextual filter. So this view will always return only 1 single user (whatever user is passed in the URL).
  2. The view that you've already likely created; the one that has a contextual filter that accepts a SFID value and lists nodes.

What we'll do is use view #1 to generate the page at user/%/mynodes, and view #2 will be embedded inside of view #1; view #2 will generate the actual list of nodes for us.

We'll assume you've created view #2 correctly already (it's basically what we've already discussed previously) and I'll focus on view #1.

  1. Create a view based on the users table
  2. Path for the view should be user/%/mynodes
  3. Add a contextual filter to the view that filters on uid
  4. Configure the view to output fields
  5. Add the SFID field to the view, but check the "exclude from display" option so it's not actually displayed. We add this to the view so that it's available as a substitution for our next step.
  6. If you've installed Views Field View then you'll have a new field available called Global: View. Add this to your view. This field has a few settings; you'll need to select the name and display of view #2, above, and enter [!field_sfid] (this is the substitution token we get from the field we added in step 5) into the Contextual filters option.

Now you need only visit user/%/mynodes with an appropriate uid in place of the % and it should work.

  • Aw shoot - Panels is out for now as it's not approved for us to use on this project. Just plain old views. Did you have any specific suggestions for just Views? I tried a Content view with the SFID contextual filter, and then tried PHP Code to load the user from the argument, and doing $user->field_sfid['und'][0]['value'], but I'm getting an empty view result. – atwixtor Aug 12 '15 at 19:12
  • Woah, that's a little beyond my expertise. So I guess there's no UI-only way of doing this in Views? My fallback is to use Rules to post-process the values by fetching entities by field values, and creating a real entity reference field value. – atwixtor Aug 12 '15 at 20:14
  • I don't know the ins-and-outs of Rules all that well, so maybe it's possible to do with Rules. – Aaron Aug 12 '15 at 20:16
  • @atwixtor I updated my answer with another option for you. – Aaron Aug 12 '15 at 20:35
  • Also: don't be scared of doing the module approach. I basically wrote the whole module for you, you just need to add a .info file and you're done. – Aaron Aug 12 '15 at 20:37

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.