Typical Entity Example
Categories
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Downloads
Component created
Component changed
Component body
Typical Entity Example module is an educational module which shows how to create
entities for Drupal in your modules.
This module contains some sub-modules, and each of these sub-modules implements
an educational Drupal entity.
- Typical Entity Example: Typical Entity Example module is a parent module for all its sub-modules and implements the Drupal permissions, used by these sub-modules.
- Typical Entity Example 1: Typical Entity Example 1 sub-module creates the smallest possible entity with machine name "typical_entity_1". This entity exists in Drupal, but you cannot even create the instances of it.
- Typical Entity Example 2: Typical Entity Example 2 sub-module creates a minimalistic entity, the instances of which can be created, edited or deleted with Entity Metadata Wrappers.
- Typical Entity Example 3: Typical Entity Example 3 sub-module creates a small entity, which has some predefined bundles and attached fields.
- Typical Entity Example 4: Typical Entity Example 4 sub-module creates the entity, the instances of which can be created and edited with a simple user interface.
- Typical Entity Example 5: Typical Entity Example 5 sub-module creates the entity, which can be used in views. Also created entity supports revisions.
- Typical Entity Example 6: Typical Entity Example 6 sub-module creates the entity, which bundles can be created and edited via Drupal user interface.
Typical use cases for each created entity type
You cannot create instances of typical_entity_example_1, because this is the smallest possible entity which is pretty useless.
Example usage of typical_entity_example_2:
// Create new entity.
$entity = entity_create('typical_entity_example_2', array());
$wrapper_1 = entity_metadata_wrapper('typical_entity_example_2', $entity);
$wrapper_1->title = 'Random name 1';
$wrapper_1->description = 'Random description 1';
$wrapper_1->save();
// Load and edit created entity.
$entities = entity_load(
'typical_entity_example_2',
array($wrapper_1->teid->value())
);
$wrapper_2 = entity_metadata_wrapper(
'typical_entity_example_2',
reset($entities)
);
$wrapper_2->title = 'Random name 2';
$wrapper_2->save();
// Delete created entity.
$wrapper_2->delete();
Example usage of typical_entity_example_3:
$entity = entity_create(
'typical_entity_example_3',
array('type' => 'typical_entity_example_3_bundle_1')
);
$wrapper = entity_metadata_wrapper('typical_entity_example_3', $entity);
$wrapper->title = 'Random title 1.';
$wrapper->description = 'Random description 1.';
$wrapper->typical_entity_example_3_field_1 = 'Random field 1.';
$wrapper->save();
Typical_entity_example_4, typical_entity_example_5 and typical_entity_example_6 can be processed by code like previous entity types, but they also have user interfaces.
Example use case for typical_entity_example_4:
- Go to /typical_entity_example_4
- Press "Add new entity"
- Choose the bundle of entity you want to create
- Enter the data of the new entity and press "Save"
- Go to /typical_entity_example_4 again and you will see the created entity in the entity list
Example use case for typical_entity_example_5:
- Go to /typical_entity_example_5
- Press "Add Typical Entity 5"
- Choose the bundle of entity you want to create
- Enter the data of the new entity and press "Save"
- Go to the "revisions" tab of the new entity to see its revisions.
- Go to /typical_entity_example_5 again and you will see the created entity in the view called "Typical Entity 5". This view can be changed on /admin/structure/views
Example use case for typical_entity_example_6:
- Go to /admin/structure/typical_entity_example_6 and press "Add typical entity 6 type"
- Enter the name of new entity type and click "Save"
- Add fields to entity type and configure it as you wish
- Go to /typical_entity_example_6
- Press "Add Typical Entity 6"
- Choose the bundle of entity you just created
- Enter the data of the new entity and press "Save entity"
- Go to /typical_entity_example_6 again and you will see the created entity in the view called "Typical Entity 6".
Credits
Development of this module was sponsored by the OITS Co. Ltd.