diff --git a/core/modules/entity/tests/modules/entity_test/entity_test.install b/core/modules/entity/tests/modules/entity_test/entity_test.install index c0c7703..e61e6c7 100644 --- a/core/modules/entity/tests/modules/entity_test/entity_test.install +++ b/core/modules/entity/tests/modules/entity_test/entity_test.install @@ -43,6 +43,39 @@ function entity_test_schema() { 'not null' => TRUE, 'description' => 'Primary Key: Unique entity-test item ID.', ), + 'langcode' => array( + 'description' => 'The {language}.langcode of the original variant of this test entity.', + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ), + ), + 'primary key' => array('id'), + ); + $schema['entity_test_property_data'] = array( + 'description' => 'Stores entity_test item properties.', + 'fields' => array( + 'id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'The {entity_test}.id of the test entity.', + ), + 'langcode' => array( + 'description' => 'The {language}.langcode of this variant of this test entity.', + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ), + 'source_langcode' => array( + 'description' => 'The {language}.langcode of the original variant of this test entity.', + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'default' => '', + ), 'name' => array( 'description' => 'The name of the test entity.', 'type' => 'varchar', @@ -57,21 +90,15 @@ function entity_test_schema() { 'default' => NULL, 'description' => "The {users}.uid of the associated user.", ), - 'langcode' => array( - 'description' => 'The {language}.langcode of the test entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), ), 'indexes' => array( 'uid' => array('uid'), ), 'foreign keys' => array( 'uid' => array('users' => 'uid'), + 'id' => array('entity_test' => 'id'), ), - 'primary key' => array('id'), + 'primary key' => array('id', 'langcode'), ); return $schema; } diff --git a/core/modules/entity/tests/modules/entity_test/entity_test.module b/core/modules/entity/tests/modules/entity_test/entity_test.module index f97d2e9..2650222 100644 --- a/core/modules/entity/tests/modules/entity_test/entity_test.module +++ b/core/modules/entity/tests/modules/entity_test/entity_test.module @@ -11,8 +11,8 @@ function entity_test_entity_info() { $items['entity_test'] = array( 'label' => t('Test entity'), - 'entity class' => 'Drupal\entity\Entity', - 'controller class' => 'Drupal\entity\DatabaseStorageController', + 'entity class' => 'Drupal\entity_test\EntityTest', + 'controller class' => 'Drupal\entity_test\EntityTestStorageController', 'base table' => 'entity_test', 'fieldable' => TRUE, 'entity keys' => array( @@ -34,7 +34,7 @@ function entity_test_entity_info() { * @param bool $reset * A boolean indicating that the internal cache should be reset. * - * @return Drupal\entity\Entity + * @return Drupal\entity_test\EntityTest * The loaded entity object, or FALSE if the entity cannot be loaded. */ function entity_test_load($id, $reset = FALSE) { diff --git a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php new file mode 100644 index 0000000..dd951e1 --- /dev/null +++ b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTest.php @@ -0,0 +1,111 @@ +propertyLangcode(!empty($values['langcode']) ? $values['langcode'] : NULL); + $langcode = $values['langcode']; + + foreach ($values as $property_name => $value) { + if (!isset($this->properties[$langcode])) { + $this->properties[$langcode] = new \stdClass(); + } + $this->properties[$langcode]->{$property_name} = $value; + } + + if (empty($values['source_langcode'])) { + $this->langcode = $langcode; + parent::__construct($values, $entity_type); + } + else { + $this->entityType = $entity_type; + } + } + + /** + * Overrides EntityInterface::get(). + */ + public function get($property_name, $langcode = NULL) { + $entity_info = $this->entityInfo(); + if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { + return parent::get($property_name, $langcode); + } + else { + $langcode = $this->propertyLangcode($langcode); + return isset($this->properties[$langcode]->{$property_name}) ? $this->properties[$langcode]->{$property_name} : NULL; + } + } + + /** + * Overrides EntityInterface::set(). + */ + public function set($property_name, $value, $langcode = NULL) { + $entity_info = $this->entityInfo(); + if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { + parent::set($property_name, $langcode); + } + else { + $langcode = $this->propertyLangcode($langcode); + $this->properties[$langcode]->{$property_name} = $value; + } + } + + /** + * Overrides EntityInterface::translations(). + */ + public function translations() { + $translations = !empty($this->properties) ? $this->properties : array(); + $languages = array_intersect_key(language_list(), $translations); + unset($languages[$this->propertyLangcode()]); + return $languages + parent::translations(); + } + + /** + * Returns the property array for the given language. + */ + public function getProperties($langcode = NULL) { + $langcode = $this->propertyLangcode($langcode); + return isset($this->properties[$langcode]) ? $this->properties[$langcode] : array(); + } + + /** + * Returns the property array for the given language. + */ + public function setProperties($properties) { + $langcode = $this->propertyLangcode($properties->langcode); + $this->properties[$langcode] = $properties; + } + + /** + * Returns a valid language code to be used to manipulate entity properties. + */ + protected function propertyLangcode($langcode = NULL) { + if (empty($langcode)) { + $langcode = $this->langcode; + } + $languages = language_list(); + return isset($languages[$langcode]) ? $langcode : LANGUAGE_NOT_SPECIFIED; + } +} diff --git a/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php new file mode 100644 index 0000000..ee9a7b3 --- /dev/null +++ b/core/modules/entity/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php @@ -0,0 +1,67 @@ +fields('data') + ->condition('id', array_keys($queried_entities)) + ->orderBy('data.id') + ->execute(); + + foreach ($data as $properties) { + $queried_entities[$properties->id]->setProperties($properties); + } + + parent::attachLoad($queried_entities, $revision_id); + } + + /** + * Overrides Drupal\entity\DatabaseStorageController::postSave(). + */ + protected function postSave(EntityInterface $entity, $update) { + $langcodes = array_keys($entity->translations()); + $langcodes[] = ($language = $entity->language()) ? $language->langcode : LANGUAGE_NOT_SPECIFIED; + + foreach ($langcodes as $langcode) { + $properties = $entity->getProperties($langcode); + $properties->id = $entity->id(); + + if (!empty($properties)) { + db_merge('entity_test_property_data') + ->fields((array) $properties) + ->condition('id', $properties->id) + ->condition('langcode', $properties->langcode) + ->execute(); + } + } + } + + /** + * Overrides Drupal\entity\DatabaseStorageController::postDelete(). + */ + protected function postDelete($entities) { + db_delete('entity_test_property_data') + ->condition('id', array_keys($entities)) + ->execute(); + } +}