DrQuincy Posted February 5, 2021 Share Posted February 5, 2021 Is there any way in a CKEditor field to have links to the current host automatically converted to root relative links? E.g. I'm on domain.com and I add this code in source: <a href="https://www.domain.com/foo">Foo</a> And on save or in the front end it converts it to: <a href="/foo">Foo</a> Any other links would stay as is. PW seems to be good at using root relative links (e.g. if you select a page from the link CKEditor dialog) but wondered if this is possible without having to do a find/replace in the database. I'm thinking of instances where you move from development to product and end up absolute links to the development domain. Link to comment Share on other sites More sharing options...
wbmnfktr Posted February 5, 2021 Share Posted February 5, 2021 I'm not totally sure about the HTML Options in the textarea settings if they might already do that or just look for changes. But ProCache is able to do that (never tested to be honest). Edit: I have to revoke this. ProCache does not change any <a href="">. 1 Link to comment Share on other sites More sharing options...
Robin S Posted February 5, 2021 Share Posted February 5, 2021 Do you mean that editors are pasting absolute internal links into the link dialog in CKEditor? If so you could try an education approach first, i.e. "Please create internal links via the fields in the link dialog". That way you won't get absolute internal links in the first place. Beyond that you could use a saveReady hook to do a string replacement in the relevant field(s): $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); // Perhaps limit by template as needed if($page->template == 'basic-page') { // Internal links: replace absolute href with relative href $page->body = str_replace('href="https://www.domain.com/', 'href="/', $page->body); } }); 3 Link to comment Share on other sites More sharing options...
DrQuincy Posted February 8, 2021 Author Share Posted February 8, 2021 Thanks @wbmnfktr I think the Markup/HTML option refers to the where internal links go to rather than the format. Thanks @Robin S. I do often add that in the notes but it does seem to confuse some users — of they just don't read it. I like your hook idea though; that could be very effective. 2 Link to comment Share on other sites More sharing options...
DrQuincy Posted February 19, 2021 Author Share Posted February 19, 2021 In case anyone is interested this will remove absolute links on all specified HTML (CKEditor Textarea) fields using the current host (HTTP and HTTPS) and is case-insensitive. Based on @Robin S’s code. $wire->addHookAfter('Pages::saveReady', function(HookEvent $event) { $htmlFields = [ // NOTE: Set your fields here 'html1', 'html2', ]; $host = $_SERVER['HTTP_HOST']; $find = [ 'href="http://' . $host . '/', 'href="https://' . $host . '/', ]; $page = $event->arguments(0); foreach ($htmlFields as $htmlField) { if ($page->$htmlField !== null) { $numChanged = 0; foreach ($find as $f) { $numChanged += mb_substr_count(mb_strtolower($page->$htmlField), $f); $page->$htmlField = str_ireplace($f, 'href="/', $page->$htmlField); } if ($numChanged > 0) { $this->message($numChanged . ' ' . $host . ' link(s) converted to relative links for field “' . $htmlField . '”'); } } } }); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now