diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index b8c38ce..8cc5c25 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; +use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; use Drupal\Core\Cache\CacheBackendInterface; /** @@ -208,7 +209,9 @@ class EntityManager extends PluginManagerBase { */ public function __construct() { // Allow the plugin definition to be altered by hook_entity_info_alter(). - $this->discovery = new AlterDecorator(new AnnotatedClassDiscovery('Core', 'Entity'), 'entity_info'); + $this->discovery = new AlterDecorator( + new DerivativeDiscoveryDecorator( + new AnnotatedClassDiscovery('Core', 'Entity')), 'entity_info'); $this->factory = new DefaultFactory($this); // Entity type plugins includes translated strings, so each language is diff --git a/core/modules/entity_type/entity_type.info b/core/modules/entity_type/entity_type.info new file mode 100644 index 0000000..bc710c5 --- /dev/null +++ b/core/modules/entity_type/entity_type.info @@ -0,0 +1,6 @@ +name = Entity Type +description = Allow the creation of entity types dynamically. +package = Core +version = VERSION +core = 8.x +configure = admin/structure/entity-types diff --git a/core/modules/entity_type/entity_type.install b/core/modules/entity_type/entity_type.install new file mode 100644 index 0000000..2d827e7 --- /dev/null +++ b/core/modules/entity_type/entity_type.install @@ -0,0 +1,16 @@ +get("name"); + $schema[$name] = entity_type_default_schema(); + } + + return $schema; +} diff --git a/core/modules/entity_type/entity_type.module b/core/modules/entity_type/entity_type.module new file mode 100644 index 0000000..ea6a3b6 --- /dev/null +++ b/core/modules/entity_type/entity_type.module @@ -0,0 +1,24 @@ + 'The base table for nodes.', + 'fields' => array( + 'id' => array( + 'description' => 'The primary identifier for a node.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'type' => array( + 'description' => 'The {node_type}.type of this node.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'primary key' => array('id'), + ); +} diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityStorageManagerInterface.php b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityStorageManagerInterface.php new file mode 100644 index 0000000..22889a7 --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityStorageManagerInterface.php @@ -0,0 +1,25 @@ + diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityType.php b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityType.php new file mode 100644 index 0000000..660c34d --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityType.php @@ -0,0 +1,75 @@ +name) ? $this->name : NULL; + } + + /** + * Create the entity type table, if it has not been created yet + */ + public function save(){ + $this->initializeManager(); + $this->manager->initialize(); + parent::save(); + } + + /** + * Delete the entity type table + */ + public function delete(){ + $this->initializeManager(); + $this->manager->destroy(); + parent::delete(); + } + + private function initializeManager(){ + if(!$this->manager){ + $this->manager = new $this->manager_class($this); + } + } +} + diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeDerivatives.php b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeDerivatives.php new file mode 100644 index 0000000..5ce6966 --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeDerivatives.php @@ -0,0 +1,47 @@ +getDerivativeDefinitions($base_plugin_definition); + if (isset($derivatives[$derivative_id])) { + return $derivatives[$derivative_id]; + } + } + + /** + * Gets EntityType configs and creates plugin definitions. + */ + public function getDerivativeDefinitions(array $base_plugin_definition) { + $derivatives = array(); + $entity_types = config_get_storage_names_with_prefix('entity_type'); + foreach($entity_types as $et){ + $obj = config($et); + $name = $obj->get("name"); + + $derivatives[$name] = array( + 'id' => 'id', + 'label' => $name, + 'base_table' => $name, + ) + $base_plugin_definition; + } + return $derivatives; + } +} + diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeInfo.php b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeInfo.php new file mode 100644 index 0000000..729bd94 --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/EntityTypeInfo.php @@ -0,0 +1,24 @@ + diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/SQLEntityStorageManager.php b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/SQLEntityStorageManager.php new file mode 100644 index 0000000..f1d059b --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Plugin/Core/Entity/SQLEntityStorageManager.php @@ -0,0 +1,27 @@ +entity_type = $entity_type; + } + + public function initialize() { + $id = $this->entity_type->id(); + if($id && !db_table_exists($id)){ + db_create_table($id, entity_type_default_schema()); + } + } + + public function destroy() { + $id = $this->entity_type->id(); + db_drop_table($id); + } +} + +?> diff --git a/core/modules/entity_type/lib/Drupal/entity_type/Tests/EntityTypeTest.php b/core/modules/entity_type/lib/Drupal/entity_type/Tests/EntityTypeTest.php new file mode 100644 index 0000000..2efd7ba --- /dev/null +++ b/core/modules/entity_type/lib/Drupal/entity_type/Tests/EntityTypeTest.php @@ -0,0 +1,31 @@ + 'Entity type tests', + 'description' => 'Test for dynamic entity types.', + 'group' => 'Entity Type', + ); + } + + function testEntityType() { + $et = new EntityType(array('name' => "fake"), "entity_type"); + $et->save(); + } +} diff --git a/core/modules/entity_type/modules/entity_type_example/entity_type_example.info b/core/modules/entity_type/modules/entity_type_example/entity_type_example.info new file mode 100644 index 0000000..d0c0c1f --- /dev/null +++ b/core/modules/entity_type/modules/entity_type_example/entity_type_example.info @@ -0,0 +1,6 @@ +name = Entity Type Example +description = Entity type example. +package = Core +version = VERSION +core = 8.x +dependencies[] = entity_type diff --git a/core/modules/entity_type/modules/entity_type_example/entity_type_example.install b/core/modules/entity_type/modules/entity_type_example/entity_type_example.install new file mode 100644 index 0000000..d677dfa --- /dev/null +++ b/core/modules/entity_type/modules/entity_type_example/entity_type_example.install @@ -0,0 +1,21 @@ +name = 'entitytypetest'; + $et->manager_class = "Drupal\entity_type\Plugin\Core\Entity\SQLEntityStorageManager"; + $et->save(); +} + +function entity_type_example_uninstall(){ + //lets delete our entity type + $cs = new EntityTypeStorageController(); + $ets = $cs->load(); + if($ets){ + $et = array_shift($ets); + $et->delete(); + } +} \ No newline at end of file diff --git a/core/modules/entity_type/modules/entity_type_example/entity_type_example.module b/core/modules/entity_type/modules/entity_type_example/entity_type_example.module new file mode 100644 index 0000000..80d0f60 --- /dev/null +++ b/core/modules/entity_type/modules/entity_type_example/entity_type_example.module @@ -0,0 +1,24 @@ + 'Create Entity', + 'description' => 'Create an entity of the entity type created by entity_type_example.', + 'page callback' => 'entity_type_example_create', + 'access arguments' => array('access content overview'), + 'weight' => -10, + ); + + return $items; +} + +function entity_type_example_create(){ + + $entity = new Entity(array('type'=>'entitytypeexample'), + "entity_type_info:entitytypetest"); + $entity->save(); + + return array('#markup' => "entitytypetest's entity {$entity->id()} has been created"); +} \ No newline at end of file