2

I have 2 content types.

BaseContent: 
Field1: Title
Field2: ID#

RelatedContent: 
Field1: [Entity reference to a BaseContent]
Field2: **[Automatically insert the value of the referenced BaseContent here]**

But I can't work out how to do the part in the **s.

I feel like it should be easy, but nothing I have tried has worked.

Can I do it with any of the following:

-Tokens?

-Entity References using a Views Selection Type and relationships?

-Programatically with PHP (using the Computed Field module?)

If so, how?

any help much appreciated!

  • Have you tried making RelatedField2 a reference that uses a view as the source, and passes the value of RelatedField1 as a contextual filter? (This would require some AJAX or something or else it would require saving the node in between, though, I believe.) – beth Jun 4 '13 at 18:57
  • Thanks. I will try. By 'some AJAX' do you mean the Ajax built into drupal views, or that i'd need to code something by hand? (which would be beyond my abilites right now). Any other advice would be much appreciated as I've been stuck on this for so long. – MiniEggs Jun 6 '13 at 3:07
  • You could try it without writing custom code and see if it works when you manually save the node in between. – beth Jun 6 '13 at 16:19
0

The rules module can do this for you. Set up a rule to fire when (new?) content is saved, set a condition that the content be of type RelatedContent and has a value in Field1. Then set the action to be saving a value in Field2 using the available tokens of the referenced node from Field1. It should be something like [Field1:entity:target_field]

You can do the same thing in php code with hook_node_presave.

Entity reference with a view to limit the selection options would be better used if you just wanted Field1 and wanted to pick the entity to reference based on the value that you would be storing in Field2 if you had a Field2. This is because the view can be used to show you the label using the value of that other field, but leave the key as the entity id needed to be stored in Field1. Since entity reference can be used for a data connection by views, rules, code, etc this is a common practice to reduce your database size and not store the same data in more than one place. However, it is a little more complicated to configure than just using rules.

0

Programmatically is easy to do this.

There are minimum two ways to do it.

First: using field_get_items() where you get referenced ID's. You can use then entity_load() or node_load() depends on your referenced entity.

Second: use Entity Metadata Wrapper - but this depends on Entity module.

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.