Support » Plugin: Media Library Assistant » Att. Category display filtering with ACF

  • Resolved ratamatcat

    (@ratamatcat)


    Hi David,

    Not sure if you can help here with a related plugin query,

    I have a field created in Advanced Custom Fields and in the location rules in that plugin selected ‘Post Taxonomy’ where I can find an MLA Attachment Category I created, called ‘Overlay’. This would allow the display of the custom field on media edits screens according to that taxonomy only.

    Below is a screen grab of the ACF location rule selector:

    https://drive.google.com/open?id=1agm1x7LHiZ_c76CqlVNvBJbwZ2F0yXML

    For some reason when I add this rule the custom field disappears from the edit screen. When I only use the other rule (i.e. ‘Attachment is equal to All image formats’) the custom field returns again to the edit screen.

    I wonder if you have come across this type of thing before,

    thanks,

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for thinking of me and for posting the screen grab; very helpful.

    Tracing through the ACF code in class-acf-location-post-taxonomy.php, function rule_match() I found that the function is hard-coded to work only for posts and pages, not attachments. Lines 47 to 54 are:

    	function rule_match( $result, $rule, $screen ) {
    		
    		// vars
    		$post_id = acf_maybe_get( $screen, 'post_id' );
    		$post_terms = acf_maybe_get( $screen, 'post_terms' );
    		
    		// bail early if not a post
    		if( !$post_id ) return false;
    

    I was able to correct the problem by adding a few lines:

    	function rule_match( $result, $rule, $screen ) {
    		
    		// vars
    		$post_id = acf_maybe_get( $screen, 'post_id' );
    		if( !$post_id ) {
    			$post_id = acf_maybe_get( $screen, 'attachment' );
    		}
    
    		$post_terms = acf_maybe_get( $screen, 'post_terms' );
    		
    		// bail early if not a post
    		if( !$post_id ) return false;
    

    You can take this up with the ACF developers to see if they can incorporate something like this in the official code.

    I am marking this topic resolved because your question has been answered and the real solution is in the ACF plugin, not the MLA plugin. Good luck with finding an official solution.

    ratamatcat

    (@ratamatcat)

    Hi David, this looks awesome and I have forwarded the same to the ACF support,

    Elliot Condon

    (@elliotcondon)

    Hi @dglingren @ratamatcat

    Thanks for the info and solution! This looks good to me, I’ll add this to my to-do.

    Plugin Author David Lingren

    (@dglingren)

    @elliotcondon

    Thanks for your post. When you get to it, also have a look at line 55:

    $post_terms = acf_maybe_get( $screen, 'post_terms' );
    

    I noticed that it returned NUL for attachments, and this may cause a problem for the “if AJAX” case.

    Elliot Condon

    (@elliotcondon)

    The “post_terms” screen data should only exist when editing a post. ACF uses AJAX calls to update the metaboxes on screen, which doesn’t currently work with the Attachments media pages.

    I think this should be fine as is, but please correct me if you believe there is a bug.

    Plugin Author David Lingren

    (@dglingren)

    @elliotcondon

    You wrote “editing a post” and “Attachments media pages“.

    MLA extends taxonomy support to Media Library items. The issue in this topic affects the “Media/Edit Media” admin screen, which I think of as “editing an attachment”. I think of the “Attachments media page” as a front end screen.

    I haven’t found a specific bug and the simple fix I gave above seems to work for the Edit Media admin screen. Perhaps your AJAX calls are specific to the new Gutenberg editor?

    Elliot Condon

    (@elliotcondon)

    Hi David

    Apologies for the confusion.

    I’ve already implemented your original suggestion and will include this in the next release.

    That’s all good, but I would just like to clarify something else. In a previous reply, you mentioned a potential problem with the following code:
    $post_terms = acf_maybe_get( $screen, 'post_terms' );

    I noticed that it returned NUL for attachments, and this may cause a problem for the “if AJAX” case.

    To explain, the ‘post_terms’ data array will exist when ACF performs an AJAX request to fetch new metaboxes for the current post. This occurs when editing a post’s attribute such as category, format, parent, etc. It is not related to Gutenberg.

    Can you please talk me though what you meant by “this may cause a problem for the “if AJAX” case.”.

    Thanks
    E

    Plugin Author David Lingren

    (@dglingren)

    @elliotcondon

    Thanks for the update and the additional explanation. I’ve just run some experiments and here is what I found.

    I created a Field Group with the following rules. Show this field group if:

    Attachment is equal to All image formats AND
    Post Taxonomy is equal to Att. Category: abc
    OR
    Post Status is equal to Published AND
    Post taxonomy is equal to Category: aa new category

    If I go to the full-screen Edit Post screen, the field group appears/disappears when I add/remove the term assignment to “aa new category”. I assume this is the “AJAX request to fetch new metaboxes for the current post” case you described.

    If I go to the full-screen Edit Media screen, the field group DOES NOT appear/disappear when I add/remove the term assignment to “abc”. This is the AJAX problem I was worried about. If I click the “Update” button to refresh the screen, the field group does appear and disappear correctly.

    It’s a minor issue, but would be great to address. Let me know if there’s anything else I can do to help.

    Hi David,

    Thanks for the explanation – this makes sense.

    I’ll have a think about how I could extend the “Dynamic Metaboxes” logic to also allow for the attachments media modal.

    I’m hoping to add compatibility for the User edit pages, and will consider bundling these two improvements together.

    Plugin Author David Lingren

    (@dglingren)

    Great. I was going to mention the Media Manager Modal Window (the bane of my existence), which requires a significantly different approach. Thanks for working with me – I wish you every success with the coming ACF enhancements.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.