While working on a patch for Linkit I failed to set a custom attribute as link text without completely overriding the plugin.js of drupallink.

drupallink uses this code to create the link text: var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
Do I have to override the complete plugin to use another attribute returned by the editor dialog?

Eventually the following code fragment would be an option (as a patch), but maybe there is a better way.

// Use link URL as text with a collapsed cursor.
if (range.collapsed) {
  // Shorten mailto URLs to just the email address.
  var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
  if (editor.config.hasOwnProperty('drupalLink_attributeText') && returnValues.attributes.hasOwnProperty(editor.config.drupalLink_attributeText) && returnValues.attributes[editor.config.drupalLink_attributeText].length) {
    // Use custom attribute as link text.
    text = new CKEDITOR.dom.text(returnValues.attributes[editor.config.drupalLink_attributeText], editor.document);
  }
  range.insertNode(text);
  range.selectNodeContents(text);
}
CommentFileSizeAuthor
#103 2741945-103-core-10.4.patch3.2 KBduaelfr
#103 2741945-103-ckeditor.patch8.1 KBduaelfr
#101 2741945-101.patch3.17 KBburnellw_cit
#100 2741945-100.patch11.79 KBsegi
#98 2741945-98.patch11.18 KBstuart_wilkes_wellcome
#94 interdiff-85-92.txt702 bytesOscaner
#94 2741945-92.patch11.39 KBOscaner
#92 interdiff-85-92.txt702 bytespotem
#92 2741945-92.patch11.39 KBpotem
#85 Sélection_177.png32.88 KBduaelfr
#85 interdiff.2741945.83.85.txt4.56 KBduaelfr
#85 2741945-85.patch11.35 KBduaelfr
#84 Sélection_176.png36.06 KBduaelfr
#83 interdiff-79-83.txt597 bytesnaresh_bavaskar
#83 2741945-83.patch10.69 KBnaresh_bavaskar
#79 2741945-79.patch10.68 KBakashkumar07
#66 2741945-66.patch10.8 KBwim leers
#66 interdiff.txt2.33 KBwim leers
#65 2741945-65.patch11.13 KBwim leers
#65 interdiff-63-65.txt7.38 KBwim leers
#65 interdiff.txt2.33 KBwim leers
#64 2741945-64.patch12.1 KBwim leers
#64 interdiff.txt7.02 KBwim leers
#63 2741945-63.patch11.32 KBwim leers
#59 ckeditor-allow-default-text-2741945-59.patch1015 bytesoknate
#58 ckeditor-allow-default-text-2741945-58.patch1.06 KBoknate
#52 interdiff-2741945-41-42.txt1.75 KBMaskyS
#47 gmail_link_without_selection.png55.91 KBduaelfr
#47 gmail_link_with_selection.png59.7 KBduaelfr
#47 libreoffice_link_without_selection.png40.37 KBduaelfr
#47 libreoffice_link_with_selection.png36.5 KBduaelfr
#47 gdoc_link_without_selection.png7.05 KBduaelfr
#47 gdoc_link_with_selection.png16.08 KBduaelfr
#42 innerdiff-2741945-41-42.txt1.91 KBanon
#42 allow_modules_to_alter-2741945-42.patch10.96 KBanon
#41 innerdiff-2741945-33-41.txt2.14 KBanon
#41 allow_modules_to_alter-2741945-41.patch11.51 KBanon
#39 innerdiff-2741945-33-39.txt38.84 KBanon
#39 allow_modules_to_alter-2741945-39.patch11.51 KBanon
#33 innerdiff-2741945-31-33.txt6.14 KBanon
#33 allow_modules_to_alter-2741945-33.patch11.62 KBanon
#31 allow_modules_to_alter-2741945-31.patch11.68 KBanon
#31 interdiff-2741945-30-31.txt7.48 KBanon
#30 drupal-alter_EditorLinkDialog_link_text-2741945-30.patch11.94 KBstborchert
#28 drupal-alter_EditorLinkDialog_link_text-2741945-28.patch7.55 KBstborchert
#22 drupal-alter_EditorLinkDialog_link_text-2741945-22.patch9.35 KBstborchert
#11 ckeditor_title_as_link_text-2741945-11.patch1.13 KBgifad

