fbg13

FieldtypeOptions - set selectable options through api

Recommended Posts

I'm setting up some fields in a module and i need an options field, but i can't figure out how to set its options with the api.

These options:

Untitled.jpg

Share this post


Link to post
Share on other sites

I also found the same problem for adding FieldtypeOptions through API. Below are the codes that I'm using which is work well for other field types.

From the json export the FieldtypeOptions array value is in this line "export_options": {"default": "1=on|On\n2=off|Off\n3=indexon|Index On\n4=indexoff|Index Off"}

 

public function extraFields() {
	$fields = array(
		'invoice_stat' => array('id'=>'112', 'type'=>'FieldtypeOptions', 'flags'=>'0', 'name'=>'invoice_stat', 'label'=>'Status', 'description'=>'', 'derefAsPage'=>'1', 'collapsed'=>'0', 'columnWidth'=>'', 'parent_id'=>'', 'template_id'=>'', 'findPagesSelector'=>'', 'labelFieldName'=>'.', 'inputfieldClass'=>'InputfieldSelect', 'usePageEdit'=>'0', 'labelFieldFormat'=>'{pg_alias}', 'tags'=>'', 'allowUnpub'=>'', 'showIf'=>'', 'required'=>'', 'requiredIf'=>'', 'findPagesCode'=>'', 'defaultValue'=>'1=on|On\n2=off|Off\n3=indexon|Index On\n4=indexoff|Index Off', 'addable'=>''),		
	);
	
	
	foreach ($fields as $field) {
		$f = new Field();
		$f->type = $this->modules->get($field['type']);
		//$f->id = $field['id'];
		$f->name = $field['name'];
		$f->label = $field['label'];
		
		if (isset($field['inputfieldClass'])) $f->inputfieldClass = $field['inputfieldClass'];
		if (isset($field['inputfield'])) $f->inputfield = $field['inputfield'];
		if (isset($field['export_options'])) $f->export_options = $field['export_options'];
		
		if (isset($field['flags'])) $f->flags = $field['flags'];
		if (isset($field['description'])) $f->description = $field['description'];
		if (isset($field['notes'])) $f->notes = $field['notes'];
		if (isset($field['derefAsPage'])) $f->derefAsPage = $field['derefAsPage'];
		if (isset($field['collapsed'])) $f->collapsed = $field['collapsed'];
		if (isset($field['parent_id'])) $f->parent_id = $field['parent_id'];
		if (isset($field['labelFieldName'])) $f->labelFieldName = $field['labelFieldName'];
		
		if (isset($field['tags'])) $f->tags = $field['tags'];
		if (isset($field['allowUnpub'])) $f->allowUnpub = $field['allowUnpub'];
		if (isset($field['showIf'])) $f->showIf = $field['showIf'];
		if (isset($field['columnWidth'])) $f->columnWidth = $field['columnWidth'];
		if (isset($field['required'])) $f->required = $field['required'];
		if (isset($field['requiredIf'])) $f->requiredIf = $field['requiredIf'];
		if (isset($field['template_id'])) $f->template_id = $field['template_id'];
		if (isset($field['findPagesSelector'])) $f->findPagesSelector = $field['findPagesSelector'];
		if (isset($field['findPagesCode'])) $f->findPagesCode = $field['findPagesCode'];
		if (isset($field['labelFieldFormat'])) $f->labelFieldFormat = $field['labelFieldFormat'];
		if (isset($field['defaultValue'])) $f->defaultValue = $field['defaultValue'];
		if (isset($field['addable'])) $f->addable = $field['addable'];

		$f->save();
	}
}

 

Share this post


Link to post
Share on other sites

What is $field['export_options'] supposed to be?

Just the value from export_options of the export json?

$f4 = new Field();
$f4->type  = $this->modules->get("FieldtypeOptions");
$f4->name  = "name";
$f4->label = "Label";
$f4->export_options  = "1=on|On\n2=off|Off\n3=indexon|Index On\n4=indexoff|Index Off";
$f4->save();

I tried multiple formats but there are no options after i install the module.

Share this post


Link to post
Share on other sites

The methods for setting new options for an Options field via the API seem to be a bit convoluted - I think it's probably an oversight and it would be worth raising a feature request at GitHub.

There's a method that seems to be primarily intended for the admin back-end: setOptionsString(). You can use it but the way the options are defined (string with line break separator) is a bit weird for API use and it's not easy to get it to play nicely with existing options.

$f = $fields->my_options_field;
$manager = new SelectableOptionManager();
$options = 'red
green
blue'; // you can also set IDs and values if needed
$manager->setOptionsString($f, $options, false); // if last argument is omitted/true you will remove any existing options

Otherwise you could manually create SelectableOption objects, add them to a SelectableOptionArray, and use addOptions(), deleteOptions(), setOptions(), etc, with that SelectableOptionArray. See the module source code. It's hardly a simple process though.

I think what's needed are methods to go from options to PHP array and PHP array to options.

  • Like 4

Share this post


Link to post
Share on other sites
$f4 = new Field();
$f4->type  = $this->modules->get("FieldtypeOptions");
$f4->name  = "name";
$f4->label = "Label";
$f4->save();
$manager = new SelectableOptionManager();
$options = 'red
green
blue'; // you can also set IDs and values if needed
$manager->setOptionsString($f4, $options, false);
$f4->save();

That did it, had to save the field before adding the options, took me a while to figure it out.

Thanks for the help.

  • Like 3

Share this post


Link to post
Share on other sites

I have this string of options:

option1|Option 1
option2|Option 2
option3|Option 3

I add the options string like mentioned before:

$manager = new \ProcessWire\SelectableOptionManager();
$manager->setOptionsString($field, $options, true);

The result is this:

Screen Shot 2017-01-04 at 13.37.52.png

If I add it with IDs

1=option1|Option 1
2=option2|Option 2
3=option3|Option 3

The result is this:

Screen Shot 2017-01-04 at 13.40.45.png

In both cases within the field settings detail tab, everything looks OK, and the preview shows the expected output:

Screen Shot 2017-01-04 at 14.15.45.png

 

Also: Changing the override argument to false, does not affect this behavior.

In the database, everything looks fine:

Screen Shot 2017-01-04 at 14.33.01.png

 

Debugging $inputfield->options in FieldtypeOptions:getInputfield, the options array looks fine as it should: Three items with an id, value and title.

This is VERY weird:

If I replace the return value in FieldtypeOptions:getInputfield

// original
return $inputfield;

// replace
$fs = $this->modules->get('InputfieldFieldset');
$fs->add($inputfield);
return $fs;

I get the correct output, in the given Fieldset:

Screen Shot 2017-01-04 at 14.50.04.png

This suggests that the misbehavior is happening somewhere else?

 

If I create it via admin, everything works as expected, only the titles show.

Any help? Thanks!

 

Share this post


Link to post
Share on other sites

Have you tried like this:

$options = "1=option1|Option 1\n2=option2|Option 2\n3=option3|Option 3";

BTW - I haven't tested this, just going on what is exported when exporting the field.

Share this post


Link to post
Share on other sites

Got it working. The source of the misbehavior was, that I set the string somewhere else afterwards, like so: $field->options = $optionsString;

Doh! The side effects are still very strange though, and interesting that the field class even uses it somehow.

Share this post


Link to post
Share on other sites

I've been working with FieldtypeOptions recently and in the absence of documentation thought I would share some example code:

$field = $fields->get('test_options');
/* @var FieldtypeOptions $fieldtype */
$fieldtype = $field->type;

// Get existing options
// $options is a SelectableOptionsArray (WireArray)
// If there are no options yet this will return an empty SelectableOptionsArray
$options = $fieldtype->getOptions($field);

// Create an option
$yellow = new SelectableOption();
$yellow->title = 'Yellow';
$yellow->value = 'yel'; // if you want a different value from the title
// Don't set an ID for new options - this is added automatically
// Will deal with 'sort' property later

// Create another option
$orange = new SelectableOption();
$orange->title = 'Orange';

// Add option after the existing options
$options->add($yellow);

// Get an option by title
$green = $options->get('title=Green');

