1

I have a City entity with two fields (Title and Population), and I have created two instances of that entity.

Can I add different fields to these two instances?

Thank for you responses but I want different approach Let me put my question in this way

1) I have entity: City (with fields Population and Title)

2) of which I created two content (what should i call Chicago an New York ? contens of type City or anything else??):

Chicago and New York

3) now i know the entity City is fieldable i.e i can add custome fields to it, but those will be accesible to both ‘Chicago’ and ‘New York

4) What I want is ‘Chicago’ and ‘New York’ also be fieldable and have distinct field as well as common field

5) so, it should look like as Chicago : (Title - ‘Chicago’, Population - 1000, CField1 - ‘Hello’) New York: (Title - ‘Chicago’, Population - 1000, NField1 - ‘Hello’, NField2 - ‘Hello’)

Please tell me if above requirement is possible to implement

1

In the same way fields are added to content types, and not to node instances, fields are added to entity's bundles, not to entity instances.

If you don't want to use two different bundles for the same entity (which IMO is the better solution), you can implement hook_field_access(), and make some fields editable/visible basing on on the value of some entity properties, such as the title, or the entity ID.
For example, the following code would make a field visible, and editable, only when the title property of the entity is equal to "New York"; not even the user #1 would be able to see it. Practically, it is like the field is not present.

function mymodule_field_access($op, $field, $entity_type, $entity, $account) {
  if ($field['field_name'] == 'your field' && entity_type == 'your entity type' && $entity->title == "New York") {
    return FALSE;
  }

  return TRUE;
}

This example is very simplified. You should normally check the value of $op, just in case future versions of Drupal introduce new values for that parameter.

  • Thanks for your response.. can you please see updated question – Chetan Nov 8 '12 at 16:27
  • My answer is still the same: Fields are added to entity bundles, not to entity instances. You cannot have a field that is attached just to an entity instance, and not another one, if they are both of the same entity bundle. You can avoid a field is editable, and visible, though; that would be like if it doesn't exist for that entity instance. – kiamlaluno Nov 8 '12 at 20:56
  • Thank you very much.. I stretched it cause I thought there will be some way to do that.. – Chetan Nov 8 '12 at 21:12
  • I am again reply to this post as i found one module drupal.org/node/1274796 now as with my problem i have to add diffrent field at instance level... can i use relation module here??? like in example given above can i use relation "Chicago --> hasField --> CField 1" and " Newyork --> hasField --> Nfield1", " Newyork --> hasField --> Nfield2"... please let me if this is possible with relation module because as per my modules requirement i must have to attach custom fields at instance level.. so hook_field_access() is not going to work for me... – Chetan Nov 12 '12 at 17:56
  • The Relation module is for relationships between two entities, not between an entity and a field. – kiamlaluno Nov 12 '12 at 18:50
1

This single entity will use all of the fields you attach to it – it's still 1 instance (what we call a 'bundle') of an entity.

If you want to conditionally allow fields to be displayed on the entity edit form (or the display of the entity) use the Conditional Fields module: http://drupal.org/project/conditional_fields

If you really want to use different sets of fields, you need 2 different bundles, i.e. a "city" entity of bundle "foo" and a "city" entity of bundle "bar".

  • Thanks for your response. Actually I implemented this using two different bundles but as per requirement it has to be done using same bundle and different set of field (i.e common + different) – Chetan Nov 7 '12 at 21:19
  • In that case I would use conditional fields, hook_field_access, or custom code that implements #states (api.drupal.org/api/drupal/includes%21common.inc/function/…). I've just finished work on a D7 site where a content type changes quite drastically depending on an initial dropdown option, but for many reasons it must remain 1 bundle. – Charlie Schliesser Nov 8 '12 at 13:51
  • Thanks for your response.. can you please see updated question – Chetan Nov 8 '12 at 16:27

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.