Comments

stBorchert created an issue. See original summary.

wim leers’s picture

What exactly were you trying to do? I don't quite understand.

stborchert’s picture

Sorry, if this sounds confusing. The issue I'm talking about is #2653246: Override link text using the title attribute.
Using LinkIt you are able to add links in CKEditor in a very comfortable way. Additionally, one can enter some attributes for the link (using Editor advanced link).
Unfortunately with the newest version of LinkIt is it not possible anymore to set the link text because the module uses the default Drupal plugin for links (drupallink).

My question is now: what is the best way to override the inserted link text? Do we need to override the complete plugin.js of drupalling or is it better to create a patch for drupallink so this can be done using a small function (or something else)?

wim leers’s picture

Override it in the EditorImageDialog form's submit handler.

wim leers’s picture

Oh wait, I see you're asking it for #2653246: Override link text using the title attribute. Which means you don't care about how editor module does things, you care about actual CKEditor plugin code.

My answer is valid for LinkIt 5, you won't need to modify the CKEditor plugin anymore.

stborchert’s picture

But, uhm ... I'm confused, now. drupallink/plugin.js uses the following code to get the link title:
var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);

I've actually no idea, how to use a different attribute here (i.e. "title") by simply overriding the submit function of the dialog :|

stborchert’s picture

I must admit, I'm a little bit lost.
Let's assume, returnValues contains not only "href" as attribute, but also "title". How do I use this attribute as the link text without overriding drupallink/plugin.js?

wim leers’s picture

I'd recommend looking at https://www.drupal.org/project/editor_advanced_link as an example. That's how you can get additional attributes to work.

But I think that is not what you are asking. I think you're asking for help with CKEditor JS code. This is not the right place for that. I believe they provide support on Stack Overflow.

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)
stborchert’s picture

I guess, we are talking of different things here. Editor advanced link adds some attributes to the link created by drupallink/plugin.js, so in case of <a href="/node/1">/node/1</a> we can create links like <a href="/node/1" title="First article">/node/1</a>.

With Linkit 4.x it was possible to create links like <a href="/node/1" title="First article">First article</a> (the *text* of the link is created using the title attribute). This was possible, because the maintainer of Linkit has used a custom CKEditor plugin.
In version 5.x this custom plugin is gone, so drupallink/plugin.js is used. I don't see a way to override the link *text* with a value of an attribute without using a custom CKEditor plugin since the text is hardcoded to returnValues.href in drupallink/plugin.js.

gifad’s picture

Category: Support request » Feature request
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.13 KB

@stBorchert : The patch you submitted in Linkit queue seems to be perfectly valid in core link plugin.

Additionally, it would be a nice addition for the users of editor advanced link.

Attached, your patch, just rerolled.

wim leers’s picture

The reason the LinkIt module doesn't do that anymore is explained in detail elsewhere. In short: it doesn't work when the title of linked content changes, or when a translated version is linked.

I don't see a valid use case for this?

anon’s picture

@Wim Leers: As I see it, it is a request for adding "other" text to the element, not the source link or selected value.

Cosider the following where the bold text is the selected text.

"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
Adding a link to /example

"Lorem <a href="/example">ipsum dolor</a> sit amet, consectetur adipiscing elit."

"Lorem ipsum dolor sit (Cursor here) amet, consectetur adipiscing elit." (Nothing selected)
Adding a link to /example

"Lorem ipsum dolor sit <a href="/example">/example</a> amet, consectetur adipiscing elit."

See the second example. Here the user has not selected any text to use for the element text. The URL will then be used by the default link plugin as the element text.

This is what the issue is about. The ability to configure the plugin to use some other texts here.

wim leers’s picture

Ahhh! Now I get it. Thanks, that really helps :)

So then the proposal here is that if there is a value specified for the title attribute, that we use that as the default link text instead of using the URL itself as the default link text?

anon’s picture

Basically yes.

Tho I'm not sure if the patch provided in #11 is the correct way to go. There might be a better structure that don't include the "attribute" name as it could be kind of confusing to other devs.

