Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Addable Popup with thumbnail (initialization problem)

Open albertso opened this issue 11 years ago • 27 comments

Hi,

I'm displaying thumbnails in an addable popup option (could also be addable box), like this: image

As you see it works fine when the image is selected after page load with the wp.media frame. It doesn't show the images again when the page is loaded after being saved:

image

If I enter the popup and change the image, it does update correctly, but only if I change it with the media frame. I've tried this in page and shortcode options. The same behaviour occurs.

This is the option:

$options = array(
    'backgrounds' => array(
        'type'          => 'addable-popup',
        'label'         => __('Background(s)', 'fw'),
        'desc'          => __('Add a background to this section. Add more backgrounds to create a slideshow.', 'fw'),
        'template'      => ''.
            '<div class="fw-row">'.
            '{{ if ("custom"==image.type && image.custom) { }}'.
                '{{ if (image.custom) { }}'.
                    '<div class="fw-col-xs-2">'.
                        '{{ if ("custom"==image.type) { }}'.
                            '{{ console.log(image.custom) }}'. // returns attachment_id
                            '{{ console.log(wp.media.attachment(image.custom)) }}'. // returns attachment object
                            '{{ if (image.custom) { }}'. //
                                '{{ if (wp.media.attachment(image.custom).attributes.sizes) { }}'.
                                    '<img class="item-thumb" src="{{-wp.media.attachment(image.custom).attributes.sizes.thumbnail.url}}"> '.
                                '{{ } }}'.
                            '{{ } }}'.
                        '{{ } else if ("predefined"==image.type) { }}'.
                                '<img class="item-thumb" src="{{-image.data.icon}}"> '.
                        '{{ } }}'.
                    '</div>'.
                '{{ } }}'.
            '{{ } }}'.
            // ...
            '</div>'.
            '',
        'size'          => 'medium',
        'limit'         => 10,
        'popup-options' => array(
            'image'     => array(
                'type' => 'background-image',
                'label' => __('Background Image', 'fw'),
                'images_only' => true,
            ),
            // ...
        ),
    ),
);

This is the console log in Chrome. See, the first time I look in the object returned, the attributes are empty. After changing the image, the object in the log does have attributes.

addable_template_console

Looks like some kind of initialization of wp.media is missing when addable options have have interior options with images. Or am I missing something?

I thought I can get and send the values in php with localize script, but that would require looking at page options and scanning the page content looking for shortcodes that have images in addable popups. The same for options in customizer. Any advice? Thanks!

albertso avatar Oct 08 '15 21:10 albertso

@albertso please watch this video. Hope this helps. I am waiting for your response.

/* Code from video */
wp.media.query().more().done(function(){
console.log('RESPONSE',wp.media.attachment('your-id').toJSON());
})

llemurya avatar Oct 09 '15 07:10 llemurya

try this template example for a sec , worked fine for me here in builder https://github.com/ThemeFuse/Unyson/issues/759#issuecomment-127020295

did you try with

image.url 

instead ?

danyj avatar Oct 09 '15 11:10 danyj

works perfectly fine for me , http://prntscr.com/8pfilc

same thing you have , do this

