CMB2 / cmb2-attached-posts

Custom field for CMB2 for attaching posts to a page.
GNU General Public License v2.0
135 stars 62 forks source link

Compatibility issue with repeatable fields #73

Open ctlcltd opened 1 year ago

ctlcltd commented 1 year ago

cmb2-attached-posts won't works with CMB2 repeatable fields.

Fields are cloned but not resetted, and there is an error with assigning draggable/droppable jquery-ui to cloned elements.

I solved this way:

if (window.CMBAP) {
  jQuery('.cmb-repeatable-group').on('cmb2_add_row', function(event, newRow) {
    var app = window.CMBAP;

    // draggables
    app.cache();
    app.makeDraggable();
    app.makeDroppable();

    // reset
    jQuery(newRow).find('.attached-posts-wrap').each(function() {
      var $wrap = jQuery(this);
      $wrap.find('.attached-posts-ids').val('');

      $wrap.find('.retrieved li').each(function() {
          jQuery(this).removeClass('added');
      });
      $wrap.find('.attached li').each(function() {
          jQuery(this).remove();
      });

      app.resetAttachedListItems($wrap);
      app.updateReadOnly($wrap);
      app.updateRemaining($wrap);
    });

    app.cache();
  });
}

:wave:

Ipstenu commented 1 year ago

@ctlcltd How are you calling this to make it work?

ctlcltd commented 1 year ago

the call is implicit on line 2 of the snippet, on the cmb2_add_row hook event

Il mar 3 ott 2023, 01:53 Ipstenu (Mika Epstein) @.***> ha scritto:

@ctlcltd https://github.com/ctlcltd How are you calling this to make it work?

— Reply to this email directly, view it on GitHub https://github.com/CMB2/cmb2-attached-posts/issues/73#issuecomment-1743934662, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABSBHJYW72GD2YPVZUDKMLX5NHXDAVCNFSM6AAAAAA2TXQMPKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONBTHEZTINRWGI . You are receiving this because you were mentioned.Message ID: @.***>

Ipstenu commented 1 year ago

@ctlcltd I mean, where in the PHP. I can make this a random named JS file but I gotta enqueue it. There are a bunch of different ways to do that, and I was curious what yours was.

ctlcltd commented 1 year ago

The snippet is a buffer fix, to apply it I used a js file hooked to the admin_enqueue_scripts hook from a php file of the theme. A definitive solution to be compatible with repeatable would be to modify the js/attached-posts.js script, something similar to the snippet but incorporated into the original source.