So maybe:
If there is a value specified for the element text, we use that as the default link text instead of using the URL itself as the default link text.

wim leers’s picture

You mean: you'd like to be able to specify link text, independently of the title attribute?

stborchert’s picture

Thanks, @anon for explaining it better :)
My initial idea (if there would be no other way) was to allow overriding the way, the link text is being "generated" (if there is no text selected in the editor). Having this possibility, other modules may use returnValues.attributes.title or something completely different.

So, yes ... specifying the link text independently of the title attribute would be great.

wim leers’s picture

Title: Best way to use custom attribute instead of href in ckeditor::drupallink » Allow modules to alter EditorLinkDialog to specify link text
Issue tags: +Contributed project blocker

Ok, so that then is acceptable. It's a valid use case, although an edge casey one. We should retain the current behavior by default (changing it would break the expectation 99% of users have), but it's fine for contrib modules to override this in particular cases.

So then this would need to:

  1. Send the current link text when making the request to /editor/dialog/image/basic_html, because the dialog needs to be able to show the current data.
    Currently:
    editor_object[href]:http://foobar.com
    

    we'd need to add to that:

    editor_object[text]: CURRENT_TEXT_OR_EMPTY_STRING
    

    I think text is acceptable, since it's not a valid attribute on <img>, so we can repurpose it. Thoughts about that?

  2. We then need to modify core/modules/ckeditor/js/plugins/drupallink/plugin.js much like the existing patch does.

Most painfully… this will need tests. JavaScript tests. Of which we have none yet for CKEditor yet. Because we've only had the ability to do JS tests since a few months. Sorry about that :(

anon’s picture

Send the current link text when making the request to /editor/dialog/image/basic_html, because the dialog needs to be able to show the current data.

Does this means that there will be an additional input field in the dialog used for "texts"?

How should other modules alter this?

Is it an absolute requirement for js-tests in order to get a possible path committed to 8.2.x?

wim leers’s picture

Does this means that there will be an additional input field in the dialog used for "texts"?

Not in Drupal core. But contrib modules could do that, yes. Of course, a contrib module can choose to not expose an input[type=text] and instead set a value based on some logic in the submit handler.

How should other modules alter this?

See above.

Is it an absolute requirement for js-tests in order to get a possible path committed to 8.2.x?

I'm not sure. It's the early days of JS tests, but I know that several other patches that were RTBC were un-RTBC'ed specifically because we can now write JS tests and hence they should have JS tests.

stborchert’s picture

Assigned: Unassigned » stborchert
Status: Needs review » Needs work

Back to "needs work". I'll have a look into this today.

stborchert’s picture

Assigned: stborchert » Unassigned
Status: Needs work » Needs review
StatusFileSize
new9.35 KB

Seems, "today" is a very flexible term ...

Anyway, here is a first draft of the patch. Unfortunately I couldn't get the javascript test to work, since the link dialog simply wouldn't open when the test clicks on the ckeditor button.
Any idea what is wrong in DrupalLinkTest?

PS: a patch to test overriding the link text can be found in #2653246-13: Override link text using the title attribute

Status: Needs review » Needs work

The last submitted patch, 22: drupal-alter_EditorLinkDialog_link_text-2741945-22.patch, failed testing.

wim leers’s picture

#22: My best guess is that CKEditor hasn't finished loading yet? Can you let your functional JS test first verify that the expected "DrupalLink" button actually exists? You already verify all that! No idea then :(

However, if we look at the test output, it actually looks like you're hitting a bug in GastonJS:

fail: [Other] Line 25 of core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php:
Drupal\Tests\ckeditor\FunctionalJavascript\DrupalLinkTest::testOverridingLinkText
Zumba\GastonJS\Exception\BrowserError: There was an error inside the PhantomJS portion of GastonJS.
This is probably a bug, so please report it:
click
html.touchevents.details.js body.user-logged-in.path-node div.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.ui-front.editor-link-dialog.ui-dialog--narrow.ui-dialog-buttons div#drupal-modal.ui-front.ui-dialog-content.ui-widget-content div#editor-link-dialog-form form#editor-link-dialog--_bnF8vu081Q.editor-link-dialog

