nickspiel
nickspiel

Reputation: 5630

SilverStripe wysiwyg images not working without base_tag

I have removed the base_tag from my template to allow for the use of SVG icon sprites. A side effect of this is that images on pages other than the home page break.

The HTMLEditorField::saveInto() method is forcing a relative url on the image and they end up pointing to /some-page-other-than-home/assets/image.jpg which is wrong.

How do I get images to resolve to the root /assets/ directory rather than pointing to /some-page-other-than-home/assets/.

Upvotes: 2

Views: 183

Answers (1)

nickspiel
nickspiel

Reputation: 5630

SilverStripe provides extension hooks on some methods. The HTMLEditorField has a 'processImage' hook that you can tap into.

You can hook into this method with the following:

1. Create your extension configuration.

/mysite/_config/Config.yml

HtmlEditorField:
  extensions:
    - HTMLEditorFieldExtension

2. Create a the HTMLEditorFieldExtension class.

/mysite/code/HTMLEditorFieldExtension.php

<?php
class HTMLEditorFieldExtension extends DataExtension
{
    // This method name must be the same as the extension hook
    public function processImage($image, $img) {

        // Get the image src attribute
        $src = $img->getAttribute('src');

        // Concatenate  a leading slash
        $img->setAttribute('src', '/' . $src);
    }
}

3. Run a dev/build.

This needs to be done so SilverStripe can find the new extension. After this your images should now have the missing leading slash.

Upvotes: 1

Related Questions