Created
November 29, 2012 11:18
-
-
Save amateescu/4168320 to your computer and use it in GitHub Desktop.
Entity bundles as plugins architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- introduce a EntityBundle plugin type | |
- create a derivative class that provides the list of bundles for all entity types that | |
a) have no default bundles | |
b) have bundles defined by a config entity type | |
- the advantage of using the plugin system is that it automatically gives us a true entity sub-type concept | |
i.e. | |
/** | |
* @Plugin( | |
* id = "node:page", | |
* label = @Translation("Page") | |
* ) | |
*/ | |
class NodePage extends Node { | |
... | |
some useful methods specific to the 'page' bundle. | |
... | |
} | |
- these subtypes can happily co-exist with bundles defined by a config entity type | |
i.e. the Node entity can have bundles managed by the NodeType config entity type but also the default NodePage bundle defined in code, as a plugin | |
- another goal is to use the EntityBundleManager for instantiating Entity objects | |
i.e. | |
function entity_create($entity_type, array $values) { | |
// Get the bundle from $values, or use a default one if it's not provided (let's say article for this example). | |
$bundle = 'article'; | |
drupal_container()->get('plugin.manager.entity_bundle')->createInstance($entity_type . ':' . $bundle, array('values' => $values)); | |
} | |
- the constructor of the Entity object calls the create() method from the appropiate storage controller. yes, this would switch the current order of the create operation. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment