Commit 877fd187 authored by alexpott's avatar alexpott

Issue #2346857 by andypost, claudiu.cristea, amateescu, m1r1k, Mile23,...

Issue #2346857 by andypost, claudiu.cristea, amateescu, m1r1k, Mile23, rpayanm, epari.siva, Berdir, yched: Set default property bundle_entity_type in EntityType to NULL
parent 8ba635b3
......@@ -256,19 +256,9 @@ public function calculateDependencies() {
parent::calculateDependencies();
$target_entity_type = $this->entityManager()->getDefinition($this->targetEntityType);
$bundle_entity_type_id = $target_entity_type->getBundleEntityType();
if ($bundle_entity_type_id != 'bundle') {
// If the target entity type uses entities to manage its bundles then
// depend on the bundle entity.
if (!$bundle_entity = $this->entityManager()->getStorage($bundle_entity_type_id)->load($this->bundle)) {
throw new \LogicException("Missing bundle entity, entity type $bundle_entity_type_id, entity id {$this->bundle}.");
}
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
}
else {
// Depend on the provider of the entity type.
$this->addDependency('module', $target_entity_type->getProvider());
}
// Create dependency on the bundle.
$bundle_config_dependency = $target_entity_type->getBundleConfigDependency($this->bundle);
$this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
// If field.module is enabled, add dependencies on 'field_config' entities
// for both displayed and hidden fields. We intentionally leave out base
......
......@@ -119,7 +119,7 @@ class EntityType implements EntityTypeInterface {
*
* @var string
*/
protected $bundle_entity_type = 'bundle';
protected $bundle_entity_type = NULL;
/**
* The name of the entity type for which bundles are provided.
......@@ -764,4 +764,30 @@ public function addConstraint($constraint_name, $options = NULL) {
return $this;
}
/**
* {@inheritdoc}
*/
public function getBundleConfigDependency($bundle) {
// If this entity type uses entities to manage its bundles then depend on
// the bundle entity.
if ($bundle_entity_type_id = $this->getBundleEntityType()) {
if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
}
$config_dependency = [
'type' => 'config',
'name' => $bundle_entity->getConfigDependencyName(),
];
}
else {
// Depend on the provider of the entity type.
$config_dependency = [
'type' => 'module',
'name' => $this->getProvider(),
];
}
return $config_dependency;
}
}
......@@ -732,4 +732,17 @@ public function setConstraints(array $constraints);
*/
public function addConstraint($constraint_name, $options = NULL);
/**
* Gets the config dependency info for this entity, if any exists.
*
* @param string $bundle
* The bundle name.
*
* @return array
* An associative array containing the following keys:
* - 'type': The config dependency type (e.g. 'module', 'config').
* - 'name': The name of the config dependency.
*/
public function getBundleConfigDependency($bundle);
}
......@@ -248,15 +248,10 @@ public function calculateDependencies() {
// @see \Drupal\Core\Field\FieldItemInterface::calculateDependencies()
$this->addDependencies($definition['class']::calculateDependencies($this));
// If the target entity type uses entities to manage its bundles then
// depend on the bundle entity.
$bundle_entity_type_id = $this->entityManager()->getDefinition($this->entity_type)->getBundleEntityType();
if ($bundle_entity_type_id != 'bundle') {
if (!$bundle_entity = $this->entityManager()->getStorage($bundle_entity_type_id)->load($this->bundle)) {
throw new \LogicException("Missing bundle entity, entity type {$bundle_entity_type_id}, entity id {$this->bundle}.");
}
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
}
// Create dependency on the bundle.
$bundle_config_dependency = $this->entityManager()->getDefinition($this->entity_type)->getBundleConfigDependency($this->bundle);
$this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
return $this->dependencies;
}
......
......@@ -32,7 +32,8 @@ class ConfigFieldMapper extends ConfigEntityMapper {
public function getBaseRouteParameters() {
$parameters = parent::getBaseRouteParameters();
$base_entity_info = $this->entityManager->getDefinition($this->pluginDefinition['base_entity_type']);
$parameters[$base_entity_info->getBundleEntityType()] = $this->entity->getTargetBundle();
$bundle_parameter_key = $base_entity_info->getBundleEntityType() ?: 'bundle';
$parameters[$bundle_parameter_key] = $this->entity->getTargetBundle();
return $parameters;
}
......
......@@ -270,7 +270,8 @@ protected function linkTemplates() {
protected function urlRouteParameters($rel) {
$parameters = parent::urlRouteParameters($rel);
$entity_type = \Drupal::entityManager()->getDefinition($this->entity_type);
$parameters[$entity_type->getBundleEntityType()] = $this->bundle;
$bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle';
$parameters[$bundle_parameter_key] = $this->bundle;
return $parameters;
}
......
......@@ -50,7 +50,7 @@ public function testTestItemWithDepenencies() {
$this->assertEqual([
'content' => ['node:article:uuid'],
'config' => ['field.storage.entity_test.field_test'],
'module' => ['field_test', 'test_module']
'module' => ['entity_test', 'field_test', 'test_module']
], $field->getDependencies());
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\Tests\field\Unit;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
......@@ -115,26 +116,10 @@ protected function setUp() {
*/
public function testCalculateDependencies() {
// Mock the interfaces necessary to create a dependency on a bundle entity.
$bundle_entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
$bundle_entity->expects($this->any())
->method('getConfigDependencyName')
->will($this->returnValue('test.test_entity_type.id'));
$storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
$storage->expects($this->any())
->method('load')
->with('test_bundle')
->will($this->returnValue($bundle_entity));
$this->entityManager->expects($this->any())
->method('getStorage')
->with('bundle_entity_type')
->will($this->returnValue($storage));
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$target_entity_type->expects($this->any())
->method('getBundleEntityType')
->will($this->returnValue('bundle_entity_type'));
->method('getBundleConfigDependency')
->will($this->returnValue(array('type' => 'config', 'name' => 'test.test_entity_type.id')));
$this->entityManager->expects($this->at(0))
->method('getDefinition')
......@@ -192,10 +177,10 @@ public function testCalculateDependenciesIncorrectBundle() {
->with('bundle_entity_type')
->will($this->returnValue($storage));
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$target_entity_type->expects($this->any())
->method('getBundleEntityType')
->will($this->returnValue('bundle_entity_type'));
$target_entity_type = new EntityType(array(
'id' => 'test_entity_type',
'bundle_entity_type' => 'bundle_entity_type',
));
$this->entityManager->expects($this->at(0))
->method('getDefinition')
......
......@@ -124,11 +124,9 @@ public function buildHeader() {
public function buildRow(EntityInterface $field_config) {
/** @var \Drupal\field\FieldConfigInterface $field_config */
$field_storage = $field_config->getFieldStorageDefinition();
$target_bundle_entity_type_id = $this->entityManager->getDefinition($this->targetEntityTypeId)->getBundleEntityType();
$route_parameters = array(
$target_bundle_entity_type_id => $this->targetBundle,
'field_config' => $field_config->id(),
);
) + FieldUI::getRouteBundleParameter($this->entityManager->getDefinition($this->targetEntityTypeId), $this->targetBundle);
$row = array(
'id' => Html::getClass($field_config->getName()),
......
......@@ -81,7 +81,8 @@ public static function getNextDestination(array $destinations) {
* An array that can be used a route parameter.
*/
public static function getRouteBundleParameter(EntityTypeInterface $entity_type, $bundle) {
return array($entity_type->getBundleEntityType() => $bundle);
$bundle_parameter_key = $entity_type->getBundleEntityType() ?: 'bundle';
return array($bundle_parameter_key => $bundle);
}
}
......@@ -48,14 +48,13 @@ protected function alterRoutes(RouteCollection $collection) {
$path = $entity_route->getPath();
$options = array();
if (($bundle_entity_type = $entity_type->getBundleEntityType()) && $bundle_entity_type !== 'bundle') {
if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
$options['parameters'][$bundle_entity_type] = array(
'type' => 'entity:' . $bundle_entity_type,
);
// Special parameter used to easily recognize all Field UI routes.
$options['_field_ui'] = TRUE;
}
// Special parameter used to easily recognize all Field UI routes.
$options['_field_ui'] = TRUE;
$defaults = array(
'entity_type_id' => $entity_type_id,
......
......@@ -194,15 +194,12 @@ public static function loadByEntityTypeBundle($entity_type_id, $bundle) {
*/
public function calculateDependencies() {
parent::calculateDependencies();
$bundle_entity_type_id = $this->entityManager()->getDefinition($this->target_entity_type_id)->getBundleEntityType();
if ($bundle_entity_type_id != 'bundle') {
// If the target entity type uses entities to manage its bundles then
// depend on the bundle entity.
if (!$bundle_entity = $this->entityManager()->getStorage($bundle_entity_type_id)->load($this->target_bundle)) {
throw new \LogicException("Missing bundle entity, entity type $bundle_entity_type_id, entity id {$this->target_bundle}.");
}
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
}
// Create dependency on the bundle.
$entity_type = \Drupal::entityManager()->getDefinition($this->target_entity_type_id);
$bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle);
$this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
return $this->dependencies;
}
......
......@@ -88,26 +88,10 @@ protected function setUp() {
*/
public function testCalculateDependencies() {
// Mock the interfaces necessary to create a dependency on a bundle entity.
$bundle_entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
$bundle_entity->expects($this->any())
->method('getConfigDependencyName')
->will($this->returnValue('test.test_entity_type.id'));
$storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
$storage->expects($this->any())
->method('load')
->with('test_bundle')
->will($this->returnValue($bundle_entity));
$this->entityManager->expects($this->any())
->method('getStorage')
->with('bundle_entity_type')
->will($this->returnValue($storage));
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$target_entity_type->expects($this->any())
->method('getBundleEntityType')
->will($this->returnValue('bundle_entity_type'));
->method('getBundleConfigDependency')
->will($this->returnValue(array('type' => 'config', 'name' => 'test.test_entity_type.id')));
$this->entityManager->expects($this->any())
->method('getDefinition')
......
......@@ -141,15 +141,13 @@ public function id() {
*/
public function calculateDependencies() {
parent::calculateDependencies();
// Create dependency on the bundle.
$entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType);
$this->addDependency('module', $entity_type->getProvider());
$bundle_entity_type_id = $entity_type->getBundleEntityType();
if ($bundle_entity_type_id != 'bundle') {
// If the target entity type uses entities to manage its bundles then
// depend on the bundle entity.
$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($this->bundle);
$this->addDependency('config', $bundle_entity->getConfigDependencyName());
}
$bundle_config_dependency = $entity_type->getBundleConfigDependency($this->bundle);
$this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
return $this->dependencies;
}
......
......@@ -80,7 +80,7 @@ public function testCalculateDependencies() {
$values = array('targetEntityType' => $target_entity_type_id);
$target_entity_type->expects($this->any())
->method('getBundleEntityType')
->will($this->returnValue('bundle'));
->will($this->returnValue(NULL));
$this->entityManager->expects($this->at(0))
->method('getDefinition')
......@@ -109,16 +109,9 @@ public function testCalculateDependenciesWithEntityBundle() {
$bundle_id = $this->randomMachineName(10);
$values = array('targetEntityType' => $target_entity_type_id , 'bundle' => $bundle_id);
$bundle_entity_type_id = $this->randomMachineName(17);
$bundle_entity = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityInterface');
$bundle_entity
->expects($this->once())
->method('getConfigDependencyName')
->will($this->returnValue('test_module.type.' . $bundle_id));
$target_entity_type->expects($this->any())
->method('getBundleEntityType')
->will($this->returnValue($bundle_entity_type_id));
->method('getBundleConfigDependency')
->will($this->returnValue(array('type' => 'config', 'name' => 'test_module.type.' . $bundle_id)));
$this->entityManager->expects($this->at(0))
->method('getDefinition')
......@@ -129,17 +122,6 @@ public function testCalculateDependenciesWithEntityBundle() {
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
$storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$storage->expects($this->once())
->method('load')
->with($bundle_id)
->will($this->returnValue($bundle_entity));
$this->entityManager->expects($this->once())
->method('getStorage')
->with($bundle_entity_type_id)
->will($this->returnValue($storage));
$entity = new RdfMapping($values, $this->entityTypeId);
$dependencies = $entity->calculateDependencies();
$this->assertContains('test_module.type.' . $bundle_id, $dependencies['config']);
......
......@@ -509,13 +509,12 @@ public function testReferencedEntity() {
}
$bundle_entity_type = $this->entity->getEntityType()->getBundleEntityType();
if ($bundle_entity_type !== 'bundle') {
if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
// Verify that after modifying the corresponding bundle entity, there is a
// cache miss for both the referencing entity, and the listing of
// referencing entities, but not for any other routes.
$this->pass("Test modification of referenced entity's bundle entity.", 'Debug');
$bundle_entity = entity_load($bundle_entity_type, $this->entity->bundle());
$bundle_entity = entity_load($bundle_entity_type_id, $this->entity->bundle());
$bundle_entity->save();
$this->verifyPageCache($referencing_entity_url, 'MISS');
$this->verifyPageCache($listing_url, 'MISS');
......
......@@ -81,12 +81,11 @@ public function testEntityUri() {
$this->verifyPageCache($entity_url, 'HIT');
$bundle_entity_type = $this->entity->getEntityType()->getBundleEntityType();
if ($bundle_entity_type !== 'bundle') {
if ($bundle_entity_type_id = $this->entity->getEntityType()->getBundleEntityType()) {
// Verify that after modifying the corresponding bundle entity, there is a
// cache miss.
$this->pass("Test modification of entity's bundle entity.", 'Debug');
$bundle_entity = entity_load($bundle_entity_type, $this->entity->bundle());
$bundle_entity = entity_load($bundle_entity_type_id, $this->entity->bundle());
$bundle_entity->save();
$this->verifyPageCache($entity_url, 'MISS');
......
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