{{ console.log(image);if (image) { }} :  <img  style=\'width:100px;height:auto;\' src=\'{{-image.url }}\' /> {{ } }}

adding , saving , changing , removing , works fine

danyj avatar Oct 09 '15 11:10 danyj

I see now , you are looking for the thumbnail instead of the actual image thus the wp.media fiddle ,

danyj avatar Oct 09 '15 12:10 danyj

@danyj, Hey yes, I'm trying to get the thumbnail (or any registered size) from the attachment instead of the full size. The thing is it almost works, at least after wp frame has been opened once.

@llemurya I followed your video and now I manage to see the image object in the console.log, on a saved page, but I still cannot print the same data to html. The image src appears blank.

I don't see how I should include the code you gave me in the $option['template']. Should I place it at the top, or nest my code in there? Should I use some kind of callback? I can't work it out. I'm awful at js and and asynchronous code...

Any advice on how to structure it to include @llemurya's code?

albertso avatar Oct 14 '15 18:10 albertso

@albertso yesterday i added the _context parameter in the addable-popup template that will be available in the next version of the Unyson, please watch this video I hope it solve your problem, please give us a feedback.I am waiting for you response. Below is the code that you must paste in the template key.

var image = "<img>";
print(image);
wp.media.query().more().done(function () {
    attachment = wp.media.attachment('your_attachment_id').toJSON();
    setTimeout(function () {
        _context.find("img").attr("src", attachment.url);
    }, 500);
});

llemurya avatar Oct 16 '15 08:10 llemurya

Please see this commit https://github.com/ThemeFuse/Unyson/commit/8c8a0ff995ea8f3295f858308a93b895511ba4bb and add it mannualy to test the code. In the next version it will be in production.

llemurya avatar Oct 16 '15 08:10 llemurya

@albertso please write an answer in order to know if it helps you and to fix the issue. I am waiting for your response.Thanks!

llemurya avatar Oct 29 '15 08:10 llemurya

Hey, sorry for the late response.

I managed to do what you show in the video already, thanks for helping out. I even have the data storing correct, I use the images in the frontend no problem.

The problem comes after saving the post. Have you tried saving that page, and opening again? Do the saved boxes display the images?

New boxes you add work, but the stored ones don't show the thumbnail.

If media frame has not been launched before drawing the boxes, wp.media doesn't return a complete attachment object (with all the attributes.sizes and data)

In my case, I have the addable-box in a shortcode, so the script is run when I click to edit that shortcode and the modal opens, therefore I can open the media frame before that, so when I click on the shortcode, the thumbnails in the addable-box show ok. I cannot do that for page-options (because the script is run on page load, and media frame has not run at that moment)

Even if I use wp.media.query().more().done() as you suggest, it doesn't work if the media frame wasn't open before rendering the option. Even using with the Timeout.

8c8a0ff doesn't solve it either...

There's some kind of initialization missing. I don't know what happens exactly when media frame opens (in the many cases it does), but when it does, something must "activate", that allows querying wp.media to get attachment data

As I say, my code works (and worked) perfectly like yours. It's in the situation I describe above, (after page save), that stored boxes don't _display_ as expected.

Functionality-wise, it works (that's why I deprioritized this issue)

albertso avatar Oct 31 '15 17:10 albertso

Btw, thanks for looking into this, and all the great work

It's not the biggest issue, but it bugs me 'cause solution seems almost there,

albertso avatar Oct 31 '15 19:10 albertso

@albertso it works after saving, please see this video as a proof.I make wp.media.query().more().done() because when the promise is resolved it gets the correct attachments list .

llemurya avatar Nov 01 '15 12:11 llemurya

Ok I see, then I'm doing something wrong. I'll let you know when I'm done, but I'll close the issue. Thanks for the patience!

albertso avatar Nov 02 '15 10:11 albertso

@albertso , bud , do you mind sharing the template snippet for thumbs ? I am doing something similar and need to extract them. thnx!

danyj avatar Feb 19 '16 10:02 danyj

what I noticed is that it works on some images and not on the others, very strange ,

danyj avatar Feb 19 '16 20:02 danyj

@danyj in this comment is the solution https://github.com/ThemeFuse/Unyson/issues/923#issuecomment-148647851

llemurya avatar Feb 19 '16 20:02 llemurya

@llemurya hey bud , that is exactly what I am using , and some images dont display screenshot_2

I added console log after attachment

                    wp.media.query().more().done(function () {
                        var aid = medsrc.attachment_id;
                        var attachment = wp.media.attachment(aid).toJSON();

                        console.log(attachment);

danyj avatar Feb 19 '16 20:02 danyj

@llemurya , I also tried with very long timeouts but same thing , also checked db that image has all attributes and it does

danyj avatar Feb 19 '16 20:02 danyj

tested your exact code with specific attachment id , same

wp.media.query().more().done(function () {
    attachment = wp.media.attachment(5482).toJSON();
    setTimeout(function () {
        console.log(attachment);
    }, 500);
});

screenshot_3

and is missing all atrr

danyj avatar Feb 19 '16 21:02 danyj

Hey! I finally got it to work fiddling with @llemurya 's code.

The purpose of this is to use thumbnail-sized versions of images, instead of full sizes.

$options = array(
    'items' => array(
        'type' => 'addable-popup',
        'label' => __('Items', 'fw'),            
        'template'    => '{{'.
            'if (typeof multimedia != "undefined") {
                if ("selected" in multimedia) {
                    if (typeof multimedia.selected != "undefined") {
                        if (multimedia.selected in multimedia) {
                            var m = multimedia[ multimedia.selected ];
                            switch (multimedia.selected) {
                                case "image" : // Attention down here
                                    var attachment = wp.media.attachment(m.src.attachment_id)
                                    var image = "<img>";
                                    print(image);
                                    wp.media.query().more().done(function () {
                                        setTimeout(function (){
                                            if (typeof attachment.attributes != "undefined") {
                                                if (typeof attachment.attributes.sizes != "undefined") {
                                                    _context.find("img").attr("src", attachment.attributes.sizes.thumbnail.url);
                                                }
                                            }
                                        }, 50);
                                    });
                                break;
                                // other cases ...
                            }
                        }
                    }
                }
            } }}',
        'popup-options' => array(
            'multimedia'     => array( // follow slider structure
                'type'         => 'multi-picker',
                'label'        => false,
                'picker'       => array(
                    'selected' => array(
                        'label' => _( 'Type', 'fw' ),
                        'type'  => 'select',
                        'value' => '',
                        'choices' => array(
                            'image' => __('Image', 'fw'),
                        ),
                    ),
                ),
                'choices' => array(
                    'image' => array(
                        'src' => array(
                            'label' => false,
                            'type' => 'upload',
                        ),
                    ),
                ),
            ),
        ),
    ),
);

There is a slight delay before the thumbnails appear, but they appear now. And I wonder if there is a better way to access object properties safely without doing:

    if ("selected" in multimedia) {
        if (typeof multimedia.selected != "undefined") {
            // multimedia.selected

I haven't done much testing, but it seems to work so far. If there's nothing else to add, this may not be an issue any more.

Thanks for the help!

BTW Great work @moldcraft, @andreiglingeanu, @thednp, @danyj, #1807 shortcodes in wp-editor!! Cool :+1:

albertso avatar Sep 05 '16 23:09 albertso

@albertso Refer to this as a reliable way of preloading your attachments. You should really do this outside of the template - template rendering should be sync (without any timeouts or AJAX requests) or you'll get into trouble someday.


Just do this in


fwEvents.on('fw:options:init', function (data) {
  // check if data.$elements contains your option
  // and do preloading here
})

// or maybe even earlier on page load

jQuery(function () {
  // do preload
})

andreiglingeanu avatar Sep 07 '16 07:09 andreiglingeanu