adrian Posted July 12, 2013 Share 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 Link to comment Share on other sites More sharing options...
Soma Posted July 12, 2013 Share 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. Link to comment Share on other sites More sharing options...
Soma Posted July 12, 2013 Share 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 Link to comment Share on other sites More sharing options...
adrian Posted July 12, 2013 Author Share 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! Link to comment Share on other sites More sharing options...
kongondo Posted July 12, 2013 Share Posted July 12, 2013 Hi, Please explain what this FieldtypeFieldsetOpen::fieldsetCloseIdentifier; does. Thanks. Link to comment Share on other sites More sharing options...
adrian Posted July 12, 2013 Author Share 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 Link to comment Share on other sites More sharing options...
Soma Posted July 12, 2013 Share 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 Link to comment Share on other sites More sharing options...
dragan Posted August 23, 2013 Share 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. Link to comment Share on other sites More sharing options...
Soma Posted August 23, 2013 Share Posted August 23, 2013 $fs = $template->fields->get("fieldsetfieldname")->children; 1 Link to comment Share on other sites More sharing options...
dragan Posted August 23, 2013 Share 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 Link to comment Share on other sites More sharing options...
ryan Posted August 24, 2013 Share 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 Link to comment Share on other sites More sharing options...
dragan Posted August 24, 2013 Share 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. Link to comment Share on other sites More sharing options...
ryan Posted August 27, 2013 Share 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"); Link to comment Share on other sites More sharing options...
Neeks Posted November 23, 2015 Share 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. Link to comment Share on other sites More sharing options...
adrian Posted November 25, 2015 Author Share 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 Link to comment Share on other sites More sharing options...
Rudy Posted October 3, 2016 Share 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 Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 3, 2016 Share 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 Link to comment Share on other sites More sharing options...
Rudy Posted October 3, 2016 Share Posted October 3, 2016 @LostKobrakai, thanks for your example. It works! Link to comment Share on other sites More sharing options...
elabx Posted May 24, 2018 Share 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 Link to comment Share on other sites More sharing options...
Jonathan Sachse Mikkelsen Posted January 24, 2020 Share 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? Link to comment Share on other sites More sharing options...
dragan Posted January 24, 2020 Share 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; Link to comment Share on other sites More sharing options...
Jonathan Sachse Mikkelsen Posted January 26, 2020 Share 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now