/var/www/html/vendor/jcalderonzumba/gastonjs/src/Browser/BrowserBase.php:122
/var/www/html/vendor/jcalderonzumba/gastonjs/src/Browser/BrowserBase.php:99
/var/www/html/vendor/jcalderonzumba/gastonjs/src/Browser/BrowserMouseEventTrait.php:17
/var/www/html/vendor/jcalderonzumba/mink-phantomjs-driver/src/MouseTrait.php:31
/var/www/html/vendor/behat/mink/src/Element/NodeElement.php:153
/var/www/html/vendor/behat/mink/src/Element/NodeElement.php:161
/var/www/html/vendor/behat/mink/src/Element/TraversableElement.php:115
/var/www/html/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php:62

However, this is also causing a fail in CKEditorTest. Because there's now two text formats/editors being installed, instead of one. You shouldn't modify ckeditor_test to install additional formats/editors. You should instead let your test create the desired formats/editors! Then that problem goes away. :)

Are you at DrupalDevDays Milan? Find dawehner, he will know how to debug that PhantomJS/GastonJS bug.

wim leers’s picture

Forgot to say: awesome work here — thanks! :)

stborchert’s picture

You shouldn't modify ckeditor_test to install additional formats/editors. You should instead let your test create the desired formats/editors!

Jup, already noticed this and am on it :)

Are you at DrupalDevDays Milan? Find dawehner, he will know how to debug that PhantomJS/GastonJS bug.

Unfortunately not, but I will talk to him afterwards.

I hope to create the next patch draft today ...

wim leers’s picture

Great — thanks so much!

stborchert’s picture

Status: Needs work » Needs review
StatusFileSize
new7.55 KB

Next try (DrupalLinkTest will fail because of the known problem).
Its very strange, why the dialog does not open (it had opened exactly 2 times during my times and I don't know, why ... running the test again without modifying something and the dialog didn't open). I've looked at the tests provided by Views and couldn't find any differences :/

Status: Needs review » Needs work

The last submitted patch, 28: drupal-alter_EditorLinkDialog_link_text-2741945-28.patch, failed testing.

stborchert’s picture

Status: Needs work » Needs review
StatusFileSize
new11.94 KB

Thanks to the help of dawehner I could finally fix the tests.
I'm not very happy with the javascript code I execute in the test but this was the only way to access the content of the ckeditor body. $session->switchToIFrame() didn't work here, because the iframe doesn't have a name :/

anon’s picture

StatusFileSize
new7.48 KB
new11.68 KB
  1. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,173 @@
    +    // Create text format, associate CKEditor.
    +    $llama_format = FilterFormat::create([
    +              'format' => 'llama',
    +              'name' => 'Llama',
    +              'weight' => 0,
    +              'filters' => [],
    +    ]);
    +    $llama_format->save();
    +    $editor = Editor::create([
    +              'format' => 'llama',
    +              'editor' => 'ckeditor',
    +    ]);
    

    Indent errors.

  2. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,173 @@
    +    // Assign widget settings for the 'default' form mode.
    +    EntityFormDisplay::create([
    +              'targetEntityType' => 'node',
    +              'bundle' => 'page',
    +              'mode' => 'default',
    +              'status' => TRUE,
    +            ])
    +            ->setComponent('body', ['type' => 'text_textarea_with_summary'])
    +            ->save();
    

    Indent errors

  3. +++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
    @@ -0,0 +1,107 @@
    +    $this->installSchema('node', array('node_access'));
    

    Use arrays are use everywhere eles, should be used here aswell.

I have fixed this issues in this patch.

anon’s picture

  1. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +    $this->assertTrue($ckeditor_loaded, 'The editor has not been loaded.');
    
    +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +    $this->assertTrue($ckeditor_loaded, 'The editor has not been loaded.');
    

    The message should be positive.
    The editor has not been loaded.

  2. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +    $this->assertEquals('/node/add/page', $link_text, 'Inserted link text does not equals expected text.');
    
    +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +    $this->assertEquals('Add page', $link_text, 'Inserted link text does not equals expected text.');
    

    The message should be positive.
    Inserted link text does not equals expected text.

