AugustMiller / kirby-architect

šŸ“ Easily reference Blueprint data from anywhere in your Kirby application.
38 stars 1 forks source link

raw #8

Open bnomei opened 7 years ago

bnomei commented 7 years ago

getting the labels is very useful indeed but in my case i needed a lot more from the blueprint to generate dynamic frontend forms matching the panel blueprint. especially the requiredsetting and the validate with its property match to create regex for parsley.

fieldname:
  type: text
  validate:
    match: "/^\d{2}\.\d{4}$/"
anotherwithoutvalidate:
  type: email

getting that with the direct yml array is a bit hacky since the match propery was not needed in some blueprint field definitions.

$mightbreak = Architect::field_info('variety', 'density')['validate']['match'];
$betterbutugly = a::get(Architect::field_info('variety', 'density'), 'validate', []), ''match', null);

i ended up with lots of if-clause testing and a::get() but maybe you can think of a better solution?

AugustMiller commented 7 years ago

This is something I'd hoped to implement from the beginningā€” a "soft getter" of some kind, that would accept an array of arrays or "accessors," like this:

Architect::get('blueprint', [
  'nested.path.to.get', # Or, ['nested', 'path', 'to', 'get'] if you're all about verbosity!
  'fallback.nested.path.to.get',
  'third.fallback.to.try'
], 'Default Value');

Does this describe the kind of functionality you'd want?

bnomei commented 7 years ago

i do not need the fallback, but the default value would be nice. in my case i need to get an option (even a nested one like with validate/match of a field dynamically but if it does not exist it should return null. kirbys 'magic'-field-methods $page->fieldname() work similar and i think the structure-type ($page->content()->toStructure(); no nested) as well.

if( $matchForParsley = Architect::get('blueprint', 'fieldname.validate.match', null) ) {
  // do something
}
bnomei commented 7 years ago

when constructing my forms based on the blueprint I actually iterate over all fields defined in the blueprint and parse all their options flat and nested. but using yml arrays and a recursive function its a bit messy. maybe the architect could return a kirby-collection-object? then filtering etc. would work out of the box.