Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-02-19 17:57:58

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Custom field value list growing by user entry?

I am wondering, how i could attempt to establish having a custom field, which reads what i enter and autocompletes from an alphabetical list of values/tags/words that have been entered before, if possible.
If that word i look for doesn’t exist already the custom field would instead offer to save my new entry to the database of values entered before.

Any better music site i know (discogs.com, Bandcamp etc.) offers this kind of feature, where the database learns from user entry, but also presents, what was entered before as possible values alphabetically.

How would you approach this in Textpattern?

Thanx for any insights in advance.

Last edited by jayrope (2018-02-19 17:58:12)


A hole turned upside down is a dome, when there’s also gravity.

Offline

#2 2018-02-19 18:05:39

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

Re: Custom field value list growing by user entry?

glz_custom_fields has some script capabilities that should allow you to offer this functionality.

I would warn you, though, accepting user input and adding it to a database will cause you pain later. Mis-spellings and malicious intent notwithstanding, one man’s Country is another man’s AOR. Just sayin’.


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

#3 2018-02-19 18:53:12

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: Custom field value list growing by user entry?

AOR = area of responsibility i suppose, or do you mean adult oriented rock music?
;)
Hmm, yeah. I am not too excited about implementing such things into a multiple user site aswell.
However, if i had a preedited list of values/words/terms, which are to be used from a dropdown menu, in fact a concealed custom field: How do i get all the preedited standard values into the database at first?


A hole turned upside down is a dome, when there’s also gravity.

Offline

#4 2018-02-19 19:34:39

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

Re: Custom field value list growing by user entry?

jayrope wrote #309249:

if i had a preedited list of values/words/terms, which are to be used from a dropdown menu, in fact a concealed custom field: How do i get all the preedited standard values into the database at first?

What you ideally need to do is set up a select list of options in glz_cf when you set up the input control itself. Give it a starter list of terms you think might be useful to people to kick things off.

What I hadn’t factored in here, due to my hasty reply, is that your CF may be populated from the public site by visitors. Is that the case? I think (but may be wrong) that custom scripts only work on the admin side, since CFs are usually designed to be one-way storage: input on admin side -> display on public site.

What I was going to suggest was to use the script to take the defined contents of your select list – your starter terms – and then combine them with a quick query across all submitted articles to extract the custom field info supplied by visitors. Merge the results and sort them alphabetically, then you can offer them all as search options.

Over time, you or the client could take the list and clean it up a bit, copy any good user-supplied terms and put them in the standard list; just to keep the thing under control.

So this brings me back to the front side. Are you using it for data entry? If so, when users submit a new value, what are you doing with the data? Do you save the custom field as part of an article via something like mem_form? Or are you simply intending to keep it completely separate from articles; just a standalone list of terms that act as a dropdown helper? What’s the workflow goal?


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 2018-02-19 21:01:33

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

Re: Custom field value list growing by user entry?

Bloke wrote #309250:

What I was going to suggest was to use the script to take the defined contents of your select list – your starter terms – and then combine them with a quick query across all submitted articles to extract the custom field info supplied by visitors. Merge the results and sort them alphabetically, then you can offer them all as search options.

Just to expand on this…

You mention your site is a multi-user site. If that means your users all access this from the backend, then Stef’s proposal sounds good. glz_custom_fields has a custom script option which allows you to specify the script to be used for a particular custom_field. You first need to specify the path to your custom scripts in the prefs, then save your custom script as a .php file in that folder. Then you can choose the script you need.

The following is just an example of such a script not a specific answer to your question, but it should give you some pointers. This example glz custom script was used to assign the project leader to a project from a drop-down list of staff. It saved the id number of the staffer’s article into the custom field and the page template then pulled in the contact details from that article for displaying alongside the project:

<?php

// this function will return all articles from the 'people' section in the textpattern table in a select
function related_people($custom_field, $custom_id, $custom_value) {
  $articles = array();
  $query = safe_rows("id, title", "textpattern", "1=1 AND status='4' AND section='people' ORDER BY Title asc");

  if (empty($query)) {
    return glz_selectInput($custom_field, $custom_id, array('' => '' ), $custom_value, "0", 1);
  } else {
    foreach ($query as $article)
      $articles[$article['id']] = $article['title'];

    // because we can't set a {default} value from Extensions > Custom Fields, we're hard-coding it here
    return glz_selectInput($custom_field, $custom_id, $articles, $custom_value, "0", 1);
  }
}

?>

As some projects had several team leaders, I also loaded a jquery script (bsmselect, example) that transformed the regular select into a multi-select chooser.

As you can see, it’s pretty simple. You could probably do something similar with a suitable script that serves your needs.

  1. First find a suitable input widget (javascript/jquery) that ideally transforms a regular select or multi-select into an input widget that offers you a choice but also allows you enter your own value. I think there are various tag-chooser scripts out there that do that.
  2. Adapt the above script to retrieve the values from that custom_field that have already been entered into an array
  3. Add a list of your own popular pre-defined values to that array
  4. Run the lot through array_unique to strip out duplicate values
  5. Use the result to construct your select / multi-select html on your admin-side page.

Again, that’s only relevant if your users have access to the back-end.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2018-02-27 21:35:53

jayrope
Plugin Author
From: Berlin
Registered: 2006-07-06
Posts: 687
Website Mastodon

Re: Custom field value list growing by user entry?

Jakob, Stef, Dankeschön for the examples.

My users have access to backend, most of whom i will have to keep away from the intestines. smd_user_manager and all…

I’ll have to work myself through your suggestions. Will come back and report/ask again.

Thank you for your expertise!


A hole turned upside down is a dome, when there’s also gravity.

Offline

Board footer

Powered by FluxBB