2

My MODX site needs to grab the first image from all pages for Open Graph Meta tags, which will be plugged into the Head chunk for all templates.

The problem with this is that not all images are located in the content part of a page. Some are located inside Chunks and others inside TVs. (Finding an image tag from the content is not an issue.)

It might be possible to get all Chunks and TVs and loop through their values to check for images.

But is there a way to get the <body> contents of the resource?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
MCSharp
  • 1,068
  • 1
  • 14
  • 37

1 Answers1

0

Probably several ways, you can try writing a plugin to parse through the entire content of a page, looks like the OnWebPageComplete event may be the one to use (take a look at the different events to see if one is more appropriate}

You can try and grab the resource from the cache, keeping in mind that any chnks/snippets/TVs called in the page un-cached will not show up in the resource cache file.

You can get a list of TVs once you have loaded a resource & then use getTVValue to get the value.

If you have an image in a chunk, getChunk might work [might, I've never tried to use it that way] to get it's contents but I would image that the image in a chunk would come from a TV ~ so you should be able to retrieve it with getTVValue.

You could also just setup a TV for the OpenGraph image and explicitly set it on a page by page basis.

Probably writing a plugin & some regex is going to be the least painful way of going about it.

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • Thanks. I have a TV setup for ograph for the blog part of the site. The issue is I'd like to get the image from a single snippet (for the same $head chunk and to prevent duplicate values). Getting an image from a specific template isn't an issue. I'd need something that would check all Chunks for tags and all TVs for image paths. Big issue is if the input is just Text rather than image. I think grabbing the body contents before page load is the way to go. How can I grab the html output OnWebPageComplete? – MCSharp Apr 13 '15 at 20:44
  • Hmmm, on reflection you do probably want to get the rendered resource from the cache. Otherwise no matter what method you use, you would be rendering the resource again. – Sean Kimball Apr 14 '15 at 01:29