Jump to content

Template families and inherited fields


Oliver
 Share

Recommended Posts

As I’m getting a bit more into the e-commerce idea I was talking about in one of the other threads, I have been thinking about the PW template system. In a shop system you’d need a kind of basic product template with certain necessary fields like default price or something like that. But you’d also need the possibility to have variations of a product template, where additional fields can be defined individually for a certain kind of product. I know that there is the possibility to clone fields from an existing template. But I don’t think, that is a good solution for this particular problem, as you were able to delete some of the basic product data fields.

So my thought was, that it maybe would be a cool thing to be able to define a kind of relationship between templates. Like a class extending another one you could define an existing template as parent and the child template would inherit all fields and settings. New fields could be added, the inherited fields were kind of protected, the settings could be overridden by the ones defined for the child templates.

I guess, this could be achieved by a module pretty easily. But I think it could also be a pretty nice enhancement as a core feature.

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

… and updated in case the original template is edited.

That's the difficult part as nobody can guess if you really want those fields to change or if you've maybe changed one of those fields just for that single template and won't want your changes to be overwritten.

Link to comment
Share on other sites

  • 4 months later...

Consider this:

  1. You have a view to choose a set of fieldgroups / templates
  2. Once selected you'll get a list of matching fields (Only those fields that are used withing all fieldgroups of the initial selection)
  3. Applied changes will be reflected on all fieldgroups

This would remove the need to maintain any sort of inheritance meta data but should be quite flexible.

Maybe you can even add bookmarks for your fieldgroup selections.

I consider developing such a module but I'd really like to hear from you guys whether there may be some drawbacks.

Link to comment
Share on other sites

Just implemented a very simple fieldgroup inheritance module. I thought it may be helpful to others. Any insights on issues that may occur using this are very welcome if you find some.

protected $relations = [
	'foo' /* parent fieldgroup name */ => [
		[
			'template_name' => 'bar',
			'auto_remove_fields' => true,
		]
	]
];

public function init() {
	$this->addHookBefore('Fieldgroups::save', $this, 'syncFields');
}

public function syncFields( HookEvent $event ) {
	$fieldgroup = $event->arguments[0];

	if ( ! array_key_exists($fieldgroup->name, $this->relations)) return;

	$processTemplate = $this->modules->get('ProcessTemplate');
	$importFieldgroup = new ReflectionMethod('ProcessTemplate', 'importFieldgroup');
	$importFieldgroup->setAccessible(true);
	$removedFields = $fieldgroup->removedFields;

	foreach ($this->relations[$fieldgroup->name] as $relation) {
		$template = $this->templates->get($relation['template_name']);
		$importFieldgroup->invoke($processTemplate, $fieldgroup, $template);

		if ($removedFields && $relation['auto_remove_fields']) {
			foreach ($removedFields->getArray() as $fieldName) {
				$this->message('Removing field "' . $fieldName . '" from fieldgroup "' . $template->fieldgroup->name . '"');
				$field = $this->fields->get($fieldName);
				$template->fieldgroup->remove($field);
			}
		}

		$template->fieldgroup->save();
	}
}
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...