Community

How to get Full Image URL in all resources?

I am building an app with Modx REST Api with Migx as main backed CMPs and I am getting images url as
assets/image.jpg
I want this to be full url so my app can get image from source url like https://sitename.com/assets/image.jpg
Because im not using any snippet or trick and only using simple image Tv for uploading image, I dont know how to get this.

So is there any system wise setting in Modx or Migx to get the full url automatically for every resources?

You would want to prepend the site_url at the beginning of the image url.

e.g.

$fullImageUrl = $this->modx->getOption('site_url').$imageUri;

Or in a Tpl chunk, prepend [[++site_url]]. Be careful not to put a closing slash after the tag, since the site_url already has one.

I can get Image url if I put it like this :
$data[‘FeaturedImage’] = $this->modx->getOption(‘site_url’) . ‘assets/dir-images/hardanews/’ . $object->hardanewsphoto;

But I am not getting full url under the content. Or I really dont know where to put your code. I tried on my rest call php but not getting success. Where to put this code ?

Here is my url for better explanation - https://digitalharda.com/rest/digitalhardanews

Sir I am not using any tpl chunk. Its just Migx CMP and textarea Tv. Content is being generated by richtext editor and I cant use site_url inside it.
Here is my json response for better explanation in which we can see image is between content.
https://digitalharda.com/rest/digitalhardanews
See : NewsContent

In your link it shows the featured image is being returned as:
https:\/\/digitalharda.com\/assets\/dir-images\/hardanews\/jasmin-tsvety-romashki-vederko-zhasmin-trava.jpg

Is this not correct?

It is generated in feature image because we are using prefix of site url.
But look inside the NewsContent… There I put an image. Its url is not full.
Check this image here that image should show but it eill not because url is not full.

It is the content of NewsContent in json.

So why don’t you prefix it there too?

See the screenshot!
The image is inside the content. How can I use prefix there ? And this is not a single article. Users will post hundreds of articles so its not the solution to add prefix by them every time.
That’s why I want full image url.

You should have mentioned the link is being created by TinyMCE…

You might find this helpful: https://forums.modx.com/thread/97987/issue-with-absolute-paths

Also: https://docs.modx.com/revolution/2.x/administering-your-site/settings/system-settings/link_tag_scheme

Sorry I just mentioned richtext editor. I should be more specific.
&scheme=`full is the solution but how and where to use this since im not using any snippet call…
I am also trying TinyMCE Wrapper. Aas in that link;s second last post, a user wrote about that.

TinyMCE has the option to let you edit the raw HTML (you may have to enable that feature), which would let you add the site_url tag.

If you don’t mind a little PHP coding, there is also a method in the EmailResource extra that converts URLs to full URLs. I think it could be used in a plugin attached to OnDocFormSave to add the site_url.

Actually, a very short custom plugin could probably do the same thing.

Actually TinyMCEWrapper is doing the trick in other resources but not in Migx.
I searched for it and found that Migx has several tv for RTEs.
Here we can see in this pic -
Annotation%202019-08-13%20232155

So the solution is TinyMCEWrapper Extra. But the problem is that its not working with Migx CMP.

@bruno17 what do you suggest me? Did I missed any settings in Migx ?

(ERROR in modProcessor::run @ /modxurl/core/model/modx/modprocessor.class.php : 177) Flat file processor support, used for action mgr/migxdb/getList with path modxurl/core/components/migx/processors/mgr/migxdb/getlist.php, is deprecated since version 2.7.0.

There’s probably a much easier way, but I think this plugin would do it (attached to OnDocFormPrerender).

$output = &$modx->resource->_output;
$pattern = '/(<img src\s*=\s*[\'"])([^h][^>"]*)/';
$output =  preg_replace($pattern, '$1' . $modx->getOption('site_url') . '$2', $output);
return;

I’ve done limited testing on it and it seems to work. At worst, it will mess up something on the screen, but it can’t cause any permanent harm.

Unfortunately, plugin is not working with me. Selected OnDocFormPrerender also.
tinyMCEWrapper generating full url of Image.

See this link - https://digitalharda.com/

But I am unable to use it on Migx form tab. :frowning:

The plugin is meant to produce the full URL in the front end of the site only (which you’ve apparently solved). It wouldn’t work in the Manager without modification, and then it would only work on Resource fields. It wouldn’t work for MIGX because its data is held in a TV.

If there’s only one MIGX TV, it might be possible to write a similar plugin connected to OnDocFormSave to re-write that TV with full URLs in the DB, but it would be tricky, and you wouldn’t see the image until you reloaded the page.

It might help to put MIGX in the subject line of your post.

Ok so I searched and found the solution.
You have to create a chunk with name - TinymceWrapperMIGX
and then paste this -

tinymce.init({
  mode: "exact",
  elements: "tv[[+tv_id]]",
  [[$TinymceWrapperCommonCode]]
  plugins: "autoresize,preview,paste,contextmenu,image,wordcount,fullscreen,code,link,charmap,searchreplace,textpattern,emoticons,insertdatetime",
  paste_word_valid_elements: "a,div,b,strong,i,em,h1,h2,h3,p,blockquote,ol,ul,pre",
  valid_elements: "iframe[*],object[*],audio[*],-span[!title|!class<test test2],a[href|target|class|rel|title|data-ajax|data-iframe],strong,b,-p[class<text-align-left?text-align-center?text-align-right],br,-h1[class|data-ajax|data-iframe],-h2[class|data-ajax|data-iframe],-h3[class|data-ajax|data-iframe],-img[!src|!alt|!class=round_img|data-ajax|data-iframe],em,-blockquote,pre[class],-ol,-ul,-li,-code[class]",
  valid_children: "-li[ul],-li[ol],-li[div],-strong[*],-em[*],-h1[*],-h2[*],-h3[*],-a[strong|em|h1|h2|h3|p|div],blockquote[p|ol|ul],pre,div",
  resize: true,
  autoresize_min_height: 100,
  autoresize_max_height: 400,
  toolbar: "newdocument | fullscreen preview | undo redo | blockquote | bold | italic | aligncenter | bullist numlist | link unlink | image | styleselect | charmap emoticons insertdatetime | searchreplace",
  contextmenu: "removeformat | link | image | code",
  setup: function(editor) {
    editor.on('mouseleave', function(evt) {
      tinyMCE.activeEditor.save();
    });
  }
});

Of course you have to install the TinyMceWrapper Extra first. :wink: