adrian 12,208 Posted July 12, 2013 I can create the FieldtypeFieldsetOpen no problem, but can't figure out how to create the FieldtypeFieldsetClose. It doesn't seem to be automatic like it is via the admin and I can't seem to create it manually either. I imagine automatic wouldn't work because it wouldn't know what fields belong inside the set. I have created the required fieldset using InputfieldFieldset and the layout looks ok until I want a second fieldset in the same template - then the need for the Close / _END field becomes important. Anyone have any ideas? PS I have been using this example as a starting point for generating the fieldsets: http://processwire.com/talk/topic/1196-api-show-field-label/?p=10691 but without defining a FieldtypeFieldsetOpen it doesn't really seem to do what I want in that nothing gets grouped in the admin. 1 Share this post Link to post Share on other sites
Soma 6,637 Posted July 12, 2013 There's nothing special about it you just use InputfieldFieldset and append the inputfields you want in there. Then create a new fieldset and add the fields you want there. Then add the two fieldsets to the inputfieldwrapper. Share this post Link to post Share on other sites
Soma 6,637 Posted July 12, 2013 Aah, so you want to use them for templates in the admin? How about: $opener = new Field(); $opener->type = new FieldtypeFieldsetOpen(); $opener->name = "myfieldset"; $opener->label = "Open fieldset"; $opener->save(); $closer = new Field(); $closer->type = new FieldtypeFieldsetClose(); $closer->name = "myfieldset" . FieldtypeFieldsetOpen::fieldsetCloseIdentifier; $closer->label = "Close an open fieldset"; $closer->save(); $tpl = $templates->get("custom"); $tpl->fieldgroup->add($opener); $tpl->fieldgroup->add($fields->get("body")); $tpl->fieldgroup->add($fields->get("counter")); $tpl->fieldgroup->add($closer); $tpl->fieldgroup->save(); 5 Share this post Link to post Share on other sites
adrian 12,208 Posted July 12, 2013 Soma - thanks muchly for the point in the right direction - it was the FieldtypeFieldsetOpen::fieldsetCloseIdentifier; that I was missing. Everything seems to be working great now! Share this post Link to post Share on other sites
kongondo 7,394 Posted July 12, 2013 Hi, Please explain what this FieldtypeFieldsetOpen::fieldsetCloseIdentifier; does. Thanks. Share this post Link to post Share on other sites
adrian 12,208 Posted July 12, 2013 As far as I know, all it does is add the "_END" to the name of the field, which is what is required to define the end of a fieldset. Although I did try manually adding it and it didn't work, but maybe I had something else messed up at the time. 1 Share this post Link to post Share on other sites
Soma 6,637 Posted July 12, 2013 class FieldtypeFieldsetOpen extends Fieldtype { /** * Appended to the name of a 'Close' version of a FieldsetOpen * */ const fieldsetCloseIdentifier = '_END'; It's just a constant string to suffix the fieldsetclose name so the admin knows what fieldset to close. 3 Share this post Link to post Share on other sites
dragan 1,730 Posted August 23, 2013 A somewhat related question: Is it possible to grab all input fields from a particular fieldset (in the frontend)? I have 3 fieldsets in a template, and need to group these in tabs. I couldn't find a selector in the docs / cheatsheet. Share this post Link to post Share on other sites
Soma 6,637 Posted August 23, 2013 $fs = $template->fields->get("fieldsetfieldname")->children; 1 Share this post Link to post Share on other sites
dragan 1,730 Posted August 23, 2013 hmm, that doesn't seem to work. e.g. $tpl = $templates->get("product"); $prodDescFields = $tpl->fields->get("product_description")->children; $prodDescFields is empty o_O Share this post Link to post Share on other sites
ryan 19,526 Posted August 24, 2013 Is it possible to grab all input fields from a particular fieldset (in the frontend)? I have 3 fieldsets in a template, and need to group these in tabs. I couldn't find a selector in the docs / cheatsheet. Soma's solution will work for typical inputfield forms, but fieldgroups are actually stored in a flat manner rather than a tree. So you'd have to iterate them to find all fields in a fieldset (unless I'm forgetting something, as I don't need to do this very often). $children = array(); $record = false; $name = 'your_fieldset_name'; foreach($template->fieldgroup as $field) { if($field->type instanceof FieldtypeFieldsetOpen && $field->name == $name) $record = true; if(!$record) continue; if($field->type instanceof FieldtypeFieldsetClose) break; $children[] = $field; } 1 Share this post Link to post Share on other sites
dragan 1,730 Posted August 24, 2013 Thanks Ryan. It works - but if I add the following code to actually output the data, I only get the default language output. If I switch to another language, it still only shows the default language. (PW 2.3.2, all field labels and values are correctly and fully translated) foreach($children as $f) { $fieldName = $f->label; $fieldValue = $page->$f; if(strlen($fieldValue) > 0) { echo "<dt>$fieldName</dt>\n"; echo "<dd>$fieldValue</dd>\n"; } } Do I manually have to add something to make this language-aware? Something with $user->language or such? I was under the impression that wouldn't be necessary anymore with the dev version 2.3.2. Share this post Link to post Share on other sites
ryan 19,526 Posted August 27, 2013 Using field labels on the front-end of your site isn't a very common usage, so they aren't automatically adjusted by language the way page fields are. But you can still get the language-specific field label pretty easily, like this: $language = $user->language; $fieldLabel = $field->get("label$language"); Share this post Link to post Share on other sites
Neeks 54 Posted November 23, 2015 What would be the best way to add an existing field right before the ending of an existing feildset? IE. I already have a fieledset called seo and I want to add a field right before the closing for "sitemap_ignore" via the API. Share this post Link to post Share on other sites
adrian 12,208 Posted November 25, 2015 What would be the best way to add an existing field right before the ending of an existing feildset? IE. I already have a fieledset called seo and I want to add a field right before the closing for "sitemap_ignore" via the API. You can use insertBefore, eg: $newField = $fields->get("your_new_field"); $t = $templates->get("your_template_name"); $existingField = $t->fields->get("sitemap_ignore_END"); $fg = $t->fieldgroup; $fg->insertBefore($newField, $existingField); $fg->save(); 2 Share this post Link to post Share on other sites
Rudy 101 Posted October 3, 2016 Are there any examples on how to use `fieldset` in the new module format? (https://github.com/ryancramerdesign/ProcessHello/blob/master/ProcessHello.config.php) Thx Share this post Link to post Share on other sites
LostKobrakai 5,218 Posted October 3, 2016 I'd imagine it should work like this: $this->add(array( // Text field: greeting array( 'name' => 'greeting', // name of field 'type' => 'text', // type of field (any Inputfield module name) 'label' => $this->_('Hello Greeting'), // field label 'description' => $this->_('What would you like to say to people using this module?'), 'required' => true, 'value' => $this->_('A very happy hello world to you.'), // default value ), // Radio buttons: greetingType array( 'name' => 'fs', 'type' => 'fieldset', 'children' => array( array( 'name' => 'greeting', // name of field 'type' => 'text', // type of field (any Inputfield module name) 'label' => $this->_('Hello Greeting'), // field label 'description' => $this->_('What would you like to say to people using this module?'), 'required' => true, 'value' => $this->_('A very happy hello world to you.'), // default value ), ) ) )); 2 Share this post Link to post Share on other sites
Rudy 101 Posted October 3, 2016 @LostKobrakai, thanks for your example. It works! Share this post Link to post Share on other sites
elabx 897 Posted May 24, 2018 Hi! Is this a valid approach to creating fieldsets? Doing this inside a module: $field = new Field(); $field->type = new FieldtypeFieldsetOpen(); $field->name = $sanitizedName; $field->label = $fieldLabel; $field->save(); $closer = $field->type->getFieldsetCloseField($field, true); Taken from here: https://github.com/processwire/processwire/blob/48fe0769a4eb3d0f5b4732fd01b4b4ed8262d952/wire/modules/Fieldtype/FieldtypeFieldsetOpen.module#L131 2 Share this post Link to post Share on other sites
Jonathan Sachse Mikkelsen 2 Posted January 24, 2020 Hi. in relation to this post. when creating a fieldset like this: $field = new Field(); $field->type = new FieldtypeFieldsetOpen(); $field->name = $sanitizedName; $field->label = $fieldLabel; $field->save(); is there any method to have it be collapsed (in admin) by default? Share this post Link to post Share on other sites
dragan 1,730 Posted January 24, 2020 Not sure, but I guess this applies to fieldsets as well: https://processwire.com/api/ref/inputfield/#pwapi-methods-collapsed-constants $field->collapsed = Inputfield::collapsedYes; Share this post Link to post Share on other sites
Jonathan Sachse Mikkelsen 2 Posted January 26, 2020 ah thanks dragan, unfortunately i didn't get to test that in the end. I worked around the issue so i didn't need to have them collapsed after all. but looks like it should work. Share this post Link to post Share on other sites