Skip to content

Commit

Permalink
fix: load assets only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed May 18, 2024
1 parent b23e68c commit 1c32703
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions tweaks/Inputfields/CheckAllCheckboxes/CheckAllCheckboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ class CheckAllCheckboxes extends Tweak
public function info(): array
{
return [
'description' => "Add checkbox to check all checkboxes in a field",
'description' => 'Add checkbox to check all checkboxes in a field',
];
}

public function ready(): void
{
$this->loadCSS();
$this->loadJS();
$this->wire->addHookAfter('InputfieldCheckboxes::renderReady', $this, 'loadJS');
$this->wire->addHookAfter('InputfieldCheckboxes::renderReady', $this, 'loadCSS');
}

}

6 comments on commit 1c32703

@adrianbj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @BernhardBaumrock - unfortunately it doesn't work at all when the assets are loaded like that. Could you please revert?

@adrianbj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe it would work with a before hook?

@BernhardBaumrock
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @adrianbj I didn't try but I don't think it would make a difference. I think it would be worth understanding why renderReady() is never called on the checkboxes inputfield, but I don't have time for that atm, so I've reverted to always load assets.

@adrianbj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BernhardBaumrock - I think this answers the question :)

image

The render hook works as expected.

@BernhardBaumrock
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but using the render() hook would mean that it does not work on ajax loaded inputfields, no? Shouldn't every inputfield trigger renderReady() automatically?

@adrianbj
Copy link
Contributor

@adrianbj adrianbj commented on 1c32703 May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but using the render() hook would mean that it does not work on ajax loaded inputfields, no?
It works with a checkboxes field inside a FieldsetTab that is AJAX loaded. Maybe there are other cases where it doesn't. I don't know.

Shouldn't every inputfield trigger renderReady() automatically?
I don't know about this either, but I thought if there is no hookable method in the inputfield class, then it isn't an option. InputfieldCheckboxes extends InputfieldSelectMultiple, which in turn extends InputfieldSelect, which does have a renderReady method, but it's not hookable. However, the core Inputfield class has a renderReadyHook (https://github.com/processwire/processwire/blob/3cc76cc886a49313b4bfb9a1a904bd88d11b7cb7/wire/core/Inputfield.php#L1474) which also works if you think that is better than renderReady

https://processwire.com/api/ref/inputfield/render-ready-hook/

Please sign in to comment.