2

I understand that it is best reuse fields where possible and to avoid unnecessary duplication.

So is it possible to duplicate or reuse a field (or field instance) on the same content type?

For Example: A news article with both a cover image and an image gallery. Is it possible for both to use field_image or must there be two distinct fields, such as field_cover_image and field_gallery_image ???

  • IS IT best to reuse fields though? I am asking since I don't know the answer to that. Where did you read about that? I though it doesn't make a huge difference. – Beebee Mar 13 '15 at 17:40
  • 1
    This is my understanding. Let's say that all your content types have a cover image. You would reuse the field_cover_image field for each content type. The alternative would be to have different cover image fields for each content type. For example field_page_cover_image, field_article_cover_image however this would result in unnecessary duplication. – MrPaulDriver Mar 13 '15 at 17:45
3

The crux of the matter you stated as '..to avoid unnecessary duplication'.

Let your conceptual requirements dictate this and not your ideas of 'let's keep the database schema lean', or ideas of performance or something similar.

To take your own example from your comment. If you have different content types that serve the same purpose or that are grouped together somehow (ie all are products types that you group under a menu product catalog) and each product's type has a cover image that you want to show when you navigate to that product type listing, then re-using the cover image for each product type makes sense because it is conceptually similar.

On the contrary, re-using a product type cover image for a user image field would not make sense as it is conceptually unrelated, even though it might have the exact same database schema.

Not being able to re-use a field on the same content type makes sense from a conceptual point of view also. To have multiple fields all you need to do is set the multiplicity on the field, whether the field is 'simple', a field collection, or an entity reference. This already enforces a conceptual similarity.

Always design to your conceptual requirements first. This allows for:

  1. better understanding of the design.

  2. being able to easier adapt to changing future requirements

  3. cleaner custom sql queries against the field's database if need be.

  • That has cleared a lot up for me @J.Reynolds. Many thanks – MrPaulDriver Mar 14 '15 at 11:18
  • In some cases it would make sense to use the same field on the same content type multiple times. For example entity reference field. You could have multiple taxonomies you want to add to a content type and now you need to add for each one new field. Could be solved with just one field used multiple times. I have some idea that I saw somewhere explanation that it is problematic for DB queries, when you fetch entity data and have multiple selects trying to access the same field data. – Marko Blazekovic Nov 24 '18 at 19:40
4

No, field instance is unique within a content type. See field_create_instance. Relevant code for checking uniqueness in that function:

// Ensure the field instance is unique within the bundle.
// We only check for instances of active fields, since adding an instance of
// a disabled field is not supported.
$prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
if (!empty($prior_instance)) {
  $message = t('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $instance['field_name'], '@bundle' => $instance['bundle']));
  throw new FieldException($message);
}
  • I would like to know why is this enforced? – Marko Blazekovic Nov 23 '18 at 12:43
  • 1
    @MarkoBlazekovic the answer above should help to understand the reason why. – Beebee Nov 23 '18 at 17:28

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.