anon’s picture

StatusFileSize
new11.62 KB
new6.14 KB

Fixed some coding standards and the test messages.

anon’s picture

Should we open a new issue to add a name to the iframe? Other tests would benefit from that, i.e. DrupalImage and contrib modules like Linkit.

stborchert’s picture

anon’s picture

  1. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +use Drupal\simpletest\ContentTypeCreationTrait;
    ...
    +  use ContentTypeCreationTrait;
    

    Is it ok to use simpletest classes in these tests?

  2. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,169 @@
    +    $link_button = $page->find('css', 'a.cke_button__drupallink');
    +    $link_button->click();
    ...
    +    $button = $page->find('css', '.editor-link-dialog')->find('css', '.button.form-submit span');
    +    $button->click();
    ...
    +    $link_button = $page->find('css', 'a.cke_button__drupallink');
    +    $link_button->click();
    ...
    +    $button = $page->find('css', '.editor-link-dialog')->find('css', '.button.form-submit span');
    +    $button->click();
    

    Could be single lines?

anon’s picture

@stBorchert: Great, thanks!

Lets see what they say about this. If they don't like it, can Drupal add a name attribute itself in any way?

stborchert’s picture

Is it ok to use simpletest classes in these tests?

Well, ExposedFilterAJAXTest is doing this also, so I guess it is ok.

Could be single lines?

Indeed. I had extra checks to test if the elements are accessible but removed this. So this can be merged into single lines.

anon’s picture

StatusFileSize
new11.51 KB
new38.84 KB

This patch contains fixes mentiond in #33.

anon’s picture

Crap, messed up when re-rolled.

anon’s picture

anon’s picture

StatusFileSize
new10.96 KB
new1.91 KB

There is no need to create a node type in EditorLinkDialogTest.

Also made some minor cleanup.

Status: Needs review » Needs work

The last submitted patch, 42: allow_modules_to_alter-2741945-42.patch, failed testing.

The last submitted patch, 42: allow_modules_to_alter-2741945-42.patch, failed testing.

anon’s picture

Status: Needs work » Needs review

Retriggered tests.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

duaelfr’s picture

Wouldn't it be a good idea to always show that text field?
I mean, on most common tools (Word, GMail, Gdocs, etc.) when you want to create a link, the UI shows you the text of the link, prepopulated if something was selected. I think it's more convenient for the end user and probably less scary.

Google Docs add a link while a text is selected

Google Docs add a link while no text is selected

LibreOffice add a link while a text is selected

LibreOffice add a link while no text is selected

GMail add a link while a text is selected

GMail add a link while no text is selected

jonathanshaw’s picture

What your propose based on Gmail and Libre Office seems good to me. However, the maintainer rejected an additional input box like this in #20.

How about we keep this issue confined to adding to the API the ability to specify the text; then in an follow-up issue we can endlessly debate the best default UI for core? That way we unblock contrib/custom more quickly.

Status: Needs review » Needs work

The last submitted patch, 42: allow_modules_to_alter-2741945-42.patch, failed testing.

jonathanshaw’s picture

Issue tags: +Needs reroll

Needs reroll against 8.3

MaskyS’s picture

Assigned: Unassigned » MaskyS
Issue tags: +Novice

@jonathanshaw I think that's a good idea... Let's re-roll this in the meanwhile.

MaskyS’s picture

Issue tags: -Needs reroll
StatusFileSize
new1.75 KB

Hi, I don't think this needs a re-roll since it applied cleanly on 8.3.x. Here is an interdiff for the last two patches though.

