2

I am writing a rule to store Tenant field's value to Previous tenant field whenever the Tenant field is updated and saved. The following are apartment and tenant content types.

Apartment content type:

  • Room
  • Tenant (entity reference to Tenant content type)
  • Previous Tenant (unlimited number of values is set for this field)

Tenant content type:

  • Name
  • Phone

And this is my rule:

{ "rules_tenant_records" : {
"LABEL" : "Tenant records",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_update--apartment" : { "bundle" : "apartment" } },
"IF" : [
  { "entity_has_field" : { "entity" : [ "node-unchanged:field-tenant" ], "field" : "field_tenant" } }
],
"DO" : [
  { "data_set" : {
      "data" : [ "node:field-previous-tenant" ],
      "value" : { "value" : [ "[node:field-tenant]" ] }
    }
  }
]

} }

It does not work, any suggestion?

2 Answers 2

1

Tenant:

enter image description here

Appartment:

enter image description here

Rule:

enter image description here

Result:

enter image description here

Rules Export:

{ "rules_store_tenant_name" : {
    "LABEL" : "Store Tenant Name",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : {
      "node_insert--appartment" : { "bundle" : "appartment" },
      "node_update--appartment" : { "bundle" : "appartment" }
    },
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "node", "id" : [ "node:field-tenant:nid" ] },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "list_add" : {
          "list" : [ "node:field-previous-tentant" ],
          "item" : "[entity-fetched:field-name] [entity-fetched:field-phone]",
          "unique" : "1",
          "pos" : "start"
        }
      }
    ]
  }
}
5
  • Your solution works for me. Thank you so so so much. Is it possible to add you as a friend? :D
    – Tony
    Sep 8, 2016 at 11:29
  • @Tony In your mind? Yes :) On this site? No, unfortunately, there is no such feature :(
    – No Sssweat
    Sep 8, 2016 at 18:27
  • Hi No Ssweat! When I add room, tenant and previous tenant to Views, it lists everything. What I want is to show Room, which has current tenant and previous tenant. I have added to the filter criteria Content: previous tenant (not empty), but it makes a loop as if the room has 10 previous tenants, it shows 10 rows. Do you have any idea? Thanks
    – Tony
    Oct 4, 2016 at 19:00
  • @Tony please create a new question regarding this. Shows 10 rows of what? What I want is to show Room, which has current tenant and previous tenant just the last previous tenant (show only 1) OR you want to show all previous tenants?
    – No Sssweat
    Oct 5, 2016 at 10:56
  • Yes. I want to show all previous tenants, which i have already did. So in your example has 2 previous tenants and one current tenant. But the problem is if i have create a new Tenant which has name for example Jane and in Jane's room does not have any previous tenant, so how to hide Jane's room?
    – Tony
    Oct 5, 2016 at 11:16
1

Do following steps:

Instead of Set data value use Add an item to a list in action. In List field select node:field-previous-tenant, in Item to add field select node-unchanged:field-tenant. At last add an action Save entity and select node in Entity field.

2
  • I've upvoted this because you spotted the two mistakes and it's right but why the save entity? Sep 8, 2016 at 10:58
  • Thank you for your reply. I have tried but it does not work, but @No Sssweat solution works for me
    – Tony
    Sep 8, 2016 at 11:30

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.