Modal window from image button

I’m trying to create a custom module that has a button item contain a logo, modal pop-up description, logo name field. I can get the modal pop-up to work when there is no “item” but as soon as I create the item to group the fields, it breaks.

The reason I’m putting it in a grouped item is because I need to put multiple logos with modals on a page. (see attached for what I’m trying to take their static images and place modals over https://www.mbassett.com/our-clients)

This is the code before putting it in grouped items (which works):

<!-- The button -->
<a href=“#{{ widget.modal_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“open-popup-link”>
<img src=“{{ widget.logo.src }}” alt=“{{ widget.logo.alt }}”>
</a>

<!-- the modal popup is hidden until the button is clicked -->
<div id=“{{ widget.modal_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“white-popup mfp-hide”>
{{ widget.logo_name }}
{{ widget.modal_popup }}
</div>

<!-- Copy and paste your css file between the quotations in the href below -->
<link rel=“stylesheet” href=“CSS_File_URL_Goes_Here”>
<style>
.white-popup {
position: relative;
background: #FFF;
padding: 20px;
width: auto;
max-width: 500px;
margin: 20px auto;
}
</style>

<!-- Copy and paste your js file between the quotations in the src below -->
<script src=“{{ get_asset_url(‘/Martyn_Bassett_November2018 Theme/Coded Files/Logo PopUp Modal.js’) }}”></script>
<script> $(document).ready(function() {
$(‘.open-popup-link’).magnificPopup({
type:‘inline’,
midClick: true
});
});
</script>

This is the code I’m trying to get to work for grouped item “logo_item”:

<!-- The button -->
<a href=“#{{ widget.item.modal_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“open-popup-link”>
<img src=“{{ widget.item.logo.src }}” alt=“{{ widget.item.logo.alt }}”>
</a>

<!-- the modal popup is hidden until the button is clicked -->
<div id=“{{ widget.item.modal_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“white-popup mfp-hide”>
{{ item.logo_name }}
{{ item.modal_popup }}
</div>

<!-- Copy and paste your css file between the quotations in the href below -->
<link rel=“stylesheet” href=“CSS_File_URL_Goes_Here”>
<style>
.white-popup {
position: relative;
background: #FFF;
padding: 20px;
width: auto;
max-width: 500px;
margin: 20px auto;
}
</style>

<!-- Copy and paste your js file between the quotations in the src below -->
<script src=“{{ get_asset_url(‘/Martyn_Bassett_November2018 Theme/Coded Files/Logo PopUp Modal.js’) }}”></script>
<script> $(document).ready(function() {
$(‘.open-popup-link’).magnificPopup({
type:‘inline’,
midClick: true
});
});
</script>

Can someone please tell me what I’m doing wrong?

@Kevin-C

any words of wisdom for @JCaulcutt ?

Thanks @dennisedson

Hey @JCaulcutt

I wonder how you’re HUBL is actually outputting the HTML? And then how you’ve modified you JS to match?

One possible approach:

Assuming the HUBL is generating a modal for every button. We will need to pass magnificPopup the ID of the modal element we want to open. See the documentation for inline elements here.

EDIT: WAS BAD JS

This is assuming that ID is unique!

If that is not the case we 'll need to come up with a way to make it so.

This works when i have a one-time use of the grouped fields but as soon as I put the grouped fields to “repeater options” turned on then it doesn’t work. I feel like I’m not naming the fields properly when they’re repeated. I’m including my code below as well as the screen shot of field items in the group.

HTML:

<a href=“#test-popup” class=“open-popup-link”><img src=“{{ widget.item.logo.src }}” alt=“{{ widget.item.logo.alt }}”></a>

<!-- Popup itself -->
<div id=“test-popup” class=“white-popup mfp-hide”>
Popup content
</div>

CSS:

.white-popup {
position: relative;
background: #FFF;
padding: 20px;
width:auto;
max-width: 500px;
margin: 20px auto;
}

JS:

$(‘.open-popup-link’).magnificPopup({
type:‘inline’,
midClick: true // allow opening popup on middle mouse click. Always set it to true if you don’t provide alternative source.
});

for them to show the image as a button and then trigger the modal.

Any solutions for this?

Ohhh my bad @JCaulcutt

You’re loop should look something like this. Note how we access the properies of each “item”:

<!-- HTML & HUBL -->

{% for item in module.logo_item %}
 <img data-id ="{{ '#test-popup' ~ loop.index }}" class="test-img" src="{{ item.logo.src }}" alt="{{ item.logo.alt }}" width="100" />

 <!-- Popup itself -->
	<div id="{{ 'test-popup' ~ loop.index }}" class="white-popup mfp-hide">
		{{ item.modal_popup }}
	</div>
{% endfor %}
/* CSS */

.test-img {
 cursor: pointer;
}​
// JS

$('.test-img').each(function(index, el) {
 $(this).magnificPopup({
 items: {
 src: $(this).data('id'),
 type: 'inline',
 midClick: true
 }
 });
});

This is kind of a down and dirty implementation that could use cleaned up.

This works when i preview from the modal. As soon as I try and put it into a page then the fields (“modal_popup” and “logo_name”) within the pop-up don’t appear, only the modal box appears.

Do you know why it would be different from module code to actual page preview?

HTML:

<!-- HTML & HUBL -->

{% for item in module.logo_item %}
<img data-id =“{{ ‘#test-popup’ ~ loop.index }}” class=“test-img” src=“{{ item.logo.src }}” alt=“{{ item.logo.alt }}” width=“190” />

<!-- Popup itself -->
<div id=“{{ ‘test-popup’ ~ loop.index }}” class=“white-popup mfp-hide”>
{{ item.logo_name }}
{{ item.modal_popup }}
</div>
{% endfor %}

CSS:

.white-popup {
position: relative;
background: #FFF;
padding: 20px;
width:auto;
max-width: 500px;
margin: 20px auto;
}

.test-img {
cursor: pointer;
}

JS:

$(‘.test-img’).each(function(index, el) {
$(this).magnificPopup({
items: {
src: $(this).data(‘id’),
type: ‘inline’,
midClick: true
}
});
});

@Kevin-C This actually ended up working. I realized I had made a small mistake but your solution worked perfectly for me.

Thanks for your help!