// Insert option at a certain position
$options->insertAfter($orange, $green);

// Remove an option
$options->remove($green);

// Reset sort properties (so order of options is the same as the SelectableOptionsArray order)
$sort = 0;
foreach($options as $option) {
    $option->sort = $sort;
    $sort++;
}

// Set options back to field
$fieldtype->setOptions($field, $options);

 

  • Like 6

Share this post


Link to post
Share on other sites

Hi @Robin S,

I try to echo an multiple options inside a class name. I can do using:

$field = $fields->get('test_options'); or  $fieldtype = $field->type; 
I use this: <div class="mix <?= $product->category_4->title ?>">

  But I have multiple selection, and in this way the show me only the first option I check in the page in the backend.

 

I have try to use : $options = $fieldtype->getOptions($field);  But I not have understand  how to make it work inside my code:

<?php foreach($page->children() as $product): ?>
        <?php
          $image_sta = $product->img_statica->first;
        ?>
          <div class="mix <?= $product->category_4->title ?>">
              <img src="<?= $image_sta->url; ?>">
            	<h3><a href="<?=$product->url;?>"><?= $product->title; ?></a></h3>
          </div>
      <?php endforeach; ?>

Every time i try differente code he give me always the first option name.  If I don't use the ->title this returns to me with all ID, but I can not use this ID I have to use the name of the options fields. I can do it?

Thanks!

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

@MarcoPLY, this topic is about setting the selectable options for a field via the API. As far as I can see your question isn't related to that so would be better asked in a new topic.

If you have a "multiple values" options field you cannot simply echo the field value. You have to loop over the field value in a foreach() or perhaps use implode() to create a string from the value.

Here is one way you could try:

<?php foreach($page->children() as $product): ?>
    <?php
    $image_sta = $product->img_statica->first;
    $classes = '';
    foreach($product->category_4 as $option) $classes .= " $option->title";
    ?>
    <div class="mix<?= $classes ?>">
        <img src="<?= $image_sta->url; ?>">
        <h3><a href="<?= $product->url; ?>"><?= $product->title; ?></a></h3>
    </div>
<?php endforeach; ?>

Also check out the documentation for the Select Options fieldtype.

  • Like 1

Share this post


Link to post
Share on other sites

Hi @Robin S sorry for going out of topic, next time I'll do a new one topic.

Thank you for your answer! It's a great help! I still have to learn a lot about how to use the api in pw (and also php!!) :) 
I read the documentation and it helped me, only that for the multiple options I was a bit confused, but now I understand how it works! So thank you a lot!

Share this post


Link to post
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

  • Recently Browsing   1 member

  • Similar Content

    • By louisstephens
      So I was going to build a todo tracking app for myself to test/broaden my knowledge of processwire, and so far it has been taxing 😓.  My Site structure is:
      - Project One - Phase One - Task - Task - Task - Phase Two - Task - Task - Task So far it was pretty easy, I can easily foreach through the Project and get the phases with their tasks. However, it gets a bit muddled when I have more than one project as I was trying to have a dashboard where the content switches out to the selected project as opposed to accessing each project via their own url. How would yall handle this?
      My next hurdle is each task has a select field (for project status) that I want to update via ajax (for the smooth transitioning).
      Scenario: You finish a task, change the option from "new" to "completed", and an onclick changes the status drop down background to green (which I have working), but then posts/saves a field on the backend to the new option.
       
      I have a page called "Actions" set up with url segments using
      if ($config->ajax && $input->urlSegment1 == 'change-status') { //save update field on admin } However, I am a little fuzzy on how to actually pass the current page along with the new selected status to actually update the page (task). I guess I am not very far into my endeavor. Any help would be greatly appreciated.
    • By louisstephens
      So this is more or less just a question regarding best practices, and how to actually achieve this. I have an internal tool I built awhile ago that has just gotten very "trashed". The naming conventions are all out of whack due to trying to get something completed, and pages are just everywhere. The fields got really out of hand as well, but I believe I have found a much cleaner field structure with the Repeater Matrix and a few other ProFields.  I decided recently to rewrite the template structure and the fields from scratch to keep everything nice and neat, but then it hit me that I would need to move all the previous data over and populate the new fields etc.
      The current tree looks something like:
      Group Title - Section A (template "section") - Category One (template "category") - Category Two (template "category") - Section B (template "section") - Category One (template "category") - Category Two (template "category") Etc etc... The structure is pretty good, and I am thinking of keeping it as it serves my purpose. However, I now have about 140 Sections with child pages that I would hopefully like to move to different templates, as well as changing the child templates/mapping data to the new fields. The only fields I need to worry about are a few textareas, text fields, options field,  as well as 1 image filed.
      I guess my real question is, what is the best way to go about this "migration" via the api while keeping the parent/child structure. It would be ok to leave it as is and chip away at it if I were the only one using it, but unfortunately that is not the case. 
      I guess one approach would be to copy over the entire structure and make the move to the new templates/fields. Once we have verified all the data/templates are correct, we could remove the old structure and possibly rename the new to mirror the old. However, this might just be too complicated.
    • By EyeDentify
      Hello Fellow PW Fans and Gurus.

      I have run into a problem where i have created a template without a template file associated with it.
      Now i would like to HAVE a template file associated with it, not an alternate but as the main template file.

      So i thought, that's easy, I upload the template file and change it in template settings but I only get the Alternate template file setting?

      Ok so maybe i can change it via the API , so i wrote some code for this.
      <?PHP function changeTemplate($pages = null, $templates = null) { /* get the pages to change template on */ $logItems = $pages->findMany('template=tmp_log_item'); /* get the template object for our desired template */ $Template = $templates->get('tmp_new_log_item'); foreach($logItems AS $key => $logData) { /* use template object $Template to set template */ $logData->template = $Template; /* save page item */ $logData->save(); } } /* do the magic change */ changeTemplate(); ?> Needless to say, PW was not happy about this, It throw up an error message:
      Error: Exception: Invalid value sent to Page::setTemplate (in /home/virtual/mydomain/public_html/wire/core/Page.php line 1782)
      Now from what i understand i gave $logData->template the wrong type of value.
      So what should the value be to correct this?
      When I read the docs at: https://processwire.com/api/ref/page/
      the value can be "string" or "Template".

      So i gave it a string and it went haywire.

      How should I approach this cause I realy dont want to manually change template file on över 50 plus pages

      Maybe I should use the Template class to create the value that the API wanted, but I am unsure how to use it.

      Thank you for any info you can give me on this.

      If you want more info to help me, ask and I will try my best to give it to you.
      Updated and Solved
      I solved the problem and have updated the code to reflect this.

      Essentialy i used the get() method of $templates to get the template object for my template i wanted to use using the template name.
      I gave the Object to the $logData->template property and all went well.
      We learn something new every day.
      One Note:
      The template file you want to use must have been uploaded and added as a template in the Template section in the Admin for this code to work as expected.
    • By chcs
      After running this code in my module's __install() the template_id is not set. Any ideas why?  (The export contents from the template created and the one that I built with Admin are the same. Both exports have the template_id is set to mm_state. When I go to edit the field in Admin, the template is not showing on the Input tab and it doesn't show the template title on a page using the template that uses this field. Using processwire 3.0.98)

      // mm_state
      $field = $this->fields->get('mm_state');
      if (!@$field->id) $field = new Field();
      $field->type = $this->modules->get("FieldtypePage");
      $field->name = 'mm_state';
      $field->label = 'State';
      $field->derefAsPage = 2;
      $field->inputfield = "InputfieldSelect";
      $this->fields->save($field);
      $field->labelFieldName = 'title';
      $field->template_id = 'mm_state';
      $field->columnWidth = 100;
      $field->required = true;
      $this->fields->save($field);
    • By mattcohen
      Hi, I am wondering if someone can help me out with getting the count of the comments of a page? 
      I am using Ryan's comments module as a reviews system for my site and would like to display the count of the comments per page, as below 
       

       
      The shops part that you see above is something similar to this 
      <?php $set_page = $page->title; $count = $pages->count("template=prices, title=$set_page"); echo $count; ?>