Skip to content
Snippets Groups Projects
Commit c16fbc55 authored by catch's avatar catch
Browse files

Issue #2267753 by pwolanin, YesCT, Berdir:...

Issue #2267753 by pwolanin, YesCT, Berdir: ContentEntityDatabaseStorage::mapToStorageRecord  hard-codes $entity->$name->value.
parent 3b9abe1e
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -587,7 +587,7 @@ protected function invokeHook($hook, EntityInterface $entity) {
/**
* Maps from an entity object to the storage record.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity object.
* @param string $table_key
* (optional) The entity key identifying the target table. Defaults to
......@@ -596,10 +596,9 @@ protected function invokeHook($hook, EntityInterface $entity) {
* @return \stdClass
* The record to store.
*/
protected function mapToStorageRecord(EntityInterface $entity, $table_key = 'base_table') {
protected function mapToStorageRecord(ContentEntityInterface $entity, $table_key = 'base_table') {
$record = new \stdClass();
$values = array();
$definitions = $entity->getFieldDefinitions();
$schema = drupal_get_schema($this->entityType->get($table_key));
$is_new = $entity->isNew();
......@@ -611,7 +610,23 @@ protected function mapToStorageRecord(EntityInterface $entity, $table_key = 'bas
$multi_column_fields[$field] = TRUE;
continue;
}
$values[$name] = isset($definitions[$name]) && isset($entity->$name->value) ? $entity->$name->value : NULL;
$values[$name] = NULL;
if ($entity->hasField($name)) {
// Only the first field item is stored.
$field_item = $entity->get($name)->first();
$main_property = $entity->getFieldDefinition($name)->getMainPropertyName();
if ($main_property && isset($field_item->$main_property)) {
// If the field has a main property, store the value of that.
$values[$name] = $field_item->$main_property;
}
elseif (!$main_property) {
// If there is no main property, get all properties from the first
// field item and assume that they will be stored serialized.
// @todo Give field types more control over this behavior in
// https://drupal.org/node/2232427.
$values[$name] = $field_item->getValue();
}
}
}
// Handle fields that store multiple properties and match each property name
......
......@@ -104,4 +104,19 @@ public function __set($name, $value) {
}
}
/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
// A map item has no main property.
return NULL;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->values);
}
}
......@@ -100,15 +100,14 @@ public function setRouteName($route_name) {
* {@inheritdoc}
*/
public function getRouteParams() {
$value = $this->get('route_parameters')->getValue();
return reset($value);
return $this->get('route_parameters')->first()->getValue();
}
/**
* {@inheritdoc}
*/
public function setRouteParams($route_parameters) {
$this->set('route_parameters', array('value' => $route_parameters));
$this->set('route_parameters', array($route_parameters));
return $this;
}
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\shortcut\Tests\ShortcutLinksTest.
* Contains \Drupal\shortcut\Tests\ShortcutLinksTest.
*/
namespace Drupal\shortcut\Tests;
......@@ -66,6 +66,18 @@ public function testShortcutLinkAdd() {
$this->assertTrue(in_array($this->container->get('path.alias_manager')->getPathByAlias($test['path']), $paths), 'Shortcut created: ' . $test['path']);
$this->assertLink($title, 0, 'Shortcut link found on the page.');
}
$saved_set = shortcut_set_load($set->id());
// Test that saving and re-loading a shortcut preserves its values.
$shortcuts = $saved_set->getShortcuts();
foreach ($shortcuts as $entity) {
// Test the node routes with parameters.
if (strpos($entity->route_name->value, 'node.') === 0) {
$entity->save();
$loaded = entity_load('shortcut', $entity->id());
$this->assertEqual($entity->route_name->value, $loaded->route_name->value);
$this->assertEqual($entity->get('route_parameters')->first()->getValue(), $loaded->get('route_parameters')->first()->getValue());
}
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment