Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-07-17 20:48:32

giz
Member
From: New Zealand
Registered: 2004-07-26
Posts: 259
Website

Find inline image shortcodes used in the body field

On a large site I have product articles which reference project image ids via a custom field.

Elsewhere there are news articles which reference the same images, but these are embedded inline in the body field using a shortcode eg. <txp::figure id="1,2,3" class="etc" />.

How can I display images which are common to both sets?

Note: this is for admin purposes and won’t be exposed to the public, so a quick and dirty solution will suit.

Offline

#2 2023-07-18 03:03:01

agovella
Member
From: Houston, TX
Registered: 2005-05-01
Posts: 65
Website Twitter

Re: Find inline image shortcodes used in the body field

What do you mean by, “How can I display images which are common to both sets?”

Offline

#3 2023-07-18 06:46:45

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,596
Website

Re: Find inline image shortcodes used in the body field

You’ll need to use <txp:php> to get at the raw article fields. The global variable $thisarticle stores the body_html field and the article_image. Shortcodes are not pre-processed so appear as-is in the textiled body field. That makes it possible to find them using a regex to get at the id variables in your inline shortcode tags. Something like this:

<txp:php>
global $thisarticle, $variable;
// Get all matches for id attribute of txp::figure shortcodes in body
preg_match_all('/<txp::figure id="(.+?)"/', $thisarticle['body'], $article_imgs_inline);

// Separate out any comma-separated values into individual values
$article_imgs_inline = explode(",", implode(",", $article_imgs_inline[1]));

// Get (any comma-separated) value(s) from article_image field
$article_imgs_field = explode(",", $thisarticle['article_image']);

// Merge the two, deduplicate and remove empty values
$article_imgs = array_filter(array_unique(array_merge($article_imgs_inline, $article_imgs_field)));

// Sort naturally (numerically but with 1, 2, … 10, 11, 12 …)
natsort($article_imgs);

// Store comma-separated string as a txp variable
$variable['article_imgs'] = implode(",", $article_imgs);
</txp:php>

That uses a regex to retrieve the id field of all instances of txp::figure id="…" in the body field. Then it collects the article image field. Any values with multiple id numbers (as in your example) are gathered as individual items. The two arrays are merged, any duplicate id numbers are filtered out, likewise empty items (e.g. when there is no article image), the list is sorted numerically. That is turned back into a comma-separated string and stored as a txp variable, which you can get at like this:

<txp:variable name="article_imgs" />

This you can plug into txp:images to output those images. Note you’ll need to check that the variable is not empty to prevent txp:images outputting everything.

Some provisos: the above assumes you write your shortcode tags uniformly with the id attribute right after the shortcode name. Also that you only use id numbers or comma-separated id numbers in your article_image field. If your shortcode usage or article image fields are not consistent, you may need to make the regex more flexible (see this regex101) and build in extra safety checks (e.g. checking for integers) for the article_image field.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2023-07-18 12:12:51

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Find inline image shortcodes used in the body field

Neat, Jakob. If it makes it any more flexible and you wanted to register the preg_match_all function for use in tags, you should be able to access, and thus assign to variables, article fields via:

<custom::field name="body" />
<custom::field name="article_image" />

etc…

That should (untested) also mean that:

<if::custom_field name="article_image">

perhaps with the match="pattern" attribute might give some avenues of exploration.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2023-07-18 18:03:19

giz
Member
From: New Zealand
Registered: 2004-07-26
Posts: 259
Website

Re: Find inline image shortcodes used in the body field

Thank you both, very much.

You’ve helped fill a(nother) gap in my understanding of how Textpattern works!

Offline

Board footer

Powered by FluxBB