Commit c2cbb6fb authored by catch's avatar catch
Browse files

Issue #3067609 by quietone, alexpott, justafish, sheanhoxie, flocondetoile,...

Issue #3067609 by quietone, alexpott, justafish, sheanhoxie, flocondetoile, heddn: Fix config schema for links and migration of link default values
parent ddfacbf0
......@@ -54,7 +54,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
case 'link':
if (!empty($widget_settings['default_value'][0]['url'])) {
$default['title'] = $widget_settings['default_value'][0]['title'];
$default['url'] = $widget_settings['default_value'][0]['url'];
$default['uri'] = $widget_settings['default_value'][0]['url'];
$default['options'] = ['attributes' => []];
}
break;
......
......@@ -28,6 +28,11 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
unset($default_value[0]['email']);
}
if ($widget_type == 'link_field' && $default_value) {
$default_value[0]['uri'] = $default_value[0]['url'];
$default_value[0]['options'] = ['attributes' => []];
unset($default_value[0]['url']);
}
return $default_value;
}
......
......@@ -101,7 +101,7 @@ public function testFieldInstanceMigration() {
$expected = ['title' => 2, 'link_type' => LinkItemInterface::LINK_GENERIC];
$this->assertSame($expected, $field->getSettings());
$this->assertSame('default link title', $entity->field_test_link->title, 'Field field_test_link default title is correct.');
$this->assertSame('https://www.drupal.org', $entity->field_test_link->url, 'Field field_test_link default title is correct.');
$this->assertSame('https://www.drupal.org', $entity->field_test_link->uri);
$this->assertSame([], $entity->field_test_link->options['attributes']);
// Test date field.
......
......@@ -54,12 +54,19 @@ field.value.link:
type: mapping
label: 'Default value'
mapping:
attributes:
type: mapping
label: 'Link attributes'
mapping:
title:
type: label
label: 'Link text'
uri:
type: string
label: 'URL'
title:
type: label
label: 'Link text'
url:
type: string
label: 'URL'
options:
type: mapping
label: 'Link options'
......
......@@ -5,8 +5,6 @@
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
......@@ -47,18 +45,11 @@ class LinkFieldUITest extends BrowserTestBase {
protected $helpTextUser;
/**
* The first content type to add fields to.
* The content type to add fields to.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $firstContentType;
/**
* The second content type to add fields to.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $secondContentType;
protected $contentType;
/**
* {@inheritdoc}
......@@ -66,15 +57,14 @@ class LinkFieldUITest extends BrowserTestBase {
protected function setUp(): void {
parent::setUp();
$this->firstContentType = $this->drupalCreateContentType();
$this->secondContentType = $this->drupalCreateContentType();
$this->contentType = $this->drupalCreateContentType();
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
]);
$this->helpTextUser = $this->drupalCreateUser([
'create ' . $this->secondContentType->id() . ' content',
'create ' . $this->contentType->id() . ' content',
]);
$this->drupalPlaceBlock('system_breadcrumb_block');
}
......@@ -84,8 +74,8 @@ protected function setUp(): void {
*/
public function testFieldUI() {
foreach ($this->providerTestFieldUI() as $item) {
list($cardinality, $link_type, $title, $label, $field_name) = $item;
$this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name);
list($cardinality, $link_type, $title, $label, $field_name, $default_uri) = $item;
$this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri);
}
}
......@@ -104,14 +94,14 @@ protected function providerTestFieldUI() {
DRUPAL_OPTIONAL,
];
$link_types = [
LinkItemInterface::LINK_EXTERNAL,
LinkItemInterface::LINK_GENERIC,
LinkItemInterface::LINK_INTERNAL,
LinkItemInterface::LINK_EXTERNAL => 'http://drupal.org',
LinkItemInterface::LINK_GENERIC => '',
LinkItemInterface::LINK_INTERNAL => '<front>',
];
// Test all variations of link types on all cardinalities.
foreach ($cardinalities as $cardinality) {
foreach ($link_types as $link_type) {
foreach ($link_types as $link_type => $default_uri) {
// Now, test this with both the title enabled and disabled.
foreach ($title_settings as $title_setting) {
// Test both empty and non-empty labels.
......@@ -135,6 +125,7 @@ protected function providerTestFieldUI() {
$title_setting,
$label_provided ? $label : '',
$id,
$default_uri,
];
}
}
......@@ -155,19 +146,27 @@ protected function providerTestFieldUI() {
* The field label.
* @param string $field_name
* The unique machine name for the field.
* @param string $default_uri
* The default URI value.
*/
public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) {
public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri) {
$this->drupalLogin($this->adminUser);
$type_path = 'admin/structure/types/manage/' . $this->firstContentType->id();
$type_path = 'admin/structure/types/manage/' . $this->contentType->id();
// Add a link field to the newly-created type. It defaults to allowing both
// internal and external links.
$field_label = str_replace('_', ' ', $field_name);
// Add a link field to the newly-created type.
$description = 'link field description';
$field_edit = [
'description' => $description,
'settings[link_type]' => (int) $link_type,
];
$this->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit);
if (!empty($default_uri)) {
$field_edit['default_value_input[field_' . $field_name . '][0][uri]'] = $default_uri;
$field_edit['default_value_input[field_' . $field_name . '][0][title]'] = 'Default title';
}
$storage_edit = [
'cardinality_number' => $cardinality,
];
$this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit);
// Load the formatter page to check that the settings summary does not
// generate warnings.
......@@ -175,26 +174,8 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_
$this->drupalGet("$type_path/display");
$this->assertText('Link text trimmed to 80 characters');
$storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'link',
'cardinality' => $cardinality,
]);
$storage->save();
FieldConfig::create([
'field_storage' => $storage,
'label' => $label,
'bundle' => $this->secondContentType->id(),
'settings' => [
'title' => $title,
'link_type' => $link_type,
],
])->save();
// Make the fields visible in the form display.
$form_display_id = implode('.', ['node', $this->secondContentType->id(), 'default']);
$form_display_id = implode('.', ['node', $this->contentType->id(), 'default']);
$form_display = EntityFormDisplay::load($form_display_id);
$form_display->setComponent($field_name, ['region' => 'content']);
$form_display->save();
......@@ -203,7 +184,7 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_
// the user can see the expected help text.
$this->drupalLogin($this->helpTextUser);
$add_path = 'node/add/' . $this->secondContentType->id();
$add_path = 'node/add/' . $this->contentType->id();
$this->drupalGet($add_path);
$expected_help_texts = [
......@@ -225,6 +206,9 @@ public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_
if (!empty($label)) {
$this->assertFieldContainsRawText($field_name, $label);
}
// Test the default field value is used as expected.
$this->assertSession()->fieldValueEquals('field_' . $field_name . '[0][uri]', $default_uri);
}
/**
......@@ -261,7 +245,7 @@ protected function assertNoFieldContainsRawText($field_name, $text) {
* The raw HTML.
*/
protected function getFieldHtml($field_name) {
$css_id = Html::cleanCssIdentifier('edit-' . $field_name . '-wrapper');
$css_id = Html::cleanCssIdentifier('edit-field-' . $field_name . '-wrapper');
return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml();
}
......
......@@ -4958,7 +4958,7 @@
'field_name' => 'field_link',
'entity_type' => 'node',
'bundle' => 'blog',
'data' => 'a:7:{s:5:"label";s:4:"Link";s:6:"widget";a:5:{s:6:"weight";s:2:"10";s:4:"type";s:10:"link_field";s:6:"module";s:4:"link";s:6:"active";i:0;s:8:"settings";a:0:{}}s:8:"settings";a:13:{s:12:"absolute_url";i:1;s:12:"validate_url";i:1;s:3:"url";i:0;s:5:"title";s:8:"required";s:11:"title_value";s:19:"Unused Static Title";s:27:"title_label_use_field_label";i:0;s:15:"title_maxlength";s:3:"128";s:7:"display";a:1:{s:10:"url_cutoff";s:2:"81";}s:10:"attributes";a:6:{s:6:"target";s:6:"_blank";s:3:"rel";s:8:"nofollow";s:18:"configurable_class";i:0;s:5:"class";s:7:"classes";s:18:"configurable_title";i:1;s:5:"title";s:0:"";}s:10:"rel_remove";s:19:"rel_remove_external";s:13:"enable_tokens";i:1;s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:7:"display";a:1:{s:7:"default";a:5:{s:5:"label";s:5:"above";s:4:"type";s:12:"link_default";s:6:"weight";s:1:"9";s:8:"settings";a:0:{}s:6:"module";s:4:"link";}}s:8:"required";i:0;s:11:"description";s:0:"";s:13:"default_value";N;}',
'data' => 'a:7:{s:5:"label";s:4:"Link";s:6:"widget";a:5:{s:6:"weight";s:2:"10";s:4:"type";s:10:"link_field";s:6:"module";s:4:"link";s:6:"active";i:0;s:8:"settings";a:0:{}}s:8:"settings";a:14:{s:12:"absolute_url";i:1;s:12:"validate_url";i:1;s:3:"url";i:0;s:5:"title";s:8:"required";s:11:"title_value";s:19:"Unused Static Title";s:20:"title_allowed_values";s:0:"";s:27:"title_label_use_field_label";i:0;s:15:"title_maxlength";s:3:"128";s:7:"display";a:1:{s:10:"url_cutoff";s:2:"81";}s:10:"attributes";a:6:{s:6:"target";s:6:"_blank";s:3:"rel";s:8:"nofollow";s:18:"configurable_class";i:0;s:5:"class";s:7:"classes";s:18:"configurable_title";i:1;s:5:"title";s:0:"";}s:10:"rel_remove";s:19:"rel_remove_external";s:13:"enable_tokens";i:1;s:18:"user_register_form";b:0;s:23:"entity_translation_sync";b:0;}s:7:"display";a:1:{s:7:"default";a:5:{s:5:"label";s:5:"above";s:4:"type";s:12:"link_default";s:6:"weight";s:1:"9";s:8:"settings";a:0:{}s:6:"module";s:4:"link";}}s:8:"required";i:0;s:11:"description";s:0:"";s:13:"default_value";a:1:{i:0;a:3:{s:5:"title";s:6:"Drupal";s:3:"url";s:23:"https://www.drupal.org/";s:10:"attributes";a:1:{s:5:"title";s:14:"Home - Drupal ";}}}}',
'deleted' => '0',
))
->values(array(
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