Commit b31bbb08 authored by xjm's avatar xjm

Issue #2204697 by alexpott, tstoeckler, mauzeh, Berdir: Move getConfigPrefix()...

Issue #2204697 by alexpott, tstoeckler, mauzeh, Berdir: Move getConfigPrefix() to ConfigEntityTypeInterface
parent 4acf0c95
......@@ -11,6 +11,7 @@
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\Entity\ConfigDependencyManager;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -102,7 +103,7 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor
*/
public function getEntityTypeIdByName($name) {
$entities = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) {
return ($config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0;
return ($entity_type instanceof ConfigEntityTypeInterface && $config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0;
});
return key($entities);
}
......
......@@ -15,44 +15,17 @@
/**
* Provides an implementation of a configuration entity type and its metadata.
*/
class ConfigEntityType extends EntityType {
class ConfigEntityType extends EntityType implements ConfigEntityTypeInterface {
/**
* Length limit of the configuration entity prefix.
* The config prefix set in the configuration entity type annotation.
*
* Configuration entity names are composed of two parts:
* - The config prefix, which is returned by getConfigPrefix() and is
* composed of:
* - The provider module name (limited to 50 characters by
* DRUPAL_EXTENSION_NAME_MAX_LENGTH).
* - The module-specific namespace identifier, which defaults to the
* configuration entity type ID. Entity type IDs are limited to 32
* characters by EntityTypeInterface::ID_MAX_LENGTH.
* - The configuration entity ID.
* So, a typical configuration entity filename will look something like:
* provider_module_name.namespace_identifier.config_entity_id.yml
* The default configuration prefix is constructed from the name of the module
* that provides the entity type and the ID of the entity type. If a
* config_prefix annotation is present it will be used in place of the entity
* type ID.
*
* Most file systems limit a file name's length to 255 characters, so
* ConfigBase::MAX_NAME_LENGTH restricts the full configuration object name
* to 250 characters (leaving 5 for the file extension). Therefore, in
* order to leave sufficient characters to construct a configuration ID,
* the configuration entity prefix is limited to 83 characters: up to 50
* characters for the module name, 1 for the dot, and 32 for the namespace
* identifier. This also allows modules with shorter names to define longer
* namespace identifiers if desired.
*
* @see \Drupal\Core\Config\ConfigBase::MAX_NAME_LENGTH
* @see \Drupal\Core\Config\Entity\ConfigEntityTypeInterface::getConfigPrefix()
* @see DRUPAL_EXTENSION_NAME_MAX_LENGTH
* @see \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH
* @see \Drupal\Core\Entity\EntityTypeInterface::ID_MAX_LENGTH
*/
const PREFIX_LENGTH = 83;
/**
* Returns the config prefix used by the configuration entity type.
*
* @var string
* @see \Drupal\Core\Config\Entity\ConfigEntityType::getConfigPrefix()
*/
protected $config_prefix;
......
<?php
/**
* @file
* Contains \Drupal\Core\Config\Entity\ConfigEntityTypeInterface.
*/
namespace Drupal\Core\Config\Entity;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Provides an interface for a configuration entity type and its metadata.
*/
interface ConfigEntityTypeInterface extends EntityTypeInterface {
/**
* Length limit of the configuration entity prefix.
*
* Configuration entity names are composed of two parts:
* - The config prefix, which is returned by getConfigPrefix() and is
* composed of:
* - The provider module name (limited to 50 characters by
* DRUPAL_EXTENSION_NAME_MAX_LENGTH).
* - The module-specific namespace identifier, which defaults to the
* configuration entity type ID. Entity type IDs are limited to 32
* characters by EntityTypeInterface::ID_MAX_LENGTH.
* - The configuration entity ID.
* So, a typical configuration entity filename will look something like:
* provider_module_name.namespace_identifier.config_entity_id.yml
*
* Most file systems limit a file name's length to 255 characters, so
* ConfigBase::MAX_NAME_LENGTH restricts the full configuration object name
* to 250 characters (leaving 5 for the file extension). Therefore, in
* order to leave sufficient characters to construct a configuration ID,
* the configuration entity prefix is limited to 83 characters: up to 50
* characters for the module name, 1 for the dot, and 32 for the namespace
* identifier. This also allows modules with shorter names to define longer
* namespace identifiers if desired.
*
* @see \Drupal\Core\Config\ConfigBase::MAX_NAME_LENGTH
* @see \Drupal\Core\Config\Entity\ConfigEntityTypeInterface::getConfigPrefix()
* @see DRUPAL_EXTENSION_NAME_MAX_LENGTH
* @see \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH
* @see \Drupal\Core\Entity\EntityTypeInterface::ID_MAX_LENGTH
*/
const PREFIX_LENGTH = 83;
/**
* Gets the config prefix used by the configuration entity type.
*
* Ensures that all configuration entities are prefixed by the module that
* provides the configuration entity type. This ensures that if a
* configuration entity is contained in a extension's default configuration,
* it will be created during extension installation. Additionally, it allows
* dependencies to be calculated without the modules that provide
* configuration entity types being installed.
*
* @return string|bool
* The config prefix, or FALSE if not a configuration entity type.
*/
public function getConfigPrefix();
}
......@@ -23,13 +23,6 @@ public function __construct($definition) {
);
}
/**
* {@inheritdoc}
*/
public function getConfigPrefix() {
return FALSE;
}
/**
* {@inheritdoc}
*/
......
......@@ -651,13 +651,6 @@ public function isRevisionable() {
return $this->hasKey('revision');
}
/**
* {@inheritdoc}
*/
public function getConfigPrefix() {
return FALSE;
}
/**
* {@inheritdoc}
*/
......
......@@ -577,16 +577,6 @@ public function isTranslatable();
*/
public function isRevisionable();
/**
* Returns the config prefix used by the configuration entity type.
*
* @todo Used for configuration entities only.
*
* @return string|bool
* The config prefix, or FALSE if not a configuration entity type.
*/
public function getConfigPrefix();
/**
* Returns the name of the entity's revision data table.
*
......
......@@ -108,7 +108,7 @@ public function testSetEntity() {
->with()
->will($this->returnValue('entity_id'));
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityManager
->expects($this->once())
->method('getDefinition')
......@@ -127,7 +127,7 @@ public function testSetEntity() {
* Tests ConfigEntityMapper::getOverviewRouteParameters().
*/
public function testGetOverviewRouteParameters() {
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityManager
->expects($this->once())
->method('getDefinition')
......@@ -158,7 +158,7 @@ public function testGetType() {
* Tests ConfigEntityMapper::getTypeName().
*/
public function testGetTypeName() {
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$entity_type->expects($this->once())
->method('getLabel')
->will($this->returnValue('test'));
......@@ -176,7 +176,7 @@ public function testGetTypeName() {
* Tests ConfigEntityMapper::getTypeLabel().
*/
public function testGetTypeLabel() {
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$entity_type->expects($this->once())
->method('getLabel')
->will($this->returnValue('test'));
......
......@@ -73,7 +73,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
*/
protected function setUp() {
$this->entityTypeId = $this->randomMachineName();
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
......
......@@ -100,7 +100,7 @@ protected function setUp() {
);
$this->entityTypeId = $this->randomMachineName();
$this->provider = $this->randomMachineName();
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityType->expects($this->any())
->method('getProvider')
->will($this->returnValue($this->provider));
......
......@@ -111,7 +111,7 @@ class ConfigEntityStorageTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$this->entityType = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$this->entityType = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityTypeId = 'test_entity_type';
$this->entityType->expects($this->any())
->method('getKey')
......
......@@ -23,7 +23,7 @@ class ConfigEntityTypeTest extends UnitTestCase {
* @param array $definition
* An array of values to use for the ConfigEntityType.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
*/
protected function setUpConfigEntityType($definition) {
if (!isset($definition['id'])) {
......@@ -109,4 +109,26 @@ public function testSetStorageClass() {
$config_entity->setStorageClass('\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage');
}
/**
* Tests the getConfigPrefix() method.
*
* @dataProvider providerTestGetConfigPrefix
*
* @covers ::getConfigPrefix
*/
public function testGetConfigPrefix($definition, $expected) {
$entity_type = $this->setUpConfigEntityType($definition);
$this->assertSame($expected, $entity_type->getConfigPrefix());
}
/**
* Provides test data.
*/
public function providerTestGetConfigPrefix() {
return array(
array(array('provider' => 'node', 'id' => 'node_type', 'config_prefix' => 'type'), 'node.type'),
array(array('provider' => 'views', 'id' => 'view'), 'views.view'),
);
}
}
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