Trouble getting FRED to save correct path to image

When editing an image TV in FRED, it insists on adding the full path the the image in front of the image path, regardless of what media source is assigned to the image. So, for example, and image TV set to the “Images” media source, with the path “assets/images”, is saved correctly when edited in MODX as:

test-stuff/inside-sample-2024.png

But when edited in FRED, it gets saved as:

/assets/site/images/test-stuff/inside-sample-2024.png

I tried setting the image inside the page template both with and without data-fred-name="page-image" data-fred-target="tv_pageimagenew", and it made no difference.

The “Images” media source is enabled for FRED, the image TV has the “Images” media source assigned to it, and TV Properties has fred.mediaSource set to “Images” and fred.type set to “image”.

What am I missing?

MODX 2.8.7, FRED 3.0 0

Is this the same issue like the one you created back in June?

Kind of the same issue, but this is the new version of FRED. That fix isn’t working for FRED 3.

Yes, but the code change from the old forum topic (that resolved the issue) was never acutally changed in the Fred source code.
Does it fix this issue, if you apply the same code change again (to this new version of Fred)?

looks like our posts overlapped. It’s not working for FRED 3.

So it looks like the same functionality (removing the path of the media-source from the TV value) is now executed in this function (same function name but different file):

(I tried to test it (locally on Windows), but with paths in Windows (and MODX in a subfolder) the whole function code breaks anyway).

  • Do you get this error in the MODX error log? → Could not load class: modFileMediaSource from mysql.modfilemediasource.
  • If not, log the values of $url and $value after line 438 to help to figure out why the media-source-path doesn’t get removed:
$this->modx->log(\modX::LOG_LEVEL_ERROR, "[Fred | reversePreparedOutput] media-source-url = {$url} | TV-value = {$value}");

Ah. I wondered why none of my edits to the old file were having any effect. This new file does have the fix in it, but it’s not working.

Yes, I get that error message now.

Here’s what gets recorded in the log:

[2024-08-02 10:48:17] (ERROR @ /www/core/components/fred/src/Traits/RenderResource.php : 426) [Fred | reversePreparedOutput] media-source-url =  | TV-value = assets/site/images//assets/site/images/test-stuff/inside-sample-2024.png

And when I save the page again without changing anything:

[2024-08-02 10:49:25] (ERROR @ /www/core/components/fred/src/Traits/RenderResource.php : 426) [Fred | reversePreparedOutput] media-source-url =  | TV-value = assets/site/images//assets/site/images/test-stuff/inside-sample-2024.png

Why is $url empty?

Did you add the line that logs the values after this line that sets $url?

If I put it after the line that sets $url, nothing gets recorded, because the script doesn’t get that far. This lines that set the URL are inside this:

if ($this->modx->loadClass($classKey)) {
which fails because the modFileMediaSource class can’t be loaded.

In the code after this line

add the following:

if ($classKey == 'modFileMediaSource') {
	$classKey = 'sources.modFileMediaSource';
}

Well, that partially fixes the problem. It doesn’t endlessly add the source path over and over again when saving, creating longer and longer strings each time. But, it still adds the media source path to the image. So, since the image is defined to use the “Images” media source, it should get saved in the DB as “test-stuff/inside-sample-2024.png”, but it’s getting saved as “/assets/site/images/test-stuff/inside-sample-2024.png”. And, note that it is adding an initial slash as well, which is not in the media source definition for the path (“assets/site/images”).

So when you select a new value for the image TV in the front-end and then click “Save”, what are the current values that get logged (for $url and $value)?

[2024-08-02 11:31:02] (ERROR @ /www/core/components/fred/src/Traits/RenderResource.php : 442) [Fred | reversePreparedOutput] media-source-url = assets/site/images/ | TV-value = /assets/site/images/test-stuff/inside-sample-2024.png

So the issue is now this:

 if (substr($value, 0, strlen($url)) === $url) {
   return substr($value, strlen($url));
 }

FRED is adding a leading slash to the TV path ($value), so “substr($value, 0, strlen($url))” does NOT match “$url”, and the truncated string does not get saved.

So the question is, where is FRED adding this initial slash?

Adding this at the top of the function fixes the issue:
if(substr($value, 0, 1) == '/') $value = substr($value, 1);

I think the / is the MODX base url.
You could add a line of code like this to make it work:

$url = $this->modx->getOption('base_url', null, MODX_BASE_URL) . $url; // prepend with MODX base url

after this line


Or instead of $properties = $source->getPropertyList(); and $properties['baseUrl'] use $bases = $source->getBases(); and $bases['urlAbsolute'].

Ah, right!

Yes, that works.

Spoke too soon. The first time I select an image TV and save it, the correct path gets saved. But for all subsequent page saves, the media source path gets added. That’s with your suggestion of adding this:
$url = $this->modx->getOption('base_url', null, MODX_BASE_URL) . $url;

When I use my fix:
if(substr($value, 0, 1) == '/') $value = substr($value, 1);
then the correct path gets saved, even on subsequent saves.