From 633b95bee9d5eeeff869aa04af55ec6795f3dda1 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 6 Mar 2017 16:37:36 +0000 Subject: [PATCH] Issue #2613694 by gambry, attiks, scott_euser, cilefen: Expose link_type on LinkWidget element --- .../Plugin/Field/FieldWidget/LinkWidget.php | 1 + core/modules/link/src/Tests/LinkFieldTest.php | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index e1b1c2a8f571..937491f336ee 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -175,6 +175,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#element_validate' => [[get_called_class(), 'validateUriElement']], '#maxlength' => 2048, '#required' => $element['#required'], + '#link_type' => $this->getFieldSetting('link_type'), ]; // If the field is configured to support internal links, it cannot use the diff --git a/core/modules/link/src/Tests/LinkFieldTest.php b/core/modules/link/src/Tests/LinkFieldTest.php index 9e2f433e7a66..0b034d177653 100644 --- a/core/modules/link/src/Tests/LinkFieldTest.php +++ b/core/modules/link/src/Tests/LinkFieldTest.php @@ -609,6 +609,49 @@ public function testLinkSeparateFormatter() { } } + /** + * Test '#link_type' property exists on 'link_default' widget. + * + * Make sure the 'link_default' widget exposes a '#link_type' property on + * its element. Modules can use it to understand if a text form element is + * a link and also which LinkItemInterface::LINK_* is (EXTERNAL, GENERIC, + * INTERNAL). + */ + public function testLinkTypeOnLinkWidget() { + + $link_type = LinkItemInterface::LINK_EXTERNAL; + $field_name = Unicode::strtolower($this->randomMachineName()); + + // Create a field with settings to validate. + $this->fieldStorage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'link', + 'cardinality' => 1, + ]); + $this->fieldStorage->save(); + FieldConfig::create([ + 'field_storage' => $this->fieldStorage, + 'label' => 'Read more about this entity', + 'bundle' => 'entity_test', + 'settings' => [ + 'title' => DRUPAL_OPTIONAL, + 'link_type' => $link_type, + ], + ])->save(); + + $this->container->get('entity.manager') + ->getStorage('entity_form_display') + ->load('entity_test.entity_test.default') + ->setComponent($field_name, [ + 'type' => 'link_default', + ]) + ->save(); + + $form = \Drupal::service('entity.form_builder')->getForm(EntityTest::create()); + $this->assertEqual($form[$field_name]['widget'][0]['uri']['#link_type'], $link_type); + } + /** * Renders a test_entity and sets the output in the internal browser. * -- GitLab