Community

Entity reference some code example needed

I like to set the relation between entities using entity reference as module. It is not done by the user but must be done by using code.
I'd like to see an example how to use this and where the code must be stored.

Thanks in advance.
John

Comments

There are a few ways to do

There are a few ways to do this - it depends on how that relation is known. Is it known when adding the node or based upon fields in that node?

For instance, if your user is on a node and creating a related node you might just use Entity Reference Prepopulate and on the reference widget set the field to hidden/disabled if pre-populated & display error if not.

You could use Rules to add the reference upon node create and/or save. Add conditions to verify the bundle or node type, to verify the node has your field (so it can be accessed later), and, if needed to verify it is empty (you'd use a NOT field has value here usually). Then in actions, load your node to relate, set a data value - your field & other node as value. Here is an example for doing it this way: http://dev.nodeone.se/en/creating-multiple-back-links

You could use a custom module using hooks. What hook you'd use again depends on just how you know the relationship. Here are a few tuts/snips I've found useful -
http://eosrei.net/articles/2012/11/programmatically-creating-nodes-using...
http://www.phase2technology.com/node/8440/
http://ado.io/blog/drupal-7/working-programmatically-with-fields-in-drup...

I could provide a code sample, but I'll need to know more about how the relationship is known and when it should be established.

I'm unique, just like everybody else.
If I helped, please pay it forward, backward or sidelong.

Relation reference

Hi,

Thanks for your elaborate answer. Drupal is very overwhelming so I am seeking out the roads to walk on. For relation only there are several ways.

Let me make it more clear.

Rental house
Calendar (weeks, with prices, free or reserved) one entity type with all the weeks
1 or more Booking records (personal details of the main renter and the people who joined him), A booking holds data for max 6 persons.

So the navigation in the site is as follows. The user visit the site and likes the house and press or navigate to the booking page. Here he entered number of weeks. All week slots available will be shown (difficult custom sql query with an Exposed filter number of weeks). If there are free weeks the user can press book the house. At that moment weeks are flagged as reserved as they pay the admin will book them as occupied.

It is doing it like this rental house ==> booking details (personal data of the renters like name, bank account) ==> begin week and end week.
So from booking to start week is an 1 to 1 relation same goes for the end week.
From rental house to booking details is from 1 to many (more people can book the house).

John

Thanks,
John

After reading your reply I'd

After reading your reply I'd be looking at MERCI or AGreservations - if they don't do what you want & can't be hooked into doing so, at least they may provide insight on how to do.

Given that your weeks aren't quantifiable entities it's hard to use rules or vbo or the like to adjust :(

I'm unique, just like everybody else.
If I helped, please pay it forward, backward or sidelong.

Merci

Thanks,

Do you mean this one http://drupal.org/project/merci and this one

http://drupal.org/project/agreservations and this one but for my simple needs I don't need extra modules

Just need a relation between my 3 entitytpes. Maybe it is smarter to link all weeks to the booking content type

John

Thanks,
John

Design

Hi,

I have created a design for my project:

I have created a draft desing http://members.chello.nl/jophof/D7/drupal_rentaldesign.jpg

I hope this clarifies what I am trying to do.

John

Thanks,
John