1

I've created a text field on a custom content type. The text field is called "targetUser" on my content type "message".

targetUser is meant to collect the uid of a user that a current app user is sending a message to. E.g. If user 1 is sending a message to user 7, the number "7" will post to the targetUser field.

I want to create a view that ONLY displays "messages" with the targetUser number that matches the logged in user's uid, so that they may ONLY see messages meant for them.

I essentially want my view to say: "Only display messages with this user's uid in the field targetuser" - is this doable? Hope I explained this well enough.

2
  • Looks like you are using nodes to allow users to send messages to other users, which is fine. But just wondering, did you consider using Private Message for this?
    – No Sssweat
    Apr 17 '16 at 1:30
  • @NoSssweat Yes I did - but Drupal iOS SDK doesn't allow me to post to an entity; only retrieve data from an entity (building an iOS app). Hence, the use of nodes to send messages :)
    – Brittany
    Apr 17 '16 at 1:39
2

targetUser is meant to collect the uid of a user that a current app user is sending a message to

IMO the better approach would be to make targetUser an entity reference field.

  • Download and install the Entity Reference module

    • In content type Message fields, add a new field with Entity Reference and a widget of Auto complete (tag style)

For example: I named mine lalala (click picture below to enlarge)

enter image description here

  • For Target Type, select User
  • Number of Values: Unlimited (as you could send a message to multiple users)

enter image description here

Then add or edit a message node and start typing user name, you will see the name in the drop down, select it and save.

Use a comma , to add other user account.

enter image description here

In your view

  • contextual filter of Logged in User id (uid)
  • add a relationship of lalala Referenced Entity

    • If not, try Referencing entity, but if my memory is correct it's Referenced

Result: you should get a view of all the nodes where the logged in user was referenced.


The autocomplete only allows by username, and not uid. E.g. posting 47 into this field isn't allowed?

Create a new USER view.

1) click on +Add

2) Select Entity Reference

3) Remove user:name and add user:uid

4) click on settings and checkmark uid (if you don't do this step, it won't let you save the view)

enter image description here

Go to your Content Type Messages manage fields and edit your entity reference field.

  • At the bottom check mark ✓ Render Views filters as select list
  • Change to mode to View and select your view.

enter image description here

Also, "logged in user id (uid)" isn't an option? Just (author) User: Uid ?

You will need to create a CONTENT view and then add:

  • Relationship of Referenced User
  • Now since you have a relationship User: Uid becomes available as contextual filter

enter image description here

enter image description here

8
  • The autocomplete only allows by username, and not uid. E.g. posting 47 into this field isn't allowed?
    – Brittany
    Apr 17 '16 at 2:14
  • Also, "logged in user id (uid)" isn't an option? Just (author) User: Uid ?
    – Brittany
    Apr 17 '16 at 2:20
  • @Brittany updated answer.
    – No Sssweat
    Apr 17 '16 at 2:42
  • After doing all of the above, for some reason no nodes are shown when I implement 'Entity Referenced from field targetUser' under contextual filters? And I'm still unable to post the UID to the targetUser field :/
    – Brittany
    Apr 17 '16 at 2:46
  • @Brittany it will show empty when you edit the view, but the nodes should show up when you visit the page. I'm still unable to post the UID to the targetUser field Darn, looks like this is a flaw of the auto-complete, it forces to user to select rather than just type =(
    – No Sssweat
    Apr 17 '16 at 2:53
1

So this ended up being MUCH easier than anticipated. I simply created a "targetuser" text field underneath of my custom Messages content type.

Then, I created a Content View, and added the following filters:

CONTEXTUAL FILTER

Content: targetuser

Then click on the above filter, and:

WHEN THE FILTER VALUE IS NOT IN THE URL

Provide Default Value

and select: User ID from logged in user

This way, the targetuser id always matches that of the user logged in. Voila, only messages for logged in user appear.

1
  • Nice one @Brittany. I guess I was over thinking things. Will make a mental note on this trick.
    – No Sssweat
    Apr 17 '16 at 23:07

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.