1

OK, so this is a bit tricky, let's see if you can follow my problem: (just let me know which part you don't understand 1,2,3...) I am using Drupal 7

1) I have 2 content types: Workers and Documents

2) In the content type Documents I have the entity reference field of all the workers. (Eg. I want to add a document/node that refers to a specificic worker John)

3) In the content type Documents there is a list field the user can select two options: red or green (it's a field where the user can say the document is OK to use)

4) Everytime I add a Document, I have to select red or green in that field.

5) Right now, I am displaying a view with all the nodes/documents from the content type Documents refering to the title and List field (red or green). Something like this:

Document 1 ------------------ red

6) On another page, I am displaying a view with a list of all workers (Content type Workers) and here comes the tricky part:

I want to add a field in this view displaying red or green based on this condition: IF (all the Documents from the entity reference "specific worker" have "green" on the field): display green. IF (one of the Documents has the field "red"): display red

It will diplay something like:

John --------------- green

How can I achieve this with views? Do I need to write code? Thank you.

2
  • Please specify your Drupal version, 7 or 8?
    – No Sssweat
    Commented Jul 19, 2016 at 10:03
  • I am using Drupal 7!
    – Drii7
    Commented Jul 19, 2016 at 10:09

1 Answer 1

2

Here is a zero coding solution.

Create a new view:

  • CONTEXTUAL FILTER add Node id (since workers is a content type), so we have the id of the worker.
  • RELATIONSHIP add Referecing Entity, checkmark require relationship.
  • Fields: Add Title

    • At top you will see a select box, select the relationship option.
    • Unchecked Link this field to the original piece of content
    • Click on NO RESULT BEHAVIOUR, put Green
    • Click on REWRITE RESULTS, checkmark Rewrite the output of this field, put Red
  • In FILTER CITERIA add type and select Workers.

  • In FILTER CRITERIA add the List Field (the one that contains Red or Green) and put is equal to Red and select relationship option at top.

for PAGER put 1 item.

enter image description here


Install and enable Views Field View

Now in your List of All Workers view:

In FIELDS add Node ID and checkmark hide field from view.

In FIELDS now Add the View Fields view field.

  • Select the previous view we created.
  • For argument, Click on replacement patterns, and use the Node ID token to pass as an argument.
5
  • Hi, thank you for your answer. I am trying to understand the logic here, and I think you misunderstood this: the List Field is on content type "Documents" not content type "Workers". Did you consider this? I'll try to see if this works.
    – Drii7
    Commented Jul 21, 2016 at 11:43
  • @Drii7, Did you consider this? i believe I did, the list field appears here from the entity referencing relationship (from documents), without that, we wouldn't be able to add that field as filter.
    – No Sssweat
    Commented Jul 21, 2016 at 13:08
  • Hi, I followed all your instructions but nothing shows up in the "Workers" view... Here are the prints of the two views: imgur.com/a/dWvm7 and imgur.com/a/Tslta (the names are in portuguese but everything else is English) When you say: In FIELDS now Add the View Fields view field - I can only find "Global View" i don't know if this is the right field... Is there something I can do to see what's wrong? I can't see no errors, I don't even know which view is wrong. Thank you!
    – Drii7
    Commented Jul 21, 2016 at 21:44
  • Hi, I had to change my view a little to correspond to my system. For example the NO RESULT BEHAVIOUR were not displaying because the view itself was not having any result. I had to add the NO RESULT BEHAVIOUR option in the view, not in the field "Title". Anyway, you did help me with your logic and with the Views Field View module so I will accept your answer. Thank you!
    – Drii7
    Commented Jul 24, 2016 at 2:41
  • @Drii7 no problem, I original thought of doing it like that, but then I thought the field no result should work, I guess not for this case. Now that I think about, its because its the only field, so you have use the view no result. Glad you figure it out. Some how I missed your 2nd last reply, I would've told you this had I seen it. Anyhow, cheers!
    – No Sssweat
    Commented Jul 24, 2016 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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