From 11e5c1da65b362b6b34b1e7a7e1ee0e1db3279a7 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 24 Apr 2013 13:27:11 +0100 Subject: [PATCH] Issue #1971668 by amateescu, Berdir: Add bundle support to EntityTest* entity types. --- .../Tests/EntitySerializationTest.php | 4 ++ .../system/Tests/Entity/EntityFieldTest.php | 2 + .../modules/entity_test/entity_test.install | 28 ++++++++ .../modules/entity_test/entity_test.module | 69 +++++++++++++++++++ .../EntityTestStorageController.php | 17 ++++- .../Plugin/Core/Entity/EntityTest.php | 9 +++ .../Core/Entity/EntityTestDefaultAccess.php | 3 +- .../Plugin/Core/Entity/EntityTestLabel.php | 3 +- .../Core/Entity/EntityTestLabelCallback.php | 3 +- .../Plugin/Core/Entity/EntityTestMul.php | 1 + .../Plugin/Core/Entity/EntityTestMulRev.php | 1 + .../Plugin/Core/Entity/EntityTestNoLabel.php | 1 + .../Plugin/Core/Entity/EntityTestRender.php | 3 +- .../Plugin/Core/Entity/EntityTestRev.php | 1 + 14 files changed, 140 insertions(+), 5 deletions(-) diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php index f6c106ee533..ce90382c083 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php @@ -90,6 +90,9 @@ class EntitySerializationTest extends NormalizerTestBase { 'name' => array( array('value' => $this->values['name']), ), + 'type' => array( + array('value' => 'entity_test_mulrev'), + ), 'user_id' => array( array('target_id' => $this->values['user_id']), ), @@ -138,6 +141,7 @@ class EntitySerializationTest extends NormalizerTestBase { 'langcode' => '' . LANGUAGE_NOT_SPECIFIED . '', 'default_langcode' => '', 'name' => '' . $this->values['name'] . '', + 'type' => 'entity_test_mulrev', 'user_id' => '' . $this->values['user_id'] . '', 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '', ); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index 3c5679b23df..fabbcfe4116 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -491,6 +491,8 @@ class EntityFieldTest extends EntityUnitTestBase { $entity->uuid->value, LANGUAGE_NOT_SPECIFIED, $this->entity_name, + // Bundle name. + $entity->bundle(), $this->entity_field_text, // Field format. NULL, diff --git a/core/modules/system/tests/modules/entity_test/entity_test.install b/core/modules/system/tests/modules/entity_test/entity_test.install index d0da0509447..3465e8c7565 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.install +++ b/core/modules/system/tests/modules/entity_test/entity_test.install @@ -58,6 +58,13 @@ function entity_test_schema() { 'length' => 128, 'not null' => FALSE, ), + 'type' => array( + 'description' => 'The bundle of the test entity.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), 'langcode' => array( 'description' => 'The {language}.langcode of the original variant of this test entity.', 'type' => 'varchar', @@ -108,6 +115,13 @@ function entity_test_schema() { 'length' => 128, 'not null' => FALSE, ), + 'type' => array( + 'description' => 'The bundle of the test entity.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), 'langcode' => array( 'description' => 'The {language}.langcode of the original variant of this test entity.', 'type' => 'varchar', @@ -190,6 +204,13 @@ function entity_test_schema() { 'length' => 128, 'not null' => FALSE, ), + 'type' => array( + 'description' => 'The bundle of the test entity.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), 'langcode' => array( 'description' => 'The {language}.langcode of the original variant of this test entity.', 'type' => 'varchar', @@ -271,6 +292,13 @@ function entity_test_schema() { 'length' => 128, 'not null' => FALSE, ), + 'type' => array( + 'description' => 'The bundle of the test entity.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), 'langcode' => array( 'description' => 'The {language}.langcode of the original variant of this test entity.', 'type' => 'varchar', diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 80f1b3945fd..673ab6714df 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -63,6 +63,75 @@ function entity_test_entity_info_alter(&$info) { } } +/** + * Creates a new bundle for entity_test entities. + * + * @param string $bundle + * The machine-readable name of the bundle. + * @param string $text + * (optional) The human-readable name of the bundle. If none is provided, the + * machine name will be used. + * @param string $entity_type + * (optional) The entity type for which the bundle is created. Defaults to + * 'entity_test'. + */ +function entity_test_create_bundle($bundle, $text = NULL, $entity_type = 'entity_test') { + $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle')); + $bundles += array($bundle => array('label' => $text ? $text : $bundle)); + Drupal::state()->set($entity_type . '.bundles', $bundles); + + entity_invoke_bundle_hook('create', $entity_type, $bundle); +} + +/** + * Renames a bundle for entity_test entities. + * + * @param string $bundle_old + * The machine-readable name of the bundle to rename. + * @param string $bundle_new + * The new machine-readable name of the bundle + * @param string $entity_type + * (optional) The entity type for which the bundle is renamed. Defaults to + * 'entity_test'. + */ +function entity_test_rename_bundle($bundle_old, $bundle_new, $entity_type = 'entity_test') { + $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle')); + $bundles[$bundle_new] = $bundles[$bundle_old]; + unset($bundles[$bundle_old]); + Drupal::state()->set($entity_type . '.bundles', $bundles); + + entity_invoke_bundle_hook('rename', $entity_type, $bundle_old, $bundle_new); +} + +/** + * Deletes a bundle for entity_test entities. + * + * @param string $bundle + * The machine-readable name of the bundle to delete. + * @param string $entity_type + * (optional) The entity type for which the bundle is deleted. Defaults to + * 'entity_test'. + */ +function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') { + $bundles = Drupal::state()->get($entity_type . '.bundles') ?: array('entity_test' => array('label' => 'Entity Test Bundle')); + unset($bundles[$bundle]); + Drupal::state()->set($entity_type . '.bundles', $bundles); + + entity_invoke_bundle_hook('delete', $entity_type, $bundle); +} + +/** + * Implements hook_entity_bundle_info_alter(). + */ +function entity_test_entity_bundle_info_alter(&$bundles) { + $entity_info = entity_get_info(); + foreach ($bundles as $entity_type => $info) { + if ($entity_info[$entity_type]['module'] == 'entity_test') { + $bundles[$entity_type] = Drupal::state()->get($entity_type . '.bundles') ?: array($entity_type => array('label' => 'Entity Test Bundle')); + } + } +} + /** * Implements hook_field_extra_fields(). */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php index 500a41cf8a5..8c5e23ff352 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php @@ -18,7 +18,17 @@ use Drupal\Core\Entity\DatabaseStorageControllerNG; class EntityTestStorageController extends DatabaseStorageControllerNG { /** - * Implements \Drupal\Core\Entity\DataBaseStorageControllerNG::baseFieldDefinitions(). + * {@inheritdoc} + */ + public function create(array $values) { + if (empty($values['type'])) { + $values['type'] = $this->entityType; + } + return parent::create($values); + } + + /** + * {@inheritdoc} */ public function baseFieldDefinitions() { $fields['id'] = array( @@ -43,6 +53,11 @@ class EntityTestStorageController extends DatabaseStorageControllerNG { 'type' => 'string_field', 'translatable' => TRUE, ); + $fields['type'] = array( + 'label' => t('Type'), + 'description' => t('The bundle of the test entity.'), + 'type' => 'string_field', + ); $fields['user_id'] = array( 'label' => t('User ID'), 'description' => t('The ID of the associated user.'), diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php index bf9a10dde88..aa6bda3d98b 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php @@ -31,6 +31,7 @@ use Drupal\Core\Annotation\Translation; * entity_keys = { * "id" = "id", * "uuid" = "uuid", + * "bundle" = "type" * }, * menu_base_path = "entity-test/manage/%entity_test" * ) @@ -51,6 +52,13 @@ class EntityTest extends EntityNG { */ public $uuid; + /** + * The bundle of the test entity. + * + * @var \Drupal\Core\Entity\Field\FieldInterface + */ + public $type; + /** * The name of the test entity. * @@ -75,6 +83,7 @@ class EntityTest extends EntityNG { unset($this->uuid); unset($this->name); unset($this->user_id); + unset($this->type); } /** diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestDefaultAccess.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestDefaultAccess.php index c6b025dc0bc..a06333842b6 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestDefaultAccess.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestDefaultAccess.php @@ -22,7 +22,8 @@ use Drupal\Core\Annotation\Translation; * }, * base_table = "entity_test", * entity_keys = { - * "id" = "id" + * "id" = "id", + * "bundle" = "type" * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabel.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabel.php index 970f5e760e7..516635ba46f 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabel.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabel.php @@ -23,7 +23,8 @@ use Drupal\Core\Annotation\Translation; * base_table = "entity_test", * entity_keys = { * "id" = "id", - * "label" = "name" + * "label" = "name", + * "bundle" = "type" * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabelCallback.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabelCallback.php index 393b1103d94..867fd746b56 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabelCallback.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestLabelCallback.php @@ -26,7 +26,8 @@ use Drupal\Core\Annotation\Translation; * label_callback = "entity_test_label_callback", * fieldable = TRUE, * entity_keys = { - * "id" = "id" + * "id" = "id", + * "bundle" = "type" * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMul.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMul.php index 887e3d4f18b..d39f91577b5 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMul.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMul.php @@ -33,6 +33,7 @@ use Drupal\Core\Annotation\Translation; * entity_keys = { * "id" = "id", * "uuid" = "uuid", + * "bundle" = "type" * }, * menu_base_path = "entity_test_mul/manage/%entity_test_mul" * ) diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMulRev.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMulRev.php index bef21482ee6..aa5fcd9f9fd 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMulRev.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestMulRev.php @@ -35,6 +35,7 @@ use Drupal\Core\Annotation\Translation; * "id" = "id", * "uuid" = "uuid", * "revision" = "revision_id", + * "bundle" = "type" * }, * menu_base_path = "entity_test_mulrev/manage/%entity_test_mulrev" * ) diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestNoLabel.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestNoLabel.php index 6b101528fe0..e7d416978aa 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestNoLabel.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestNoLabel.php @@ -24,6 +24,7 @@ use Drupal\Core\Annotation\Translation; * base_table = "entity_test", * entity_keys = { * "id" = "ftid", + * "bundle" = "type" * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRender.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRender.php index 14b8cd00948..90222ba4b29 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRender.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRender.php @@ -26,7 +26,8 @@ use Drupal\Core\Annotation\Translation; * entity_keys = { * "id" = "id", * "uuid" = "uuid", - * "label" = "name" + * "label" = "name", + * "bundle" = "type" * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRev.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRev.php index 7d01d5796f6..d3984ea0df5 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRev.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTestRev.php @@ -33,6 +33,7 @@ use Drupal\Core\Annotation\Translation; * "id" = "id", * "uuid" = "uuid", * "revision" = "revision_id", + * "bundle" = "type" * }, * menu_base_path = "entity_test_rev/manage/%entity_test_rev" * )