[5.x] Ensure attached only once

Created on 30 March 2022, over 2 years ago
Updated 3 September 2024, 26 days ago

Problem/Motivation

Fix flaws in PhotoswipeAssetsManager. See MR.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

5.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡¦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
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡ΊπŸ‡¦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 cache

    Also 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.
  • Assigned to bobi-mel
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡¦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

  • Pipeline finished with Success
    8 months ago
    Total: 319s
    #91352
  • Status changed to RTBC 8 months ago
  • πŸ‡ΊπŸ‡¦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 about 1 month ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks, let's remove that old disabled stuff.

  • Pipeline finished with Skipped
    about 1 month ago
    #259087
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024