Jump to content

How to get subfields or properties if fieldtype is unknown. Helpful Snippet


kixe
 Share

Recommended Posts

I was struggling a bit getting all subfields of a field if type is unknown. I made a small function for use in templates which returns an array() of all properties of (maybe any?) pagefieldvalue.
If there is something similar in core (which I couldn't find) please let me know.
Tested it with Fiedtype Options, Page, ProfieldsTable.
Feel free to use it. :)

/**
 * ProcessWire UsefulSnippets
 *
 * How to get all properties, subfields of any field if you don't know the type only if value is set
 * @return array
 */

function getProperties($fieldvalue) {
// multiple value field
if ($fieldvalue instanceof WireArray) {
    $result = array();
    foreach ($fieldvalue as $subfieldvalue) {
        $result[] = getProperties($subfieldvalue);
    }
    return $result;
// single value field with subfields
} else if ($fieldvalue instanceof WireData) return get_object_vars($fieldvalue->getIterator());
// single value field
else return $fieldvalue;
}

// Example
var_dump(getProperties($page->myfield));
  • Like 4
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

×
×
  • Create New...