How to share, synchronise and update/edit a field across content types?

Events happening in the community are now at Drupal community events on www.drupal.org.
spessex's picture

Hi

I'm not sure if I'm missing something obvious in Drupal 7 but is there a way to share, synchronise and update/edit a field within a content type?

I have two content types that require the same field which relates to 'project status' (this would include statuses such as 'provision', 'live', 'cancelled' etc but in fact around 20+ statuses). Each content types must have the same status and thus be synchronised so any change in one must be reflected in the other.

Is there a way of doing this.

I'm not sure if it matters but the field in question uses hierarchical select which has unlimited field values added to it so essentially a project can have multiple statuses which are added/deleted when required.

Any help would be greatly appreciated.

Stephen

Comments

hi

khaled.zaidan's picture

Let me make sure I understand your issue correctly. You need both fields to have their settings synced, right?

In D7, you can create a field regularly for one content type (which I assume you already did), and then you can reuse that "existing" field in other content types. On the field management page of the content type, you will find you can either create a "new field", or use an "existing field". This way you will have created a new "instance" of that field. And any changes to the settings of the field will be reflected on all its instances.

(note that there are field-settings, and instance-settings. Only the field-settings are synced)

I hope this helps :)

Thanks, but because I'm using

spessex's picture

Thanks, but because I'm using a 'term reference' (from a taxonomy with the list of statuses) all using an existing field does is merely copy the field type, it doesn't appear to have any connection/sync.

So for example if I have Content Type A & B, having created Node A and B in terms of actual content both use the existing field, and both look and work in the same manner. However, there does not appear to be any intrinsic link between the two so if I change the field on Content Type A's node this is not mirrored in Content Type B's node.

You can "reuse" fields but

wipeout_dude's picture

You can "reuse" fields but they are not "synced".. By "reuse" it simply means you can share the database table and the field definition.. The data in the field is unique to the node..

My suggestion to "sync" them if you don't want to custom code something is just to use rules and VBO.. So when the status field is changed on Node A update it to be the same on Node B.. You will probably need some form of link between the nodes using node/entity references so you can get all the related nodes for the rule to update..

Hope that helps..

Thank you. I'll take a look

spessex's picture

Thank you. I'll take a look at this and see what I can come up with.

Done this in Drupal 6

__ian's picture

Hi Steve,

I've done exactly this in module code in Drupal 6. I'm sure it's quite similar in D7, if you like I can send you the code showing how it works.

Hi Ian Sorry for the late

spessex's picture

Hi Ian

Sorry for the late reply, I've just noticed it.

It might be a bit beyond me at the moment but saying that I'm intensively learning PHP now so when you do get 5 mins if you could send it to me it would be much appreciated.

@ Hi Ian, Since we can't

Michael-IDA's picture

@ Hi Ian,

Since we can't attach files here, can you publish your module somewhere (*) and link to it here?

@ Hi Stephen,

“Each content types must have the same status”

One thought is to only have the status field in one content type, e.g. there never is a syncing problem, and then link the two content types by node/entity reference. Your node displays (theme template, views, whatever...) then can pull in the correct status.

Best,
Sam

  • The module development forum would seem like the best choice, but it doesn't allow attachments either, http://drupal.org/forum/4

Hi Sam I've tried tried node

spessex's picture

Hi Sam

I've tried tried node and entity reference but there doesn't appear to be a way in which to bring in the exact field that I required. Node reference brings in the title of the node (not the status field belonging to that node) and entity reference also does not appear to target particular fields unless I'm doing something wrong?

Hi Stephen, No, the reference

Michael-IDA's picture

Hi Stephen,

No, the reference idea is just to get the two nodes linked together, so you can get at whichever one has the status field. It's generally very bad database management to have the same field on two different tables, especially if they are to have the same exact same value across connected records.

To do it properly [1], you're pretty much should use one status field (place it in whichever content type makes the most sense of where is should be edited), link the nodes together with a relationship, and then use a view or theme template to display each content type.

You can use rules (etc.), but expect that at some point you will be manually updating the status field(s) to synchronize them after the rule breaks, cron runs out of memory half way through a run, etc., etc. ...

Best,

Sam

[1] Based upon what you've written. In reality, you should probably take your data needs to a proper DBA, who's familiar with Drupal, and let them create a data layout for you. (I'd guess at a minimum they'll come up with you needing three tables [content types].)

Sam's idea is best

__ian's picture

Hi Stephen,

Just re-reading through all the comments and it dawns on me that your situation isn't the same as mine. I'm making changes to a node of one content type when nodes in another content type change, but the data held in each isn't the same.

In your case, the field to be synced in the nodes of both types are holding exactly the same data, is this correct?

If that's so, then Sam's suggestion is best - hold the data once in one node and refer to it from all other interested nodes. How you do this probably depends on what you need it for.

FYI, I did my sync updates using nodeapi hook (you'll need to make a custom module called 'mymodule' for this example):

function mymodule_nodeapi($node, $op, $arg1, $arg2) {

  // Ensure changes to nodes of mycontenttype are synced
  if ($node->type == 'mycontenttype') {

    switch ($op) {
       case 'update':
         // Check for changes made to a mycontenttype node during update, and copy them to other nodes as required

       case 'insert':
         // Same for new mycontenttype nodes, if applicable

      case 'delete':
         // Cleanup for when a mycontenttype node is deleted
    }
  }
}

Sync data between two content type

Adam2926's picture

Hye everyone,

Good morning.I have problem to match data between two content type. I really need your help regarding on this matter. For your information I have two content type (Property corner and Property announcement) which the field almost same. So, right now i want to sync some data form property corner to property announcement when the developer update their property corner content type. Which means when the developer update the image of project, the number of bathroom. and the number of bedroom in property corner the data that developer upload will also show in Property announcement.

For your information, I have use entity reference to solve this issue but it does't not work perfectly.This only happen if the the entity reference field (record id) is reload again then the filed from property corner will show in property announcement. For your information my entity reference cannot be edit by developer because that data i will be import in property corner.

Really need your help on this. If you have code maybe you can share with me

Regards,
adam

Try Rules module

luisrc7's picture

Hi adam,

Assuming you don't want to write a small custom module to update the content type No.2 "on update" of content type No.1

I would go with Rules module.

You can easily create a Rule with a trigger on contetn type No.1 update and modify or copy any field into content type No.2.

If you are not familiar with rules it could be a little bit confusing how to get the field you need, but the module documentation page is quite good: https://www.drupal.org/documentation/modules/rules

I hope this help you.

Thanks,
Luis.

Sync data

Adam2926's picture

Hye Luis,

Thank you for replying. Really appreciate it, For your details information, i want to copy 4 data form property corner to show in property announcement when developer upload the data.So here the process that what i have done.

1) I have created a field (Record ID) in property announcement as entity field.
Reason: because want to match the record id between this two content type.So the data in property announcement will take only data from the same record id in property corner.

2) Then i set a rule so that data will be set to property announcement.

3) But the result not working perfectly. This is because the filed in property announcement only fill if the developer key in again their record id which is entity reference.

United Kingdom

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: