Jump to content

Seeking clarity on Overrides: best practices and what did I do wrong?


Boost
 Share

Recommended Posts

Hello,

I'm aware that Overrides are not necessarily dangerous; most of the time, they are simply misused, just like I did. However, I believe there is a lack of documentation on this topic. So, I would love some clarity on Overrides, as I think I might have misunderstood the concept. Here's what I've gathered:

  • I created a FieldsetPage fieldtype named Hero, with the following fields: image, text, body, and URL.
  • I added the Hero field to the basic-page template.
  • I created three pages: A, B, and C, all using the basic-page template.

Now, here's where it gets interesting.

  • On page A, I made no changes to the Hero. I used it as it is.
  • On page B, I overrode the label of the image field to "logo".
  • On page C, I overrode the Hero by adding an extra field, such as a textarea.
  • Now, the changes made on page B and C will affect page A, correct? Is there a way to allow overrides by individual pages, rather templates? This would provide us with a lot of flexibility, wouldn't it?

My issue happened when I started adding extra fields and removing some, like I did on page C. I forgot it was one template-based page, so I ended up messing up the other pages.

One last thing. I don't understand the warning: " WARNING: enabling settings beyond those specified by the Fieldtype/Inputfield module may not always work, or may cause problems"

So, folks, I'd be extremely grateful for any insights regarding overrides, perhaps some best practices and what to avoid.

Cheers!

 

Link to comment
Share on other sites

Interesting topic. Assuming you have enabled the override for "Details: fields in fieldset", the warning you are talking about applies to your case. It simply means that any fields not rendered bold in the Overrides list are untested and may cause problems when using an override on them. It doesn't say it wouldn't work, though. It says you must test whatever you enable.

Now, I don't get how you achieved Overrides outside the template context. As far as I know, this isn't possible and from a frontend point of view also doesn't make any sense. You can easily create multiple templates (and their respective template files) which only differ in Overrides for a specific field. There is simply no need to have Overrides on a per-page basis.

Can you clarify on that last part?

Link to comment
Share on other sites

44 minutes ago, poljpocket said:

Now, I don't get how you achieved Overrides outside the template context. As far as I know, this isn't possible and from a frontend point of view also doesn't make any sense. You can easily create multiple templates (and their respective template files) which only differ in Overrides for a specific field. There is simply no need to have Overrides on a per-page basis.

I did not get Overrides outside the template. 

So, maybe here is my confusion about how to use Overrides correctly.

Link to comment
Share on other sites

It just so happens, that I needed to do overrides for a FieldTypeFieldsetPage field from the API inside a hook. Took some effort to finally get this working. Just wanted to share this.

/**
 * HOOK for changing label, description, notes for fields inside a FieldtypeFieldsetPage field
 */
$wire->addHookAfter("ProcessPageEdit::buildFormContent", function (HookEvent $event) {
	$page = $event->object->getPage();
	if ($page->template->name !== 'your-template-name') return;
	$form = $event->return;
	// product_description is our target field 
	// which holds fields 'image', 'headline', 'description'
	// product_description is of type FieldtypeFieldsetPage (FieldtypeRepeater under the hood)

	// get InputField
	/** @var InputfieldRepeater $inputField */
	$inputField = $form->get('product_description');
	// get actual Field from InputField
	/** @var RepeaterField $field */
	$field = $inputField->hasField->getContext($page->template);
	// change label, description, notes for 'image' field
	/** @var Field $field */
	$field->fields('image')->label = 'Hero Image';
	$field->fields('image')->description = 'Also shows on overview cards';
	$field->fields('image')->notes = 'Dimensions: 800x600px';
});

@Boostdid you manage to do the overrides in the template settings? On my install this does not work.
Here are my field settings:

1700659072_Edit-Field-product_description--ziehm-ddev-site.thumb.png.bfebe50c906770b62520a9b9dcac0cb4.png

And here's how the override settings in a template look like:

296216552_Edit-Template-clinical-application--ziehm-ddev-site.thumb.png.b0e4c8261ead051a02f12f49bb23b10a.png

Just being curious how that looks on your install :-)

 

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

  

On 10/27/2023 at 12:34 PM, gebeer said:

It just so happens, that I needed to do overrides for a FieldTypeFieldsetPage field from the API inside a hook. Took some effort to finally get this working. Just wanted to share this.

/**
 * HOOK for changing label, description, notes for fields inside a FieldtypeFieldsetPage field
 */
$wire->addHookAfter("ProcessPageEdit::buildFormContent", function (HookEvent $event) {
	$page = $event->object->getPage();
	if ($page->template->name !== 'your-template-name') return;
	$form = $event->return;
	// product_description is our target field 
	// which holds fields 'image', 'headline', 'description'
	// product_description is of type FieldtypeFieldsetPage (FieldtypeRepeater under the hood)

	// get InputField
	/** @var InputfieldRepeater $inputField */
	$inputField = $form->get('product_description');
	// get actual Field from InputField
	/** @var RepeaterField $field */
	$field = $inputField->hasField->getContext($page->template);
	// change label, description, notes for 'image' field
	/** @var Field $field */
	$field->fields('image')->label = 'Hero Image';
	$field->fields('image')->description = 'Also shows on overview cards';
	$field->fields('image')->notes = 'Dimensions: 800x600px';
});

@Boostdid you manage to do the overrides in the template settings? On my install this does not work.
Here are my field settings:

1700659072_Edit-Field-product_description--ziehm-ddev-site.thumb.png.bfebe50c906770b62520a9b9dcac0cb4.png

And here's how the override settings in a template look like:

296216552_Edit-Template-clinical-application--ziehm-ddev-site.thumb.png.b0e4c8261ead051a02f12f49bb23b10a.png

Just being curious how that looks on your install 🙂

In ProcessWire, template-based field overrides affect all pages using the same template. To achieve unique configurations per page, consider using separate fields or templates, ensuring individual control without affecting others. Be cautious when extending settings beyond module specifications, as it may lead to unexpected issues. Understanding and careful use of overrides is key to effective customization. 

Thanks for answering, I will try it.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...