Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jerode 44 posts 159 karma points
    Aug 26, 2015 @ 15:00
    Jerode
    0

    RichTextEditor and Media URL

    Perhaps it is just a configuration value that I am not aware of, but here is my question for the community:

    When using the Media Picker in the RTE (RichText Editor), I add an image and most is excellent. The only thing that is not, I need fully qualified URLs because the consumer of the content may not be the Umbraco site.

    Why you may ask? The Umbraco site serves as the backend API for content that is consumed by multiple sites and delivered using an UmbracoApiController. I could do a search for anything that begins with /media and prepend it with my domain before returning it to the caller, but that is the approach I will take if there is not other options.

    Example

    Body = item.GetProperty<string>("resourceBody").Replace("/media/", $"{Request.RequestUri.GetLeftPart(UriPartial.Authority)}/media/");
    

    I already needed to do Request.RequestUri.GetLeftPart(UriPartial.Authority) to append it to an upload property URL but that was easy to get to because it is a single property and not the content of the RTE. It seems globally there should be a setting for fully qualified URLs.

  • Mark Bowser 273 posts 860 karma points c-trib
    Aug 27, 2015 @ 16:18
    Mark Bowser
    2

    Interesting. One approach could be to attach to the "Saved" event on ApplicationStarted and update the media paths. Not sure if this is the ideal solution, but it should be workable.

    Something like this should get you close. The hard part will be identifying which properties are RichTextEditors.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Saved += FixRTEMediaUrls;
    }
    
    private static void FixRTEMediaUrls(IContentService cs, SaveEventArgs<IContent> args)
    {
        foreach (var node in args.SavedEntities)
        {
            // Find RTE properties. 
            // ...
    
            // Append the domain to the media paths
            // ...
    
            // save without raising more events. We don't want an infinite loop!
            cs.Save(node, raiseEvents: false);
        }
    }
    

    I'm not sure how or even if its possible, at the moment, to get properties by their data type, but these might be some clues to the puzzle.

            // This might get you the Umbraco.TinyMCEv3 property types.
            var propertyTypes = node.PropertyTypes.Where(pt => pt.DataTypeId == new Guid("5e9b75ae-face-41c8-b47e-5f4b0fd82f83"));
    

    Or you could always just get the properties by alias.

            var properties = node.Properties.Where(p => p.Alias == "bodyText" || p.Alias == "otherRTEProperty");
    

    Hope this helps.

  • Jerode 44 posts 159 karma points
    Aug 27, 2015 @ 16:58
    Jerode
    0

    This is an interesting approach and would work better than my backup replace method because I would then take the performance hit on the save instead of read of each item. Thank you, I like this recommendation and would use it over my replace backup.

    Still wondering if configuration or some implemented in the base Umbraco source is available.

    P.S. You may have helped point me in a direction for an unrelated post so thank you for your nice examples.

Please Sign in or register to post replies

Write your reply to:

Draft