[SOLVED] How to implement entity field collection in Drupal 7?
Entity in Drupal 7 makes web development much easier, like building structures using LEGO. We can build Entity types, that can be made Bundles. Here we can add fields and then create Entities. This article explains how to add fields.
Often, while building scalable websites, it is important to reduce dependencies as much as possible. The code or database should be such that it should be subject to modification, which must reflect in the website completely without loss of data. This could be accomplished by Field collection. Unlike bundles or types, Field collection isn't there in the core. You must install the field collection module.
Consider an example where there is an entity "group". This contains properties 'user_id', 'created', 'last_accessed' and fields 'name', 'age', 'dob'. Learn how to create a custom entity here.
Entity user:
- Properties:
- user_id
- created
- last_accessed
- Fields:
- name
- age
- dob
This entity can be extended by adding a field collection which is separately maintained but linked to the entity user. Consider the field collection "address" with fields 'house_no', 'lane', 'pin_code', 'state'. In the <MODULE_NAME>.fields.inc file, this is how you add a field collection. While defining the field collection, make sure you define a bundle for address and use that bundle for the subsequent fields in that field collection (house_no, lane, pin_code, state).
$user_address['address'] = array(
'field' => array(
'field_name' => 'address',
'entity_types' => array('field_collection_item'),
'cardinality' => 1,
'active' => 1,
'type' => 'user_reference',
'foreign keys' => array(
'uid' => array(
'columns' => array(
'uid' => 'uid',
),
'table' => 'users',
),
),
'indexes' => array(
'uid' => array(
0 => 'uid',
),
),
'locked' => 0,
'module' => 'user_reference',
),
'instance' => array(
'field_name' => 'address',
'label' => 'The address of the user',
'entity_type' => 'field_collection_item',
'bundle' => 'user_address',
'description' => 'The user details.',
'default_value' => NULL,
'deleted' => 0,
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'user_reference',
'settings' => array(),
'type' => 'user_reference_default',
'weight' => 0,
),
),
'label' => 'Address',
'required' => 0,
'settings' => array(
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'user_reference',
'settings' => array(
'autocomplete_match' => 'contains',
'autocomplete_path' => 'user_reference/autocomplete',
'size' => 60,
),
'type' => 'user_reference_autocomplete',
),
)
);
$user_address['house_no'] = array(
'field' => array(
'field_name' => 'house_no',
'entity_types' => array('field_collection_item'),
'cardinality' => 1,
'type' => 'text',
'settings' => array('max_length' => 255),
),
'instance' => array(
'field_name' => 'house_no',
'label' => 'House No.',
'entity_type' => 'field_collection_item',
'bundle' => 'user_address',
'description' => 'The house no.',
)
);
$user_address['lane'] = array(
'field' => array(
'field_name' => 'lane',
'entity_types' => array('field_collection_item'),
'cardinality' => 1,
'type' => 'text',
'settings' => array('max_length' => 255),
),
'instance' => array(
'field_name' => 'lane',
'label' => 'Lane',
'entity_type' => 'field_collection_item',
'bundle' => 'user_address',
'description' => 'The name of the Lane.',
)
);
$user_address['pin_code'] = array(
'field' => array(
'field_name' => 'pin_code',
'entity_types' => array('field_collection_item'),
'cardinality' => 1,
'type' => 'integer',
),
'instance' => array(
'field_name' => 'pin_code',
'label' => 'Pin Code',
'entity_type' => 'field_collection_item',
'bundle' => 'user_address',
'description' => 'The pin code of your area.',
)
);
$user_address['state'] = array(
'field' => array(
'field_name' => 'state',
'entity_types' => array('field_collection_item'),
'cardinality' => 1,
'type' => 'integer',
),
'instance' => array(
'field_name' => 'state',
'label' => 'State',
'entity_type' => 'field_collection_item',
'bundle' => 'user_address',
'description' => 'The state you live in.',
)
);
Please feel free to share your queries with us here. If you face any difficulties with Drupal coding, don't worry. Our Drupal experts will take care of your concerns.