Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-06-23 21:24:52

Myusername
Member
Registered: 2019-12-12
Posts: 165

Multiple images in article via URL does not work

I don’t know if this is really a problem, or I’m thinking wrong. But in this link, right in the first sentence of the first paragraph it says the following: “The Article image field associates one or more (comma-separated) images with an article”. Okay, so we can associate multiple images with an article, right?

Later in the documentation, it says that we can associate the image in two ways: (1)by id (2)by absolute url.

The problem comes when I start using the absolute url, it just doesn’t work with commas. Is this expected?

If the “article_image” field is “1,2”, it works perfectly. The <txp:article_image/> tag will only display the first image, while the <txp:images autodetect/> tag will display both. So far it’s all right, I think.

But if the field is “1,/images/2.jpg”, it will display only the first image, ignoring the second.

If it is otherwise “/images/2.jpg,1”, it will display the field contents literally in the image src. The same happens if it is “/images/1.jpg,/images/2.jpg”.

I don’t know if it’s a problem or not, but it’s a little inconsistent, isn’t it?

Offline

#2 2021-06-24 09:16:21

etc
Developer
Registered: 2010-11-11
Posts: 5,132
Website GitHub

Re: Multiple images in article via URL does not work

Myusername wrote #330638:

I don’t know if it’s a problem or not, but it’s a little inconsistent, isn’t it?

Yes, it is… was. Please test 4.8.8 if you can. As an extra bonus, <txp:article_image /> accepts items range attribute (1 by default) that indicates which (by position) images you want to output. The valueless <txp:article_image range /> will output all article images. A better attribute name suggestions are welcome. Thanks!

Offline

#3 2021-06-26 05:35:37

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: Multiple images in article via URL does not work

I just tested it and I confess I’m very happy about it!

Some observations:

The article_image field appears to have a 255 character limit, which can be tricky if the user doesn’t like to host the images. I only added 4 url’s of images hosted on pixabay, and that was enough for the last image to have the url cut off.

I noticed that the range attribute accepts multiple values:

<txp:article_image range="1,2,3,4,5"/>

A limit attribute would be interesting too, just a suggestion:

<txp:article_image range limit="5"/>

Anyway, it’s great.

Offline

#4 2021-06-26 07:35:21

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

Re: Multiple images in article via URL does not work

Yes, the article image field is small primarily because it was only ever intended to store one image ID or one URL. Then we expanded it to permit multiples and the storage didn’t increase to catch up.

I don’t want to make it a Text field particularly because most users still only use it for a single ID or a list of them, and you can get a fair few in there (especially with ID ranges now) before running out of space. But we could entertain a bigger varchar up to, say, 1024 perhaps.

As far as limit is concerned: that’s not traditionally what the article_image field is designed for. It’s a single tag, just like <txp:image> to output the contents of the article_image field as an <img> tag. If you want to start doing that kind of thing, wrap it like this:

<txp:if_article_image>
   <txp:images limit="5">
      <txp:article_image />
   </txp:images>
</txp:if_article_image>

By default, <txp:images> uses the article image field to base its list on as first priority (see the auto_detect attribute). You need the conditional wrapper because otherwise, if the field is empty, it’d consider all images in the database.


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 2021-06-26 08:25:40

etc
Developer
Registered: 2010-11-11
Posts: 5,132
Website GitHub

Re: Multiple images in article via URL does not work

Myusername wrote #330705:

A limit attribute would be interesting too, just a suggestion:

<txp:article_image range limit="5"/>...

How would it be different from <txp:article_image range="1-5" />, assuming all images exist?

There is still some work to do (negative ranges, query optimizations), but further enhancements would turn <txp:article_image /> into a <txp:images /> clone.

Offline

#6 2021-06-26 09:41:36

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: Multiple images in article via URL does not work

etc wrote #330710:

How would it be different from <txp:article_image range="1-5" />, assuming all images exist?

Well, if that means you’re going to display from image 1 to image 5, then that’s the idea.

There is still some work to do (negative ranges, query optimizations), but further enhancements would turn <txp:article_image /> into a <txp:images /> clone.

I understand your thinking, but I disagree a bit. As far as I know, <txp:images /> has the power to work with every image in the database. <txp:article_image/> works only with the images in the article and can only be used inside a <txp:article/> or <txp:article_custom/>. Not forgetting too, that users can choose to use an external url. So I think this tag has a duty to work well with all these formats, even when used at the same time.

Also, by using the <txp:images /> tag, as Stef suggested, the same inconsistency I mentioned in the topic would resurface. Something like this “4,pixabay.com/5.jpg” (imagining an external url) would result in just one image being displayed. Which in my opinion doesn’t seem to be a problem for the tag <txp:images/>, since the function of this tag is to work with images from the database, not external url’s.

In my view of things, if you want to show multiple images of an article, this would be simple and correct:

<txp:article_image range="1-5" />

And not like this:

<txp:if_article_image>
   <txp:images limit="5">
      <txp:article_image />
   </txp:images>
</txp:if_article_image>

However, this is just an opinion.

Last edited by Myusername (2021-06-26 09:48:21)

Offline

#7 2021-06-26 14:34:44

etc
Developer
Registered: 2010-11-11
Posts: 5,132
Website GitHub

Re: Multiple images in article via URL does not work

Myusername wrote #330712:

Well, if that means you’re going to display from image 1 to image 5, then that’s the idea.

Yes, that’s how it works now. Negative ranges are possible too, so

<txp:article_image range="2 - -1" />

will output all images from the 2nd to the last one. And <txp:image limit="3" offset="1" /> will be mapped to <txp:article_image range="2-4" /> as expected.

Offline

#8 2021-06-26 18:14:00

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: Multiple images in article via URL does not work

etc wrote #330731:

Yes, that’s how it works now. Negative ranges are possible too, so

That is great. It will definitely be very useful. Thanks.

Last edited by Myusername (2021-06-26 18:14:23)

Offline

#9 2021-07-04 10:28:58

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: Multiple images in article via URL does not work

Assuming I have the image field like this: 1,2,/image.jpeg. There are three images in my article. Two ID’s and an external URL.

If I want to get the URL of the first image to a meta tag, for example, is there a native way to do this? Remembering that the image can be an ID or URL.

Offline

#10 2021-07-05 08:59:28

etc
Developer
Registered: 2010-11-11
Posts: 5,132
Website GitHub

Re: Multiple images in article via URL does not work

Myusername wrote #330866:

If I want to get the URL of the first image to a meta tag, for example, is there a native way to do this? Remembering that the image can be an ID or URL.

Kind of:

<txp:article_image range="1" trim='/^.+src="([^"]*)".+$/' replace="$1" />

Offline

#11 2021-07-05 11:27:03

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: Multiple images in article via URL does not work

I hadn’t thought about it. Thank you for your help

Offline

Board footer

Powered by FluxBB