Jump to content

Getting fields in a fieldset


Pete
 Share

Recommended Posts

Hi folks

I'm trying to fetch and iterate through fields in a specific fieldset in a page via the API but for some reason my brain cells are failing to work as a team tonight. Any suggestions?

Link to comment
Share on other sites

Well, the fields are not really inside the fieldset, so you can’t get them in an easy way. What you can is look for the fieldset_open and get all the fields after that and before fieldset_close. Have to go, so no code example, I’m sure you’ll get it from here :)

  • Like 1
Link to comment
Share on other sites

That would do it diogo - I hadn't thought of the simple answer! :)

I had been looking for some magic function thinking there was some relationship stored between the fieldset and the fields inside it forgetting it's a purely visual thing.

Link to comment
Share on other sites

And here's some code for those that may need it:

$myfieldset_start = false;
foreach ($page->template->fields as $field) { // or something like $this->templates->get('templatename')->fields
    if ($field->name == 'myfield') { // opening element of a fieldset is just the field name you gave it
        $myfieldset_start = true;
    } elseif ($field->name == 'myfield_END') { // ending element is field name with _END on it - break out of the loop if we reach this
        break;
    } elseif ($myfieldset_start == 'true') { // otherwise we are iterating fields in the chosen fieldset so do what you like here
        echo $field . "<br>";
    }
}
  • Like 12
Link to comment
Share on other sites

  • 1 year later...
<?php
/**
 * getFieldsetOf
 *
 * for ProcessWire
 * 
 * gets fields inside a fieldset of pages or templates
 * choose to retrieve values
 * 
 * @param  Template|Page         $context                   the page or template
 * @param  String                $fieldsetName              name of the fieldset
 * @param  bool|boolean          $collectValues             want to collect values of the pages fieldset?
 * @param  string                $fieldsetCloseIdentifier   default: '_END'
 * @return FieldsArray|WireData                             returns FieldsArray or if data wanted, WireData
 */
function getFieldsetOf($context, String $fieldsetName, $collectValues = false, $fieldsetCloseIdentifier = '_END') {    

    if ($collectValues === true && $context instanceof Page) {
        $collectedItems = new WireData();
    } else if($context instanceof Template) {
        $collectValues = false;
        $collectedItems = new FieldsArray();
    } else {
        throw new WireException("getPageFieldset: first argument must be of type Page or Template", 1);
    }

    if (!$context->fieldgroup->get($fieldsetName . $fieldsetCloseIdentifier)) return NULL;

    $collecting = false;
    foreach ($context->fieldgroup as $field) {
        if ($field->name == $fieldsetName) {
            $collecting = true;
            continue;
        }
        if ($field->name == $fieldsetName . $fieldsetCloseIdentifier) {
            break;
        }
        if ($collecting) {
            if ($collectValues) {
                $collectedItems->set($field->name, $context->get($field->name));
            } else {
                $collectedItems->add($field);
            }
        }
    }
    return $collectedItems;
}

Some extension of the above code - works with templates and pages.
If thrown at pages you may choose to retrieve the values inside the fieldset as a WireData object.

 

edit:

@dragan (below) thanks for the hint! Just removed it.

  • Like 9
  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...

Just a quick note: remove the hints in the function, otherwise it won't work:

function getFieldsetOf($context, $fieldsetName, $collectValues = false, $fieldsetCloseIdentifier = '_END'); // this will work
function getFieldsetOf($context, String $fieldsetName, bool $collectValues = false, $fieldsetCloseIdentifier = '_END'); // this won't

at least not with the latest PW dev on PHP 7.0.9

example function call: 

$fieldSet_import = getFieldsetOf($templates->get('project'), 'import_only', false, $fieldsetCloseIdentifier = '_END');
Edited by dragan
realized what the error caused, right after posting...
  • Like 2
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...