- πΊπ¦Ukraine dinazaur
This could be achieved with such approach.
public function attach(array &$element) { .. $element['#attached']['library'][] = 'photoswipe/photoswipe.init'; if ($this->isAttached()) { // Add photoswipe js settings. $options = $this->config->get('options'); // Allow other modules to alter / extend the options to pass to photoswipe // JavaScript. $this->moduleHandler->alter('photoswipe_js_options', $options); $this->themeManager->alter('photoswipe_js_options', $options); $element['#attached']['drupalSettings']['photoswipe']['options'] = $options; $this->attached = TRUE; } }
Which such code, we would attach drupalSettings once. And it does not matter where we set drupalSettings, they will be attached anyway.
- Status changed to Needs work
over 1 year ago 10:58am 14 June 2023 - πΊπ¦Ukraine dinazaur
We cannot attach it "once" because there might be cases when the element to which it was attached was not rendered. e.g. #access = false and all subsequent elements to which it should be attached will not contain a library at all.
So the solution is only one in that case. We can cache result of
photoswipe_js_options
hook since it does not contain the actual element so users cannot detect which element is used now, so they always return the same result theoretically. It means that we can cache it without worries. By cache, I mean static variable once per request. I don't see reasons why we need to use actual cacheAlso if there are override options we should not cache it and trigger our hooks.
That's the only thing that we can do here.
- π©πͺGermany Anybody Porta Westfalica
Thanks for your investigations @dinazaur! Or should we simply close it and leave things as-is? What would you suggest?
- πΊπ¦Ukraine dinazaur
Eh, our developer almost finished this implementation, don't want to throw its work away. But I also don't like this thing. I guess we can at least remove all stuff related to this issue, like this->isAttached etc. I will tell him to remove it.
- First commit to issue fork.
- Merge request !119Issue #3272485 by Anybody, Grevil: [5.x] Ensure attached only once Changed the... β (Merged) created by Unnamed author
- Assigned to bobi-mel
- Status changed to Needs review
11 months ago 1:26pm 9 February 2024 - πΊπ¦Ukraine bobi-mel
Hi @dinazaur @Anybody
I returned the implementation of this method to the current implementation as in the 5.x branch, but removed the isAttached() method as useless - Status changed to RTBC
11 months ago 2:05pm 9 February 2024 - πΊπ¦Ukraine dinazaur
Looks good to me.
Just for reference here is logic from #16 https://git.drupalcode.org/project/photoswipe/-/merge_requests/119/diffs...
- Issue was unassigned.
- Status changed to Fixed
4 months ago 10:16am 20 August 2024 - π©πͺGermany Anybody Porta Westfalica
Thanks, let's remove that old disabled stuff.
-
Anybody β
committed 1145daba on 5.x authored by
bobi-mel β
Issue #3272485 by Anybody, bobi-mel, dinazaur, grevil: [5.x] Ensure...
-
Anybody β
committed 1145daba on 5.x authored by
bobi-mel β
Automatically closed - issue fixed for 2 weeks with no activity.