Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
220
Merge Requests
220
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
8007ab1b
Commit
8007ab1b
authored
Feb 15, 2015
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2423213
by plach: Schema for newly defined entity types is never created
parent
f671f3c0
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
157 additions
and
66 deletions
+157
-66
core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php
.../lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php
+64
-64
core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManagerInterface.php
...al/Core/Entity/EntityDefinitionUpdateManagerInterface.php
+21
-0
core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php
...locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php
+2
-0
core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php
...les/system/src/Tests/Entity/EntityDefinitionTestTrait.php
+9
-0
core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php
...es/system/src/Tests/Entity/EntityDefinitionUpdateTest.php
+20
-0
core/modules/system/src/Tests/Update/UpdateScriptTest.php
core/modules/system/src/Tests/Update/UpdateScriptTest.php
+2
-0
core/modules/system/tests/modules/entity_test/entity_test.module
...dules/system/tests/modules/entity_test/entity_test.module
+9
-2
core/modules/system/tests/modules/entity_test/src/Entity/EntityTestNew.php
...em/tests/modules/entity_test/src/Entity/EntityTestNew.php
+30
-0
No files found.
core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php
View file @
8007ab1b
...
...
@@ -18,27 +18,6 @@
class
EntityDefinitionUpdateManager
implements
EntityDefinitionUpdateManagerInterface
{
use
StringTranslationTrait
;
/**
* Indicates that a definition has just been created.
*
* @var int
*/
const
DEFINITION_CREATED
=
1
;
/**
* Indicates that a definition has changes.
*
* @var int
*/
const
DEFINITION_UPDATED
=
2
;
/**
* Indicates that a definition has just been deleted.
*
* @var int
*/
const
DEFINITION_DELETED
=
3
;
/**
* The entity manager service.
*
...
...
@@ -70,6 +49,22 @@ public function getChangeSummary() {
$summary
=
array
();
foreach
(
$this
->
getChangeList
()
as
$entity_type_id
=>
$change_list
)
{
// Process entity type definition changes.
if
(
!
empty
(
$change_list
[
'entity_type'
]))
{
$entity_type
=
$this
->
entityManager
->
getDefinition
(
$entity_type_id
);
$t_args
=
array
(
'%entity_type'
=>
$entity_type
->
getLabel
());
switch
(
$change_list
[
'entity_type'
])
{
case
static
::
DEFINITION_CREATED
:
$summary
[
$entity_type_id
][]
=
$this
->
t
(
'Create the %entity_type entity type.'
,
$t_args
);
break
;
case
static
::
DEFINITION_UPDATED
:
$summary
[
$entity_type_id
][]
=
$this
->
t
(
'Update the %entity_type entity type.'
,
$t_args
);
break
;
}
}
// Process field storage definition changes.
if
(
!
empty
(
$change_list
[
'field_storage_definitions'
]))
{
$storage_definitions
=
$this
->
entityManager
->
getFieldStorageDefinitions
(
$entity_type_id
);
...
...
@@ -91,11 +86,6 @@ public function getChangeSummary() {
}
}
}
// Process entity type definition changes.
if
(
!
empty
(
$change_list
[
'entity_type'
])
&&
$change_list
[
'entity_type'
]
==
static
::
DEFINITION_UPDATED
)
{
$entity_type
=
$this
->
entityManager
->
getDefinition
(
$entity_type_id
);
$summary
[
$entity_type_id
][]
=
$this
->
t
(
'Update the %entity_type entity type.'
,
array
(
'%entity_type'
=>
$entity_type
->
getLabel
()));
}
}
return
$summary
;
...
...
@@ -110,10 +100,19 @@ public function applyUpdates() {
// this is necessary when you change an entity type from non-revisionable
// to revisionable and at the same time add revisionable fields to the
// entity type.
if
(
!
empty
(
$change_list
[
'entity_type'
])
&&
$change_list
[
'entity_type'
]
==
static
::
DEFINITION_UPDATED
)
{
if
(
!
empty
(
$change_list
[
'entity_type'
]))
{
$entity_type
=
$this
->
entityManager
->
getDefinition
(
$entity_type_id
);
$original
=
$this
->
entityManager
->
getLastInstalledDefinition
(
$entity_type_id
);
$this
->
entityManager
->
onEntityTypeUpdate
(
$entity_type
,
$original
);
switch
(
$change_list
[
'entity_type'
])
{
case
static
::
DEFINITION_CREATED
:
$this
->
entityManager
->
onEntityTypeCreate
(
$entity_type
);
break
;
case
static
::
DEFINITION_UPDATED
:
$original
=
$this
->
entityManager
->
getLastInstalledDefinition
(
$entity_type_id
);
$this
->
entityManager
->
onEntityTypeUpdate
(
$entity_type
,
$original
);
break
;
}
}
// Process field storage definition changes.
...
...
@@ -160,54 +159,55 @@ protected function getChangeList() {
foreach
(
$this
->
entityManager
->
getDefinitions
()
as
$entity_type_id
=>
$entity_type
)
{
$original
=
$this
->
entityManager
->
getLastInstalledDefinition
(
$entity_type_id
);
// Only manage changes to already installed entity types. Entity type
// installation is handled elsewhere (e.g.,
// \Drupal\Core\Extension\ModuleHandler::install()).
if
(
!
$original
)
{
continue
;
}
// @todo Support non-storage-schema-changing definition updates too:
// https://www.drupal.org/node/2336895.
if
(
$this
->
requiresEntityStorageSchemaChanges
(
$entity_type
,
$original
)
)
{
$change_list
[
$entity_type_id
][
'entity_type'
]
=
static
::
DEFINITION_
UPD
ATED
;
if
(
!
$original
)
{
$change_list
[
$entity_type_id
][
'entity_type'
]
=
static
::
DEFINITION_
CRE
ATED
;
}
else
{
if
(
$this
->
requiresEntityStorageSchemaChanges
(
$entity_type
,
$original
))
{
$change_list
[
$entity_type_id
][
'entity_type'
]
=
static
::
DEFINITION_UPDATED
;
}
if
(
$this
->
entityManager
->
getStorage
(
$entity_type_id
)
instanceof
DynamicallyFieldableEntityStorageInterface
)
{
$field_changes
=
array
();
$storage_definitions
=
$this
->
entityManager
->
getFieldStorageDefinitions
(
$entity_type_id
);
$original_storage_definitions
=
$this
->
entityManager
->
getLastInstalledFieldStorageDefinitions
(
$entity_type_id
);
if
(
$this
->
entityManager
->
getStorage
(
$entity_type_id
)
instanceof
DynamicallyFieldableEntityStorageInterface
)
{
$field_changes
=
array
();
$storage_definitions
=
$this
->
entityManager
->
getFieldStorageDefinitions
(
$entity_type_id
);
$original_storage_definitions
=
$this
->
entityManager
->
getLastInstalledFieldStorageDefinitions
(
$entity_type_id
);
// Detect created field storage definitions.
foreach
(
array_diff_key
(
$storage_definitions
,
$original_storage_definitions
)
as
$field_name
=>
$storage_definition
)
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_CREATED
;
}
// Detect created field storage definitions.
foreach
(
array_diff_key
(
$storage_definitions
,
$original_storage_definitions
)
as
$field_name
=>
$storage_definition
)
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_CREATED
;
}
// Detect deleted field storage definitions.
foreach
(
array_diff_key
(
$original_storage_definitions
,
$storage_definitions
)
as
$field_name
=>
$original_storage_definition
)
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_DELETED
;
}
// Detect deleted field storage definitions.
foreach
(
array_diff_key
(
$original_storage_definitions
,
$storage_definitions
)
as
$field_name
=>
$original_storage_definition
)
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_DELETED
;
}
// Detect updated field storage definitions.
foreach
(
array_intersect_key
(
$storage_definitions
,
$original_storage_definitions
)
as
$field_name
=>
$storage_definition
)
{
// @todo Support non-storage-schema-changing definition updates too:
// https://www.drupal.org/node/2336895. So long as we're checking
// based on schema change requirements rather than definition
// equality, skip the check if the entity type itself needs to be
// updated, since that can affect the schema of all fields, so we
// want to process that update first without reporting false
// positives here.
if
(
!
isset
(
$change_list
[
$entity_type_id
][
'entity_type'
])
&&
$this
->
requiresFieldStorageSchemaChanges
(
$storage_definition
,
$original_storage_definitions
[
$field_name
]))
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_UPDATED
;
// Detect updated field storage definitions.
foreach
(
array_intersect_key
(
$storage_definitions
,
$original_storage_definitions
)
as
$field_name
=>
$storage_definition
)
{
// @todo Support non-storage-schema-changing definition updates too:
// https://www.drupal.org/node/2336895. So long as we're checking
// based on schema change requirements rather than definition
// equality, skip the check if the entity type itself needs to be
// updated, since that can affect the schema of all fields, so we
// want to process that update first without reporting false
// positives here.
if
(
!
isset
(
$change_list
[
$entity_type_id
][
'entity_type'
])
&&
$this
->
requiresFieldStorageSchemaChanges
(
$storage_definition
,
$original_storage_definitions
[
$field_name
]))
{
$field_changes
[
$field_name
]
=
static
::
DEFINITION_UPDATED
;
}
}
}
if
(
$field_changes
)
{
$change_list
[
$entity_type_id
][
'field_storage_definitions'
]
=
$field_changes
;
if
(
$field_changes
)
{
$change_list
[
$entity_type_id
][
'field_storage_definitions'
]
=
$field_changes
;
}
}
}
}
// @todo Support deleting entity definitions when we support base field
// purging. See https://www.drupal.org/node/2282119.
return
array_filter
(
$change_list
);
}
...
...
core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManagerInterface.php
View file @
8007ab1b
...
...
@@ -34,6 +34,27 @@
*/
interface
EntityDefinitionUpdateManagerInterface
{
/**
* Indicates that a definition has just been created.
*
* @var int
*/
const
DEFINITION_CREATED
=
1
;
/**
* Indicates that a definition has changes.
*
* @var int
*/
const
DEFINITION_UPDATED
=
2
;
/**
* Indicates that a definition has just been deleted.
*
* @var int
*/
const
DEFINITION_DELETED
=
3
;
/**
* Checks if there are any definition updates that need to be applied.
*
...
...
core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php
View file @
8007ab1b
...
...
@@ -31,6 +31,8 @@ protected function setUp() {
parent
::
setUp
();
ConfigurableLanguage
::
createFromLangcode
(
'fr'
)
->
save
();
$this
->
config
(
'system.site'
)
->
set
(
'langcode'
,
'fr'
)
->
save
();
// Make sure new entity type definitions are processed.
\
Drupal
::
service
(
'entity.definition_update_manager'
)
->
applyUpdates
();
// Clear all caches so that the base field definition, its cache in the
// entity manager, the t() cache, etc. are all cleared.
drupal_flush_all_caches
();
...
...
core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php
View file @
8007ab1b
...
...
@@ -15,6 +15,15 @@
*/
trait
EntityDefinitionTestTrait
{
/**
* Enables a new entity type definition.
*/
protected
function
enableNewEntityType
()
{
$this
->
state
->
set
(
'entity_test_new'
,
TRUE
);
$this
->
entityManager
->
clearCachedDefinitions
();
$this
->
entityDefinitionUpdateManager
->
applyUpdates
();
}
/**
* Resets the entity type definition.
*/
...
...
core/modules/system/src/Tests/Entity/EntityDefinitionUpdateTest.php
View file @
8007ab1b
...
...
@@ -54,6 +54,26 @@ protected function setUp() {
}
}
/**
* Tests that new entity type definitions are correctly handled.
*/
public
function
testNewEntityType
()
{
$entity_type_id
=
'entity_test_new'
;
$schema
=
$this
->
database
->
schema
();
// Check that the "entity_test_new" is not defined.
$entity_types
=
$this
->
entityManager
->
getDefinitions
();
$this
->
assertFalse
(
isset
(
$entity_types
[
$entity_type_id
]),
'The "entity_test_new" entity type does not exist.'
);
$this
->
assertFalse
(
$schema
->
tableExists
(
$entity_type_id
),
'Schema for the "entity_test_new" entity type does not exist.'
);
// Check that the "entity_test_new" is now defined and the related schema
// has been created.
$this
->
enableNewEntityType
();
$entity_types
=
$this
->
entityManager
->
getDefinitions
();
$this
->
assertTrue
(
isset
(
$entity_types
[
$entity_type_id
]),
'The "entity_test_new" entity type exists.'
);
$this
->
assertTrue
(
$schema
->
tableExists
(
$entity_type_id
),
'Schema for the "entity_test_new" entity type has been created.'
);
}
/**
* Tests when no definition update is needed.
*/
...
...
core/modules/system/src/Tests/Update/UpdateScriptTest.php
View file @
8007ab1b
...
...
@@ -32,6 +32,8 @@ protected function setUp() {
parent
::
setUp
();
$this
->
update_url
=
$GLOBALS
[
'base_url'
]
.
'/update.php'
;
$this
->
update_user
=
$this
->
drupalCreateUser
(
array
(
'administer software updates'
,
'access site in maintenance mode'
));
// Make sure updates for new entity type definitions are processed.
\
Drupal
::
service
(
'entity.definition_update_manager'
)
->
applyUpdates
();
}
/**
...
...
core/modules/system/tests/modules/entity_test/entity_test.module
View file @
8007ab1b
...
...
@@ -74,10 +74,12 @@ function entity_test_entity_types($filter = NULL) {
* Implements hook_entity_type_alter().
*/
function
entity_test_entity_type_alter
(
array
&
$entity_types
)
{
$state
=
\
Drupal
::
state
();
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
foreach
(
entity_test_entity_types
()
as
$entity_type
)
{
// Optionally specify a translation handler for testing translations.
if
(
\
Drupal
::
state
()
->
get
(
'entity_test.translation'
))
{
if
(
$state
->
get
(
'entity_test.translation'
))
{
$translation
=
$entity_types
[
$entity_type
]
->
get
(
'translation'
);
$translation
[
$entity_type
]
=
TRUE
;
$entity_types
[
$entity_type
]
->
set
(
'translation'
,
$translation
);
...
...
@@ -85,7 +87,12 @@ function entity_test_entity_type_alter(array &$entity_types) {
}
// Allow entity_test_update tests to override the entity type definition.
$entity_types
[
'entity_test_update'
]
=
\
Drupal
::
state
()
->
get
(
'entity_test_update.entity_type'
,
$entity_types
[
'entity_test_update'
]);
$entity_types
[
'entity_test_update'
]
=
$state
->
get
(
'entity_test_update.entity_type'
,
$entity_types
[
'entity_test_update'
]);
// Enable the entity_test_new only when needed.
if
(
!
$state
->
get
(
'entity_test_new'
))
{
unset
(
$entity_types
[
'entity_test_new'
]);
}
}
/**
...
...
core/modules/system/tests/modules/entity_test/src/Entity/EntityTestNew.php
0 → 100644
View file @
8007ab1b
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTestNew.
*/
namespace
Drupal\entity_test\Entity
;
/**
* Defines the test entity class for testing definition addition.
*
* This entity type is initially not defined. It is enabled when needed to test
* the related updates.
*
* @ContentEntityType(
* id = "entity_test_new",
* label = @Translation("New test entity"),
* base_table = "entity_test_new",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name",
* "langcode" = "langcode",
* }
* )
*/
class
EntityTestNew
extends
EntityTest
{
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment