Jump to content

Fieldtype Select External Option


kixe
 Share

Recommended Posts

Awesome update!

I just played around with automatically detecting all available and compatible Inputfield types: https://github.com/adrianbj/FieldtypeSelectExtOption

It works great with all the standard ones as well as other 3rd party ones like InputfieldChosenSelect

I was hoping it might also with with Autocomplete and SelectMultipleTransfer but autocomplete doesn't have an AddOption method and InputfieldSelectMultipleTransfer doesn't style properly, but I haven't looked into fully to find out the issue.

I have restricted the Inputfieldtype options to exclude those which are a subclass of InputfieldPageListSelection (becaue these don't work at all), those that don't have an addOption method, and then specifically InputfieldSelectMultipleTransfer, although I'd like to figure out a better way of detecting the reason this one doesn't work so it could also match others than won't work.

If you want to take a look at my changes, please feel free to incorporate, or if you want, I can send you a PR. 

Perhaps it would be worthwhile adding a note under the Inputfield selector saying that there are no guarantees that all the Inputfields will work as expected and the user should test carefully.

  • Like 2
Link to comment
Share on other sites

quick look
InputfieldPageListSelect and Autocomplete are subclasses of Inputfield and not InputfieldSelect. Maybe its good to restrict the selectable Inputfields to all subclasses of InputfieldSelect, which includes the method addOption() too. Furthermore render() method should be hookable.

Everything else is more on the side of Inputfield Developers. :)

Thanks again

  • Like 1
Link to comment
Share on other sites

I took most parts of your code and implemented it. It works.
InputfieldSelectMultipleTransfer expects item-content class 'InputfieldSelectMultipleTransfer', which is not rendered. tested with InputfieldWrapper. It would be great if the developer could change this and make the module flexible like InputfieldChosenSelect. I like also install method in this one, which adds the module to InputfieldPage settings otherwise it should be done by hand.

Thanks a lot for your support.
 
 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Hi, I installed this module on 2.5.25 version and as soon as I tried to create a new field I got the following error. Any ideas?

Error: Using $this when not in object context (line 112 of /home/maloco15/public_html/site/modules/FieldtypeSelectExtOption/FieldtypeSelectExtOption.module)
This error message was shown because you are logged in as a Superuser. Error has been logged

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

You don't need the API for that. There is a filter option under the details tab. With the filter you can create a WHERE clause. Example: Lets say you have a database table with an inttype column 'gender' (0=male, 1=female). You want only females as selectable options. Go to the Filter settings select 'gender' for column, the equal operator and 1 as value. Done.

Link to comment
Share on other sites

Yes but I want to change the available options automatically depending on various conditions which change from time to time.

EDIT:

Maybe there's a way I can modify the module's filter method so I can change the filtering dynamically. Then when building a form I could setup the filter before calling getInputfield(Page $page, Field $field) and the user would see only those options.

Putting this at the top of the filter method is a good start:

		$attr = 'optionFilter_'.$field->name;
		if(!empty($page->$attr)){
			$field->filter_column = $page->$attr->filter_column;
			$field->filter_selector = $page->$attr->filter_selector;
			$field->filter_value = $page->$attr->filter_value;
		}
Link to comment
Share on other sites

If I understand you right you would like to render the field via API and manipulate some field properties (filter_column, filter_selector and filter_value).
Following code should work. (not tested)

$fieldtype = $modules->get('FieldtypeSelectExtOption');
$field = $fields->get(123); // 123 is the id of the field you have assigned to your page template

// set your filter options 
$field->filter_column = 'id';
$field->filter_selector = '<';
$field->filter_value = 3;

$inputfield = $fieldtype->getInputfield($page, $field);
echo $inputfield->render();
Link to comment
Share on other sites

Yes, that's pretty much what I did. At the time I thought I might like storing filter options as an attribute of the $page. We'll see.

This is working fine:

$fid = $fields->get($fieldname)->id;     //get the field id
$field = $fields->get($fid);    //this is what we need to set filter settings
(then like your example above)
 

Link to comment
Share on other sites

  • 1 month later...

@kixe: I'm seeing a potential issue with this module. Using this in combination with InputfieldSelect everything else works, but the inputfield won't display the selected option as selected, so saving the page again I always end up with the first available option selected  :)

InputfieldSelect checks the string value of field to determine if current item should get 'selected="selected"', and since the string value will always be "SelectExtOption" (name of the class), this won't work at all. I've solved this locally by adding a __toString() method to the SelectExtOption class:

    public function __toString() {
	return (string) $this->value;
    }

Is there something I'm missing here? :)

For the record, using latest dev version of ProcessWire (2.6.9).

Link to comment
Share on other sites

@teppo
I couldn't reproduce this using pw 2.6.10 and Modules Version 1.1.2.
The sleep value is always an integer. In the field settings you can only select integer columns with unique values to populate the option values of your selectfield.

Could you tell me some more details to reproduce your problem. Maybe an example of your database table (column settings) you took to populate the selectable options and field settings too? Thanks.

Link to comment
Share on other sites

@kixe: I'll try to set up a clean test case and/or a screencast later, probably easier to explain that way.

To summarise, it looks like we're talking about different things here, since sleepValue() shouldn't have anything to do with my issue. The problem isn't saving broken/malformed values to database (which, to my best knowledge, is where sleepValue() steps in), it's about InputfieldSelect not being able to add "selected='selected'" to the selected option, and the field value reverting to the value of the first option available is simply a result of that :)

Link to comment
Share on other sites

  • 1 month later...

I've got a small problem with this module. I created a new field in the backend with this new fieldtype, select a table and put the new field into the template. No problems. When I edit the page, I saw the values, I could select one or more and save the page. But the second time I want to edit the page, the choosen data won't be displayed as selected or choosen like in normal select fields. The choosen values stored correctly in the database, that's not the problem. Maybe someone got an idea to solve this? :)

Link to comment
Share on other sites

welcome dsdb,
some values are selected but not all? in this case check if each value of the column you have choosen for your option values is unique. If not, some options will be overwritten by the preceding same option value.

If this doesn't help, could you please post a part or example of your source database table? and or a screenshot of your field? Which settings did you choose?

Link to comment
Share on other sites

Hi kixe!

Here is the structure of my custom table:

CREATE TABLE IF NOT EXISTS `t_fsk` (
  `id_fsk` int(11) NOT NULL AUTO_INCREMENT,
  `f_fsk_value` int(11) NOT NULL,
  `f_fsk` varchar(200) NOT NULL,
  PRIMARY KEY (`id_fsk`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

I used the field "f_fsk_value" (values are; 1,2,3,4,5,6) as "option value" and the field "f_fsk" as "option label". My selection will stored correctly into

the database but when I try to edit the page a second time, the selection I made before isn't displayed. I tried the select and selectMulitple Option. Both of

them with the same result. May you have an idea to solve this problem?

  • Like 1
Link to comment
Share on other sites

@dsdb
thanks for reporting this. It looks similar to teppos problem, which I couldn't reproduce in my surrounding. I don't have the time now. I will have a deeper look asap.
If there is anybody out there. Help is welcome. :)

Link to comment
Share on other sites

Update to Version 1.1.3 (dev)
Download from github https://github.com/kixe/FieldtypeSelectExtOption/tree/dev

Changes

  • hook in Inputfield render() replaced by adding magic toString() method to SelectExtOption class (which helped teppo and dsdb) @Teppo Thanks! :)
  • filter() method is now hookable to define more complex SQL WHERE clauses.
  • whole usage description available in module settings.
  • from now all column values are populated as a property if the column is not named by a reserved word. Reserved words are: 'value','label','row' and 'data'.
$page->myfield->value
$page->myfield->label
$page->myfield->row // associative array of all values of the selected datatablerow
$page->myfield->columnname-1
$page->myfield->columnname-2 // ...

I tested the Module with PW 2.6.17. Please give me feedback if the module works in your surroundings.

  • Like 2
Link to comment
Share on other sites

Thanks kixe for this awesome module. ^-^

I am new to ProcessWire. I am using this module to make a tagging system and I have a stupid question.

I have two templates, "news" and "news_detail". "news_detail" contains the Select External Option field named "tag" with some option tags which come from the input of other pages, while "news" page display all "news_detail" pages with the tags.

For some reasons I want to have the array of "tag" value.

$news = $pages->find("template=news_detail");
foreach($news as $n) {
  $tag = $n->tag; //echo this gives SelectExternalOption|SelectExternalOption|SelectExternalOption
  $arr = [];
  foreach($tags as $tag){
  $arr[] = $tag->row['data'];
  } 
...

I think it is not the correct way to do so. I tried 

$arr = $n->tag->row['data'];

But this outputs nothing. Any clean way to achieve that?

Link to comment
Share on other sites

@starter welcome first!

$news = $pages->find("template=news_detail");
$arr = array();
foreach($news as $n) {
  $tags = $n->tag; // <- TYPO CORRECTED
  foreach($tags as $tag){
  $arr[] = $tag->row['data']; // will fill the array with each value of multiple value field of each page in page array
  } 
...

$news is an pageArray
$n is a single page of this array
$n->tag  is afield which returns a wireArray, because you choosed a multiple value field

//call a single value
$n->tag->first()->value
$n->tag->last()->value
$n->tag->eq(4)->value

or loop like you did. Maybe you prefer a multiple array (pages/ multivalues)?
 

Link to comment
Share on other sites

Thanks kixe for kind helping!

Yes I want to obtain the array of the value of chosen options in a page.

I can then iterate the array for other matters easily like displaying them.

Calling single value is quite clear for me, but for calling multiple values I stuck, so I loop through them for getting single value one by one and merge them in an array. Any API for doing so directly? :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...