Storing Scattered Fields as a Single Object

I’d love to have a way to store the output of a few fields that are deliberately scattered all over the panel UI into one object. Is there a way to achieve that?

For example, I have three separate fields (award_year, award_procedure, award_label) displayed in different parts of the panel. Ideally, I’d like them stored as a single object in the content file:

acquisition: 

  year: 2023
  procedure: some-procedure
  label: gold

Putting the fields in an object would ruin the UI, so that’s off the table. Is there a way of storing data from a couple of fields without grouping them visually in the UI or duplicating the data (yeah, I entertained the “hidden object field” idea for like three minutes)?

Would be pretty cool to have a property like parent_object on any field, that would nest this field in an object, along with any other field that has the same value set to that property…

Did I miss something? Is there an obscure Kirby pattern or plugin that handles this kind of “scattered UI → unified storage” mapping?

I’m pretty sure there is a way to achieve this, but in your case, it is not only a matter of storing the different fields into one field but also reading back from the unified field. Seems like an unnecessary effort for a purpose that is not quite clear to me. What do you want to achieve with the storage in one field?

I guess you could use a panel hook to copy those values into a json file which would give you a central place and key / value pairs to read and write from, but not sure if thats entrely what you want.

Why would grouping them together in the panel ruin the UI? Those feilds do seem related.

1 Like

If you only want to have this object to handle it in the frontend (and don’t need it stored in the content file), you could create a Page model with a method like this:

public function unifiedField(): Kirby\Content\Field
{
	$array = [
		'year'      => $this->year()->value(),
		'procedure' => $this->procedure()->value(),
		'label'     => $this->label()->value(),
	];
	
	return new Kirby\Content\Field($this, 'object', Data::encode($array, 'yaml'));
}

And then use it in your template like this:

$object = $page->unifiedField()->toObject();

echo $object->year();
echo $object->procedure();
echo $object->label();
1 Like

First of all, let me take a minute to appreciate how alive this community feels!

Ha, I guess I didn’t really think that far yesterday… This is pretty simple and a nice resolution for what “bothers” me most about my current setup: Handling these properties back in the Template…

This is the way to go for me, I guess. Thank you!

It’s merely a component of the structured data I’m currently working with - when viewed in isolation, my example does indeed make very limited sense, haha… Nevertheless, I believe I’ve discovered a promising way towards a solution.