Jump to content

Create fieldset from API


adrian
 Share

Recommended Posts

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.

  • Like 1
Link to comment
Share on other sites

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

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();
  • Like 5
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.
  • Like 3
Link to comment
Share on other sites

  • 1 month later...

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

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; 
}
  • Like 1
Link to comment
Share on other sites

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

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

  • 2 years later...

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();
  • Like 2
Link to comment
Share on other sites

  • 10 months later...

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
			        ),
                )
			)
		));

 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

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

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...