Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-04-02 07:39:11

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

[resolved] Test if custom field content is a list of values

Hi,
I would like to test a custom field to know if it contains several values but the following tag and attributes do not work, probably because “,” is not a valid value as it used to be a separator…
Is there something I can do to make it work? (I know smd_if but I’m looking for native solution, if there is one).

<txp:if_custom_field name="Image(s)" match="any" value=",">
  Do something…
</txp:if_custom_field>

Thanks

Last edited by NicolasGraph (2015-04-02 08:28:38)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#2 2015-04-02 07:49:52

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,172
Website GitHub Mastodon Twitter

Re: [resolved] Test if custom field content is a list of values

I am wandering if the brackets in the custom field’s name is affecting the result also it could be amended to

<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value=",">
  Do something…
</txp:if_variable>

The above will/should work only if there is more thank one image as there should be no comma if there is just one. As such the code could be amended to

<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:else />
  Do something…
</txp:if_variable>

A combination of the two codes could be used to detect if there is 1 or more images

<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:hide>no images</txp:hide>
<txp:else />
<txp:hide>there is at least one image</txp:hide>
<txp:variable name="alotofimages"><txp:variable name="cfimages" /></txp:variable>
<txp:if_variable name="alotofimages" value=",">
  You have more than one image…
<txp:else />
You have just one image
</txp:if_variable>
</txp:if_variable>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2015-04-02 08:07:54

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: [resolved] Test if custom field content is a list of values

colak wrote #289674:

I am wandering if the brackets in the custom field’s name is affecting the result

I tried with “Images” but it doesn’t change anything.

also it could be amended to

<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>...

Finally, this will/should work

Unfortunatly it does not, I think because <if_variable name="is_list" value=","> is the same as <if_custom_field name="images" match="exact" value","> and not match="any" which I would need.


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#4 2015-04-02 08:13:08

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

Re: [resolved] Test if custom field content is a list of values

NicolasGraph wrote #289673:

because “,” is not a valid value as it used to be a separator

Correct. But you can harness the power of regexes to do what you want instead:

<txp:if_custom_field name="Images(s)" match="pattern" value="[\,]+">
  Do something…
<txp:else />
  Don't
</txp:if_custom_field>

Essentially that’s “does the field contain one or more commas?”

Edit: it won’t cater for the situation where your field contains only a single comma, so if you want to ensure that your field actually contains a list of items including at least one comma, you’ll need to modify the pattern a tiny bit.

Last edited by Bloke (2015-04-02 08:14:55)


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 2015-04-02 08:19:42

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,172
Website GitHub Mastodon Twitter

Re: [resolved] Test if custom field content is a list of values

Hi Nicolas,

Looking at the if_custom_field specs I guess you are right.

I wonder if you separated the ids of the images with a dash and then replace those values using rah_replace would do the job. I know you said no plugins and I hope somebody will come up for a solution but here is an untested code

<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:hide>no images</txp:hide>
<txp:else />
<txp:hide>there is at least one image</txp:hide>
<txp:variable name="islist" match="any"><txp:variable name="cfimages" /></txp:variable>
<txp:if_variable name="islist" value="-">
<txp:rah_replace from="-" to=",">
<txp:images id='<txp:variable name="islist" />'><txp:image /></txp:images>
</txp:rah_replace>
<txp:else />
You have just one image
</txp:if_variable>
</txp:if_variable>

> Edit: Stef to the rescue

Last edited by colak (2015-04-02 08:23:09)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2015-04-02 08:20:20

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: [resolved] Test if custom field content is a list of values

Bloke wrote #289676:

Correct. But you can harness the power of regexes to do what you want instead:

<txp:if_custom_field name="Images(s)" match="pattern" value="[\,]+">...

Essentially that’s “does the field contain one or more commas?”

Edit: it won’t cater for the situation where your field contains only a single comma, so if you want to ensure that your field actually contains a list of items including at least one comma, you’ll need to modify the pattern a tiny bit.

Thank you Stef! I just removed + and it works with only one comma.

Last edited by NicolasGraph (2015-04-02 08:22:39)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

Board footer

Powered by FluxBB