Home Forums Add-ons Gallery Field Default sort order when adding images Reply To: Default sort order when adding images

  • I guess the answer to that is it depends on how much work you want to do.

    You would need to add custom JavaScript. In this JS you would need to detect that the gallery has changed, as it happens I’ve been working doing just that for a project that I’m working on. I can give you some ideas for what’s involved, but I can give you details on the exact code to get it done.

    To trigger the change event you need to target all of the hidden fields in the gallery. For jQuery the selector looks like

    $('[data-key="field_589f92d2d394a"] input[type="hidden"]')

    where data-key is the field key of your gallery field.

    I can share some of my code with you, basically it is written in the same style as this JS from one of my dynamic field examples.

    My event is this

    'change [data-key="field_589f92d2d394a"] input[type="hidden"]': '_get_set_change_gallery',

    and this is the function I have so far

    _get_set_change_gallery: function(e) {
      // we use a timeout here because if several images are added all at once
      // then this function will be triggered several times
      // we can avoid runing this multiple times by using a timeout
      // we also want to set a timeout like with images to give them time to load
      if (window.gallery_change_timeout) {
      window.gallery_change_timeout = setTimeout(function() {
        window.gallery_change_timeout = null;
        // ****************************************
        // code to do what you want here
        // ****************************************
      }, 200);

    Your next step for what you wan to do you need to put where it says code to do what you want here.

    What you want to do is find the sort select field, change the value of that field and then trigger the change event on that field.

    Not sure if that helps you get to where you want to go.