Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-11-20 02:52:47

kevinpotts
Member
From: Kansas City
Registered: 2004-12-07
Posts: 364
Website

[howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Fair warning: I am a HACK. There is a good chance that a real programmer might improve this script. I just roll with what works.

This might be potentially useful to someone. The scenario: I have a client who wants to add all kinds of image galleries to their site. I have set up one image category for each gallery, so in the future they can add a new category and then add their images to that category. Pretty basic stuff. However, each gallery is associated with a single page on their site, so the gallery is called with each individual article. The most logical thing to do would be to add a custom field that allows them to simply type in the category name of the gallery they want to appear.

But then I thought: why do it the easy way when I can do it the hard way?

So here is a quick and dirty solution for dynamically pulling in a list of specific categories to populate a custom field dropdown.

1. Download and install glz_custom_fields

Pretty self-explanatory. And pay gerhard; he worked hard on that thing.

2. Build a custom script

Gerhard supplies a well-documented custom script called my_images.php that can serve as a template for these types of things. All I did was change a few things around. So, copy and paste this script into a file called list_categories.php.

<?php
function list_categories($custom_field, $custom_id, $custom_value) {
    $categories = array();
    $query = safe_rows('name, title', 'txp_category','1=1');
    foreach ($query as $category)
        $categories[$category['name']] = $category['title'];
    return glz_selectInput($custom_field, $custom_id, $categories, $custom_value, "");
}
?>

This will pull all categories. If you want to dictate a parent category, you need to add a WHERE clause to the safe_rows() function. In my case, I created a parent category called “galleries”, so I changed this:

$query = safe_rows('name, title', 'txp_category');

to this:

$query = safe_rows('name, title', 'txp_category', 'parent like "galleries"');

If you want to pull all categories for articles, images, links or files, you have to use the “type” column from the database, not the “parent” column. Example:

$query = safe_rows('name, title', 'txp_category', 'type like "image"');

That’s it. Pretty simple. Now copy list_categories.php to your server.

3. Add custom script to glz_custom_fields

Once glz_custom_fields is installed, go to Extensions > Custom Fields. Edit one of your un-used 1-10 custom fields, or choose to add a new one. Give it an appropriate name (in my case “Gallery”), and select “Custom Script” from the Type dropdown. In the Value field, add the complete path to the script, like so:

/absolute/path/to/scripts/list_categories.php

Click SAVE and you’re all set.

4. Put it to use

When you create or edit an article, you should see this dropdown appear with the appropriate categories. Remember, while the category titles are displaying, the category name (the one that counts) is the actual value for the option tag. So this is how I used it in my article form:

<txp:if_custom_field name="Gallery">
<ul id="photos" class="galleryview">
   <txp:smd_gallery form="gallery_item" category='<txp:custom_field name="Gallery" />' />
</ul>
</txp:if_custom_field>

(In my case, smd_gallery is doing all the heavy lifting for the actual gallery building.)

Have fun.

EDIT: Updated safe_rows() call in initial example for pulling all values.

Last edited by kevinpotts (2010-11-21 01:46:28)

Offline

#2 2010-11-20 04:54:25

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,394
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

So, are you sending this in to TXP Tips…?!

Offline

#3 2010-11-20 04:58:42

kevinpotts
Member
From: Kansas City
Registered: 2004-12-07
Posts: 364
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Grab it and repost it. Unless there are smarter people who can improve it.

To be clear: written under a WTFPL license. :)

Last edited by kevinpotts (2010-11-20 11:58:51)

Offline

#4 2010-11-20 05:00:04

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,394
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Thanks Kevin. Let’s see if anyone chips in before publishing then.

Offline

#5 2010-11-20 07:14:43

jpdupont
Member
From: Virton (BE)
Registered: 2004-10-01
Posts: 752
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Thanks Kevin !
Very useful for all my sites : as you, I allow galery linking on each article with a “image category” custom field !

Offline

#6 2010-11-20 13:38:35

jpdupont
Member
From: Virton (BE)
Registered: 2004-10-01
Posts: 752
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Kevin,

$query = safe_rows(‘name, title’, ‘txp_category’);

I have a php warning if I don’t set the WHERE clause in the safe_rows().

Offline

#7 2010-11-20 17:00:31

kevinpotts
Member
From: Kansas City
Registered: 2004-12-07
Posts: 364
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

jpdupont wrote:

I have a php warning if I don’t set the WHERE clause in the safe_rows().

Try this:

$query = safe_rows('name, title', 'txp_category', '1=1');

Offline

#8 2010-11-20 17:34:38

jpdupont
Member
From: Virton (BE)
Registered: 2004-10-01
Posts: 752
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Yes, I know :-))))
… It was just to correct the tutorial …
Your script is ok and works fine on my site … Thanks !!!
And big thanks also to Gerhard !

Offline

#9 2010-11-26 03:01:00

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,394
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Just posted this tutorial by Kevin over on TXP Tips. Thanks Kevin!

Offline

#10 2010-12-15 11:38:48

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 858
Website

Re: [howto] Use List of Categories as Values for glz_custom_field Dropdown Options

Just one thing from me…. not a biggy – but using it without assigning a parent category, it always shows the ‘root’ category

Is there anyway to prevent this without creating a parent – or is that how it works?

Ps – I’ve already commented on txptips – but I find this pure genious – saves me so coding time, I have articles which I can now assign image and file categories… Clients love it because it goes with their way of thinking

Great Code / hack – whatever you want to call it – 10/10 from me :)

Offline

Board footer

Powered by FluxBB