root@masky-VirtualBox:/home/drupal/cap/drupal# git apply -v allow_modules_to_alter-2741945-42.patch 
Checking patch core/modules/ckeditor/js/plugins/drupallink/plugin.js...
Checking patch core/modules/ckeditor/tests/modules/ckeditor_test.module...
Checking patch core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php...
Checking patch core/modules/editor/src/Form/EditorLinkDialog.php...
Checking patch core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php...
Applied patch core/modules/ckeditor/js/plugins/drupallink/plugin.js cleanly.
Applied patch core/modules/ckeditor/tests/modules/ckeditor_test.module cleanly.
Applied patch core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php cleanly.
Applied patch core/modules/editor/src/Form/EditorLinkDialog.php cleanly.
Applied patch core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php cleanly.
MaskyS’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 42: allow_modules_to_alter-2741945-42.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jonathanshaw’s picture

Status: Needs work » Needs review

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

oknate’s picture

This patch is related to this issue, but reduces the scope of the changes significantly. I just need a way for other modules to override the default text, specifically linkit. If you click in ckeditor without highlighting text, it displays the href as the text of the link. There's no way, short of patching core for other modules to change this.

oknate’s picture

StatusFileSize
new1015 bytes

This is same as #58, but rerolled because #58 wasn't working on Drupal 8.4.2

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Assigned: MaskyS » wim leers
Status: Needs review » Needs work
Issue tags: -Novice

Was reminded of this via #2640398-21: Allow <drupal-entity> elements to be inline CKEditor Widgets. Completely lost track of this. My focus has been API-First for the past two years. Very sorry.

This looks great.

@oknate: while well-intentioned, those other changes are just test coverage. I think having test coverage for this makes sense. So I'm bringing back @anon's patch from #42, but I do prefer your handling of the text over @anon's, because there is no text attribute.

Rerolling…

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new11.32 KB

This is a straight rebase of #42.

wim leers’s picture

