Commit 11e5c1da authored by alexpott's avatar alexpott

Issue #1971668 by amateescu, Berdir: Add bundle support to EntityTest* entity types.

parent f9a4d699
......@@ -90,6 +90,9 @@ public function testNormalize() {
'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 @@ public function testSerialize() {
'langcode' => '<langcode><value>' . LANGUAGE_NOT_SPECIFIED . '</value></langcode>',
'default_langcode' => '<default_langcode><value/></default_langcode>',
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
'type' => '<type><value>entity_test_mulrev</value></type>',
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>',
);
......
......@@ -491,6 +491,8 @@ protected function assertDataStructureInterfaces($entity_type) {
$entity->uuid->value,
LANGUAGE_NOT_SPECIFIED,
$this->entity_name,
// Bundle name.
$entity->bundle(),
$this->entity_field_text,
// Field format.
NULL,
......
......@@ -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',
......
......@@ -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().
*/
......
......@@ -18,7 +18,17 @@
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 @@ public function baseFieldDefinitions() {
'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.'),
......
......@@ -31,6 +31,7 @@
* 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 @@ protected function init() {
unset($this->uuid);
unset($this->name);
unset($this->user_id);
unset($this->type);
}
/**
......
......@@ -22,7 +22,8 @@
* },
* base_table = "entity_test",
* entity_keys = {
* "id" = "id"
* "id" = "id",
* "bundle" = "type"
* }
* )
*/
......
......@@ -23,7 +23,8 @@
* base_table = "entity_test",
* entity_keys = {
* "id" = "id",
* "label" = "name"
* "label" = "name",
* "bundle" = "type"
* }
* )
*/
......
......@@ -26,7 +26,8 @@
* label_callback = "entity_test_label_callback",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id"
* "id" = "id",
* "bundle" = "type"
* }
* )
*/
......
......@@ -33,6 +33,7 @@
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type"
* },
* menu_base_path = "entity_test_mul/manage/%entity_test_mul"
* )
......
......@@ -35,6 +35,7 @@
* "id" = "id",
* "uuid" = "uuid",
* "revision" = "revision_id",
* "bundle" = "type"
* },
* menu_base_path = "entity_test_mulrev/manage/%entity_test_mulrev"
* )
......
......@@ -24,6 +24,7 @@
* base_table = "entity_test",
* entity_keys = {
* "id" = "ftid",
* "bundle" = "type"
* }
* )
*/
......
......@@ -26,7 +26,8 @@
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "label" = "name"
* "label" = "name",
* "bundle" = "type"
* }
* )
*/
......
......@@ -33,6 +33,7 @@
* "id" = "id",
* "uuid" = "uuid",
* "revision" = "revision_id",
* "bundle" = "type"
* },
* menu_base_path = "entity_test_rev/manage/%entity_test_rev"
* )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment