Jump to content

TinyMCE / Textarea field - how to disable making <a> links relative? (For email content)


donatas
 Share

Recommended Posts

I want to keep the full absolute local URLs inside my TinyMCE field (edit: I'm using it to send email content), but after saving they are all overwritten with relative paths to the existing pages (edit: and relative links don't work in emails)

Example: https://domain.com/my-link/ becomes /my-link only. And I need the full https://domain... version.

I think I had seen such option some time ago, but maybe I'm mistaken?
"Link abstraction" doesn't seem to fix this. I've tried many options in the field configuration, but no luck. Any advice?

For now I am adding the domain back while rendering output with a hook for this field, but I'd rather not.

Link to comment
Share on other sites

Using full absolute URLs for internal website links is liable to cause trouble down the line. For example, if the website domain changes you have to update every internal link. Instead you could add a <base> element to your _main.php.

  • Like 1
Link to comment
Share on other sites

I know, I wouldn't want this for a regular page. But I'm using this field for email content that is being sent and links have to be full absolute URLs to work - that's the issue.

The field has to be user-friendly, I can't use raw html. It's edited quite often. I would even like to use the page selector in the pwlink plugin, but that the links should remain absolute.

Link to comment
Share on other sites

  • donatas changed the title to TinyMCE / Textarea field - how to disable making <a> links relative? (For email content)
1 hour ago, donatas said:

But I'm using this field for email content that is being sent and links have to be full absolute URLs to work - that's the issue.

Right, makes sense.

You could change to a CKEditor field as this keeps full absolute URLs if you paste them in. But I actually think this is a good use case for a hook like you are already using, or a custom textformatter module. That way you can use all the normal features of the pwlink plugin and the string replacement is dead simple.

Link to comment
Share on other sites

Using RockFrontend you could also do this, for example inside a saveReady hook:

<?php

use Wa72\HtmlPageDom\HtmlPageCrawler;

$dom = rockfrontend()->dom($page->body);
$dom
  ->filter("a")
  ->each(function (HtmlPageCrawler $node) {
    $href = $node->getAttribute("href");

    if (!$href) return;
    if (strpos($href, "http://") === 0) return;
    if (strpos($href, "https://") === 0) return;

    $https = rtrim($this->wire->pages->get(1)->httpUrl(true),"/");
    $node->setAttribute("href", $https.$href);
  });

$page->set('body', $dom->html());

I've added docs for it on the dev branch: https://github.com/baumrock/RockFrontend/tree/dev/docs/dom

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...