StatusFileSize
new7.02 KB
new12.1 KB
  1. +++ b/core/modules/ckeditor/js/plugins/drupallink/plugin.js
    @@ -113,7 +113,13 @@
    +                // Allow overriding the link text by using the special attribute "text".
    +                if (returnValues.attributes.hasOwnProperty('text') && returnValues.attributes.text.length) {
    +                  linkText = returnValues.attributes.text;
    +                }
    

    This only allows the server to override the text on the client; the client doesn't pass the current value to the server.

  2. +++ b/core/modules/ckeditor/tests/modules/ckeditor_test.module
    @@ -13,3 +14,13 @@
    +  $form['attributes']['text']['#type'] = 'textfield';
    
    +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,165 @@
    +    $page->fillField('attributes[text]', 'Add page');
    
    +++ b/core/modules/editor/src/Form/EditorLinkDialog.php
    @@ -49,6 +49,13 @@ public function buildForm(array $form, FormStateInterface $form_state, Editor $e
         ];
    ...
    +    $form['attributes']['text'] = array(
    
    +++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
    @@ -0,0 +1,94 @@
    +    $this->assertEquals('some text', $form_state->getValue(['attributes', 'text'], 'not-this'), 'Attribute "text" exists and has the correct value.');
    

    text is not an attribute, so this should not be ['attributes']['text'], but ['text'].

    #58 did this better IMHO. This keeps the name text, but stops putting it under attributes.

  3. +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/DrupalLinkTest.php
    @@ -0,0 +1,165 @@
    +    // We can't use $session->switchToIFrame() here, because the iframe does not
    +    // have a name.
    +    $javascript = <<<JS
    +    (function(){
    +      var iframes = document.getElementsByClassName('cke_wysiwyg_frame');
    +      if (iframes.length) {
    +        var doc = iframes[0].contentDocument || iframes[0].contentWindow.document;
    +        var link = doc.getElementsByTagName('a')[0];
    +        return link.innerText.trim() || link.textContent || '';
    +      }
    +    })()
    +JS;
    +    $link_text = $session->evaluateScript($javascript);
    ...
    +    // We can't use $session->switchToIFrame() here, because the iframe does not
    +    // have a name.
    +    $javascript = <<<JS
    +    (function(){
    +      var iframes = document.getElementsByClassName('cke_wysiwyg_frame');
    +      if (iframes.length) {
    +        var doc = iframes[0].contentDocument || iframes[0].contentWindow.document;
    +        var link = doc.getElementsByTagName('a')[0];
    +        return link.innerText.trim() || link.textContent || '';
    +      }
    +    })()
    +JS;
    +    $link_text = $session->evaluateScript($javascript);
    

    This is repeating the exact same complex logic twice. Let's move it to a helper.

  4. +++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
    @@ -0,0 +1,94 @@
    +  protected $format;
    ...
    +    $this->format = FilterFormat::create([
    ...
    +    $editor = Editor::create([
    ...
    +      ->addBuildInfo('args', [$this->format]);
    

    In the time that has passed, it's the Editor that became expected, not the FilterFormat.

  5. +++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
    @@ -0,0 +1,94 @@
    +  /**
    +   * Modules to enable.
    +   *
    +   * @var array
    +   */
    ...
    +  /**
    +   * Sets up the test.
    +   */
    

    Nit: inheritdoc.

wim leers’s picture

StatusFileSize
new2.33 KB
new7.38 KB
new11.13 KB

I messed that up.

wim leers’s picture

Assigned: wim leers » Unassigned
StatusFileSize
new2.33 KB
new10.8 KB
+++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
@@ -0,0 +1,91 @@
+class EditorLinkDialogTest extends EntityKernelTestBase {

I believe this can become quite a bit simpler.

The last submitted patch, 64: 2741945-64.patch, failed testing. View results

The last submitted patch, 63: 2741945-63.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 65: 2741945-65.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 66: 2741945-66.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

firewaller’s picture

+1

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

lukus’s picture

Hi there .. I've also come to this issue via (https://www.drupal.org/comment/12335702#comment-12335702).

Feels like all of the solutions for a particular problem I'm facing point towards LinkIt as the best approach.

I'm wondering what the status of this is now? Is there anything I can help out with?

jonathanshaw’s picture

@lukus The failing test in #66 needs addressing.

ravi.shankar’s picture

Issue tags: +Needs reroll

Patch #66 not applying on Drupal 9.1.x.

akashkumar07’s picture

Assigned: Unassigned » akashkumar07
akashkumar07’s picture

Status: Needs work » Needs review
StatusFileSize
new10.68 KB

Rerolled patch #66

akashkumar07’s picture

Assigned: akashkumar07 » Unassigned

Status: Needs review » Needs work

The last submitted patch, 79: 2741945-79.patch, failed testing. View results

naresh_bavaskar’s picture

Assigned: Unassigned » naresh_bavaskar
naresh_bavaskar’s picture

Assigned: naresh_bavaskar » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new10.69 KB
new597 bytes

Please review, Fixing the failed test cases

duaelfr’s picture

Assigned: Unassigned » duaelfr
Status: Needs review » Needs work
StatusFileSize
new36.06 KB

Thank you all for moving this forward!

I did some testing and the patches does not entirely fill the issue, though.

Plus, I've noticed that you directly edited the plugin.js file instead of the plugin.es6.js file which is the current way to contribute JS in Core.

I'll try to provide a new patch today.

duaelfr’s picture

Assigned: duaelfr » Unassigned
Status: Needs work » Needs review
StatusFileSize
new11.35 KB
new4.56 KB
new32.88 KB

Here we go.


I feel like we should extend the test coverage for some of these cases but I have no idea of how to do that for those with selected text or existing link editing.

PS: I moved the text field above the attributes in the Dialog so contrib modules won't have to play with weights to look like most common tools (cf #47) when they will transform the field's type.

jonathanshaw’s picture

+++ b/core/modules/ckeditor/tests/modules/src/FunctionalJavascript/DrupalLinkTest.php
@@ -0,0 +1,162 @@
+   */
...
+    $this->drupalGet('node/add/page');
...
+    $page->fillField('attributes[href]', '/node/add/page');
...
+    $page->fillField('text', 'Add page');
...
+    $this->assertFirstLinkText('Add page');

+++ b/core/modules/editor/tests/src/Kernel/EditorLinkDialogTest.php
@@ -0,0 +1,75 @@
+

I think these tests would be more robust and easier to understand if we were adding a link to a page other than the one we are on e.g. /admin/some/destination "Some link text"

jonathanshaw’s picture

#85

I feel like we should extend the test coverage for some of these cases but I have no idea of how to do that for those with selected text or existing link editing.

In slack @lendude said that Googling suggested that double clicking a word was perhaps the best way to accomplish selecting text in a test. He also provided these helper methods which I paste here in case they're of any use.

/**
 * Fill a CKEditor field in a container.
 *
 * @param string $label
 *   The label of the CKEditor field.
 * @param string $value
 *   The value to set in the CKEditor field.
 * @param \Behat\Mink\Element\TraversableElement $container
 *   (optional) The element to search for the field. Defaults to the current
 *   page.
 * @param bool $set_as_source
 *   Indicate if the passed value should be set as 'source', use this when
 *   adding HTML tags in the value. Defaults to FALSE.
 */
protected function fillCkeditorField($label, $value, TraversableElement $container = NULL, $set_as_source = FALSE) {
  if (!$container) {
    $container = $this->getSession()->getPage();
  }
  // Get the field wrapper for the field. The first parent is.
  $field_wrapper = $this->findParentByClass($container->findField($label), 'field--type-text-long');
  if ($set_as_source) {
    $container->find('css', '.cke_button__source')->click();
    $container->find('css', '.cke_source')->setValue($value);
  }
  else {
    // Switch to CKEditor iframe and set te value.
    $this->switchToUnnamedIframe('#' . $field_wrapper->getAttribute('id') . ' iframe');
    $page = $this->getSession()->getPage();
    $field = $page->find('css', 'body');
    $field->focus();
    $field->setValue($value);
    // Switch back to the parent frame.
    $this->getSession()->getDriver()->switchToIFrame();
  }
}
/**
 * Switches to an unnamed iframe.
 *
 * @param string $iframeSelector
 *   The CSS selector for the iframe.
 *
 * @throws \Behat\Mink\Exception\DriverException
 * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
 */
protected function switchToUnnamedIframe($iframeSelector) {
  static $iframe_count = 0;
  $name = "iframeToSwitchTo" . $iframe_count;
  $function = <<<JS
  (function(){
     let iframe = document.querySelector("$iframeSelector");
     iframe.name = "$name";
  })()
JS;
  try {
    $this->getSession()->executeScript($function);
  }
  catch (\Exception $e) {
    throw new AssertionFailedError("Element '$iframeSelector' was not found. " . $e->getMessage());
  }
  $iframe_count++;
  $this->getSession()->getDriver()->switchToIFrame($name);
}
lukus’s picture

This applies well for 8.9.x.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

potem’s picture

StatusFileSize
new11.39 KB
new702 bytes

I think that most of the time users just customize the link text.
So I changed the $form['text']['#type'] to textfield, no need to use hooks or modules to override the EditorLinkdialog

potem’s picture

Oscaner’s picture

StatusFileSize
new11.39 KB
new702 bytes

@potem upload fail for patch 2741945-92.patch. So i help him to re-upload.
This patch just update type 'value' to type 'textfield'.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Project: Drupal core » CKEditor 4 - WYSIWYG HTML editor
Version: 9.5.x-dev » 1.0.x-dev
Component: ckeditor.module » Code

CKEditor has been removed from core, CKEditor 4 is removed from Drupal Core in 10.0.0

stuart_wilkes_wellcome’s picture

StatusFileSize
new11.18 KB

The existing patch was failing with Drupal 9.5 - so have re-rolled for that:

coopernet’s picture

Patch 2741945-98.patch failed to apply with drupal 9.5.1 php 8.1

segi’s picture

StatusFileSize
new11.79 KB

Hi, I re-rolled the patch and tested with 9.5.0 and 9.5.2.

burnellw_cit’s picture

StatusFileSize
new3.17 KB

Status: Needs review » Needs work

The last submitted patch, 101: 2741945-101.patch, failed testing. View results

duaelfr’s picture

StatusFileSize
new8.1 KB
new3.2 KB

I needed that on D10 to delay an upgrade so I rerolled and split the patch.

If you need that feature you have to apply:

  • 2741945-103-ckeditor.patch on the ckeditor module
  • 2741945-103-core-10.4.patch on Drupal Core (tested on 10.4 but might also work on previous 10.x versions)