Commit 5fdcfc74 authored by alexpott's avatar alexpott

Issue #2346911 by larowlan, vijaycs85, Berdir | sihv: Fixed Base field...

Issue #2346911 by larowlan, vijaycs85, Berdir | sihv: Fixed Base field overrides have incorrect default value callable properties.
parent 96cde6e3
......@@ -364,9 +364,9 @@ field_config_base:
label: 'Translatable'
default_value:
type: field.[%parent.field_type].value
default_value_function:
default_value_callback:
type: string
label: 'Default value function'
label: 'Default value callback'
settings:
type: field.[%parent.field_type].field_settings
third_party_settings:
......
......@@ -397,7 +397,7 @@ public function getDefaultValue(ContentEntityInterface $entity) {
*
* If set, the callback overrides any set default value.
*
* @param callable|null $callback
* @param string|null $callback
* The callback to invoke for getting the default value (pass NULL to unset
* a previously set callback). The callback will be invoked with the
* following arguments:
......@@ -411,6 +411,9 @@ public function getDefaultValue(ContentEntityInterface $entity) {
* @return $this
*/
public function setDefaultValueCallback($callback) {
if (isset($callback) && !is_string($callback)) {
throw new \InvalidArgumentException('Default value callback must be a string, like "function_name" or "ClassName::methodName"');
}
$this->definition['default_value_callback'] = $callback;
return $this;
}
......
......@@ -135,7 +135,7 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn
* hook_field_schema(). If the number of items exceeds the cardinality of the
* field, extraneous items will be ignored.
*
* This property is overlooked if the $default_value_function is non-empty.
* This property is overlooked if the $default_value_callback is non-empty.
*
* Example for a integer field:
* @code
......@@ -165,7 +165,7 @@ abstract class FieldConfigBase extends ConfigEntityBase implements FieldConfigIn
*
* @var string
*/
public $default_value_function = '';
public $default_value_callback = '';
/**
* The field storage object.
......@@ -334,8 +334,8 @@ public function isRequired() {
*/
public function getDefaultValue(ContentEntityInterface $entity) {
// Allow custom default values function.
if ($function = $this->default_value_function) {
$value = call_user_func($function, $entity, $this);
if ($callback = $this->default_value_callback) {
$value = call_user_func($callback, $entity, $this);
}
else {
$value = $this->default_value;
......
......@@ -318,7 +318,7 @@ public function getConstraints() {
* {@inheritdoc}
*/
public function defaultValuesForm(array &$form, FormStateInterface $form_state) {
if (empty($this->getFieldDefinition()->default_value_function)) {
if (empty($this->getFieldDefinition()->default_value_callback)) {
// Place the input in a separate place in the submitted values tree.
$widget = $this->defaultValueWidget($form_state);
......
......@@ -15,6 +15,6 @@ translatable: true
default_value:
-
value: 0
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: boolean
......@@ -32,7 +32,7 @@ class DateTimeFieldItemList extends FieldItemList {
* {@inheritdoc}
*/
public function defaultValuesForm(array &$form, FormStateInterface $form_state) {
if (empty($this->getFieldDefinition()->default_value_function)) {
if (empty($this->getFieldDefinition()->default_value_callback)) {
$default_value = $this->getFieldDefinition()->default_value;
$element = array(
......
......@@ -194,7 +194,7 @@ function testFieldAttachSaveEmptyDataDefaultValue() {
$this->createFieldWithStorage('', $entity_type);
// Add a default value function.
$this->fieldTestData->field->default_value_function = 'field_test_default_value';
$this->fieldTestData->field->default_value_callback = 'field_test_default_value';
$this->fieldTestData->field->save();
// Verify that fields are populated with default values.
......
......@@ -7,7 +7,7 @@ label: 'Test import field'
description: ''
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -7,7 +7,7 @@ label: 'Test import field 2 on entity_test bundle'
description: ''
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -7,7 +7,7 @@ label: 'Test import field 2 on test bundle'
description: ''
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -8,7 +8,7 @@ label: 'Import from staging'
description: ''
required: '0'
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -8,7 +8,7 @@ label: 'Test import field 2 on test bundle'
description: ''
required: '0'
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -8,7 +8,7 @@ label: 'Test import field 2 on test bundle 2'
description: ''
required: '0'
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: text
dependencies:
......
......@@ -159,7 +159,7 @@ public function testToArray() {
'description' => '',
'required' => FALSE,
'default_value' => array(),
'default_value_function' => '',
'default_value_callback' => '',
'settings' => array(),
'dependencies' => array(),
'field_type' => 'test_field',
......
......@@ -15,6 +15,6 @@ translatable: true
default_value:
-
value: 0
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: boolean
......@@ -13,6 +13,6 @@ description: 'The title of this node, always treated as non-markup plain text.'
required: true
translatable: true
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: string
......@@ -10,7 +10,7 @@ required: true
default_value:
-
value: 0
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: boolean
dependencies:
......
......@@ -36,7 +36,7 @@ process:
- widget_settings
- global_settings
default_value_function: ''
default_value_callback: ''
default_value:
plugin: d6_field_instance_defaults
source:
......
......@@ -376,7 +376,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback(array('Drupal\node\Entity\Node', 'getCurrentUserId'))
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
......
<?php
/**
* @file
* Contains \Drupal\node\Tests\NodeFieldOverridesTest.
*/
namespace Drupal\node\Tests;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
/**
* Tests node field overrides.
*
* @group node
*/
class NodeFieldOverridesTest extends EntityUnitTestBase {
/**
* Current logged in user.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'system', 'field', 'node');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('node');
$this->installConfig(array('user'));
$this->user = $this->createUser();
\Drupal::service('current_user')->setAccount($this->user);
}
/**
* Tests that field overrides work as expected.
*/
public function testFieldOverrides() {
if (!NodeType::load('ponies')) {
NodeType::create(['name' => 'Ponies', 'type' => 'ponies'])->save();
}
$override = BaseFieldOverride::loadByName('node', 'ponies', 'uid');
if ($override) {
$override->delete();
}
$uid_field = \Drupal::entityManager()->getBaseFieldDefinitions('node')['uid'];
$config = $uid_field->getConfig('ponies');
$config->save();
$this->assertEqual($config->get('default_value_callback'), 'Drupal\node\Entity\Node::getCurrentUserId');
/** @var \Drupal\node\NodeInterface $node */
$node = Node::create(['type' => 'ponies']);
$owner = $node->getOwner();
$this->assertTrue($owner instanceof \Drupal\user\UserInterface);
$this->assertEqual($owner->id(), $this->user->id());
}
}
......@@ -438,7 +438,7 @@ function testEntityTranslationAPI() {
$entity = $this->reloadEntity($entity);
$field_id = implode('.', array($entity->getEntityTypeId(), $entity->bundle(), $this->field_name));
$field = $this->entityManager->getStorage('field_config')->load($field_id);
$field->default_value_function = 'entity_test_field_default_value';
$field->default_value_callback = 'entity_test_field_default_value';
$field->save();
$translation = $entity->addTranslation($langcode2);
$field_storage = $translation->get($this->field_name);
......
......@@ -15,6 +15,6 @@ translatable: true
default_value:
-
value: 0
default_value_function: ''
default_value_callback: ''
settings: { }
field_type: boolean
......@@ -6,7 +6,7 @@ label: Image
description: ''
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings:
file_directory: field/image
file_extensions: 'png gif jpg jpeg'
......
......@@ -7,7 +7,7 @@ label: Tags
description: 'Enter a comma-separated list. For example: Amsterdam, Mexico City, "Cleveland, Ohio"'
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings: { }
status: true
langcode: und
......
......@@ -8,7 +8,7 @@ label: Picture
description: 'Your virtual face or picture.'
required: false
default_value: { }
default_value_function: ''
default_value_callback: ''
settings:
file_extensions: 'png gif jpg jpeg'
file_directory: pictures
......
......@@ -16,6 +16,7 @@
* Unit test for BaseFieldDefinition.
*
* @group Entity
* @coversDefaultClass \Drupal\Core\Field\BaseFieldDefinition
*/
class BaseFieldDefinitionTest extends UnitTestCase {
......@@ -75,6 +76,8 @@ protected function setUp() {
/**
* Tests field name methods.
*
* @covers ::getName()
*/
public function testFieldName() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -85,6 +88,8 @@ public function testFieldName() {
/**
* Tests field label methods.
*
* @covers ::getLabel()
*/
public function testFieldLabel() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -95,6 +100,8 @@ public function testFieldLabel() {
/**
* Tests field description methods.
*
* @covers ::getDescription()
*/
public function testFieldDescription() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -105,6 +112,8 @@ public function testFieldDescription() {
/**
* Tests field type methods.
*
* @covers ::getType()
*/
public function testFieldType() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -113,6 +122,10 @@ public function testFieldType() {
/**
* Tests field settings methods.
*
* @covers ::getSetting()
* @covers ::setSetting()
* @covers ::getSettings()
*/
public function testFieldSettings() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -126,6 +139,10 @@ public function testFieldSettings() {
/**
* Tests the initialization of default field settings.
*
* @covers ::getSetting()
* @covers ::setSetting()
* @covers ::getSettings()
*/
public function testDefaultFieldSettings() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -138,6 +155,9 @@ public function testDefaultFieldSettings() {
/**
* Tests field default value.
*
* @covers ::getDefaultValue()
* @covers ::setDefaultValue()
*/
public function testFieldDefaultValue() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -154,6 +174,9 @@ public function testFieldDefaultValue() {
/**
* Tests field translatable methods.
*
* @covers ::isTranslatable()
* @covers ::setTranslatable()
*/
public function testFieldTranslatable() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -166,6 +189,9 @@ public function testFieldTranslatable() {
/**
* Tests field revisionable methods.
*
* @covers ::isRevisionable()
* @covers ::setRevisionable()
*/
public function testFieldRevisionable() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -178,6 +204,9 @@ public function testFieldRevisionable() {
/**
* Tests field cardinality.
*
* @covers ::getCardinality()
* @covers ::setCardinality()
*/
public function testFieldCardinality() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -190,6 +219,9 @@ public function testFieldCardinality() {
/**
* Tests required.
*
* @covers ::isRequired()
* @covers ::setRequired()
*/
public function testFieldRequired() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -202,6 +234,9 @@ public function testFieldRequired() {
/**
* Tests provider.
*
* @covers ::getProvider()
* @covers ::setProvider()
*/
public function testFieldProvider() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -212,6 +247,9 @@ public function testFieldProvider() {
/**
* Tests custom storage.
*
* @covers ::hasCustomStorage()
* @covers ::setCustomStorage()
*/
public function testCustomStorage() {
$definition = BaseFieldDefinition::create($this->fieldType);
......@@ -222,4 +260,51 @@ public function testCustomStorage() {
$this->assertFalse($definition->hasCustomStorage());
}
/**
* Tests default value callbacks.
*
* @covers ::setDefaultValueCallback()
*/
public function testDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$callback = get_class($this) . '::mockDefaultValueCallback';
$definition->setDefaultValueCallback($callback);
}
/**
* Tests invalid default value callbacks.
*
* @covers ::setDefaultValueCallback()
* @expectedException \InvalidArgumentException
*/
public function testInvalidDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$definition->setDefaultValueCallback([get_class($this), 'mockDefaultValueCallback']);
}
/**
* Tests NULL default value callbacks.
*
* @covers ::setDefaultValueCallback
*/
public function testNullDefaultValueCallback() {
$definition = BaseFieldDefinition::create($this->fieldType);
$definition->setDefaultValueCallback(NULL);
}
/**
* Provides a Mock base field default value callback.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity interface.
* @param \Drupal\Core\Field\FieldDefinitionInterface $definition
* Field definition.
*
* @return string
* Default value.
*/
public static function mockDefaultValueCallback($entity, $definition) {
return 'a default value';
}
}
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