Pete Posted June 21, 2015 Share Posted June 21, 2015 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 More sharing options...
diogo Posted June 21, 2015 Share Posted June 21, 2015 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 1 Link to comment Share on other sites More sharing options...
Pete Posted June 21, 2015 Author Share Posted June 21, 2015 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 More sharing options...
Pete Posted June 21, 2015 Author Share Posted June 21, 2015 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>"; } } 13 Link to comment Share on other sites More sharing options...
blynx Posted January 20, 2017 Share Posted January 20, 2017 <?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. 9 1 Link to comment Share on other sites More sharing options...
dragan Posted July 23, 2017 Share Posted July 23, 2017 (edited) 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 July 23, 2017 by dragan realized what the error caused, right after posting... 2 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