Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Check for duplicate values in custom fields
How can I check for duplicates in custom_fields while saving article?
Last edited by wornout (2012-07-18 12:42:18)
Offline
Re: Check for duplicate values in custom fields
Maybe I was too generic.
I’m asking for possibility to check if a custom field can has a unique value because is used for product code.
If already exist Textpattern alerts user.
Thanks in advance for every suggestions!
Offline
Re: Check for duplicate values in custom fields
On the admin-side? On Textpattern 4.5 and current dev branch you can use the new asynchronous API and messager to fire Textpattern’s native alerts. 4.5 also offers few callback helpful callback events for this purpose. Along the lines of:
/**
* Hook function abc_product_id to callback events article_posted and article_saved
*/
register_callback('abc_product_id', 'article_posted');
register_callback('abc_product_id', 'article_saved');
/**
* Checks that product_id is unique and fires and message if it isn't
*/
function abc_product_id($event, $step, $pre, $data) {
global $theme;
/*
If no duplicates end here. Checks "custom_1". Note: it will break when/if multiple-custom field
code lands.
*/
if(safe_count('textpattern', "custom_1='".doSlash($data['custom_1'])."'") <= 1) {
return;
}
/*
If there is more than one article with the same custom field value, issue a warning.
Uses Textpattern's native messages.
*/
send_script_response(
$theme->announce_async(array(
gTxt('abc_duplicate_product_id'), E_WARNING
))
);
}
Also, v4.5.0 offers way to validate values, which would outright block saving. There is an article_validate
event which can be used to add constraints. It requires bit more code so, I won’t write it on the spot on this web form. You would pretty much create new class that extends Constraint
, and add it to the stack.
If it needs to work in TXP 4.4.0, well dunno. It has none of the above. I suppose you could fire hackish a JavaScript alert dialog after the form has been sent and saved. Well, you can know if the article has been saved, but fire a warning based on raw post data on “article” event. Along the lines:
register_callback('abc_product_id', 'article');
function abc_product_id() {
if(!ps('custom_1') || safe_count('textpattern', "custom_1='".doSlash(ps('custom_1'))."'") <= 1) {
return;
}
echo script_js('alert("Dublicate product ID")');
}
Just note that the above, while it works in TXP 4.4.1, it will not work in TXP 4.5 or any other future releases so to speak correctly. Plus it’s hackish. Heck, it doesn’t even know if an article was saved or not.
Please note that both snippets are just examples and untested too. May or may not work. Both expect that you have existing PHP knowledge, or someone that can deploy them for you. If future compatibility is wanted, then both snippets could be used together, well, potentially.
Last edited by Gocom (2012-07-18 13:54:53)
Offline
Re: Check for duplicate values in custom fields
There is another not very satisfactory options: write a snippet of rah_external_output
that checks whether a given custom field value exists and attach an appropriate ajax call to submit
event of the edit form. This will act before the article is saved, but relies on client-side javascript and hence can be circumvented.
Offline
Re: Check for duplicate values in custom fields
And the most quick and dirty solution would be to create a unique index on your custom field :)
Offline