Forum

Thread tagged as: Problem, Blog, Gallery

Gallery in a block

Hi,

I'm having a problem getting a gallery to work in a block. I'm adding a gallery selection menu in the blog post form like this and it works fine.

<perch:blocks order="4">
<perch:block type="ImageGallery" label="Image Gallery">
    <perch:blog id="album" type="albumlist" label="Blog Gallery" suppress="true" />
 </perch:block>
</perch:blocks>

In my post.php file I have this to get the selected gallery...

<?php
    $blog_album = perch_blog_post_field(perch_get('s'), 'album', true);
    perch_gallery_album($blog_album);
?>

Then display using the template...

<perch:before>
<div id="owl-slideshow-multi" class="itemsScaleUp-true owl-carousel owl-theme">
</perch:before>

    <div class="item">
        <a href="<perch:gallery id="main" />" class='strip' data-strip-group-options="loop: true, maxWidth: 800" data-strip-caption="{$item.caption}" data-strip-group="{$item.belongs_to_gallery}" title="{$item.caption}">
            <img src="<perch:gallery id="main" />" width="<perch:gallery id="small-w" />" height="<perch:gallery id="small-h" />" alt="<perch:gallery id="imageAlt" />" />
        </a>
    </div>


<perch:after>
</div>
</perch:after>

All of this works fine if I don't put the Gallery Select menu in a block. But if it is in a block the gallery does not show. No output at all.

Shawn North

Shawn North 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Try adding scope-parent="true" on your perch:blocks tag, and then using parent.album from within the block.

I added scope-parent="true" to the blocks tag but not sure exactly where to add parent.album from within the block.

<perch:blocks scope-parent="true" order="4">
<perch:block type="ImageGallery" label="Image Gallery">
    <perch:blog id="album" type="albumlist" label="Blog Gallery" suppress="true" />
 </perch:block>
</perch:blocks>

Thanks

From the docs I think I see where I should change id="album" to id="parent.album"

<perch:blocks scope-parent="true" order="4">
<perch:block type="ImageGallery" label="Image Gallery">
    <perch:blog id="parent.album" type="albumlist" label="Blog Gallery" suppress="true" />
 </perch:block>
</perch:blocks>

I did that but now i get a block with no contents when trying to add and select a gallery. The block shows but the selection box is not there.

I have many more block between this same blocks tag. Should all of their id's be change to parent. ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, I've got this backwards. I see the source of the album is in the blocks - I misunderstood.

Roll back those changes I just had you make - apologies.

The issue is that this line is going to be insufficient to access the content within blocks:

$blog_album = perch_blog_post_field(perch_get('s'), 'album', true);

I think you're going to need to use perch_blog_custom() with the skip-template option to get the data back. How's your PHP?

No worries, I have rolled it back now.

Well my PHP skills are minimal at best. Mostly just find snipped code and try and make it work. lol

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, can you do this and let me know what it outputs?

$result = perch_blog_custom([
    'filter' => 'postSlug',
    'value' => perch_get('s'),
]);
print_r($result);

Sure, this outputs my blog post. When placed in the blog/post.php file. It outputs the post contents using the perch/templates/post.html template.

I should also say it did not include the gallery. Here is a link

https://192.185.195.67/~eisenhowerpta/blog/post.php?s=2015-08-25-jog-a-thon

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, I missed an attribute. That'll teach me to try and be helpful at 9pm on a Friday night!

$result = perch_blog_custom([
    'filter' => 'postSlug',
    'value' => perch_get('s'),
    'skip-template' => true,
]);
print_r($result);

No problem, It's very much appreciated. :) So adding that produced several arrays in-between normal content. You can see from the link in my last post. I see that the imageGallery block was also included in the arrays.

[perch_image] => /~eisenhowerpta/perch/resources/jog-a-thon.png [perch_event_date_time] => Friday, September 18th 2015 [perch_categories] => Array ( [0] => 2 ) [perch_og_title] => Jog-A-Thon [perch_og_description] => This is Eisenhower PTA's major fundraiser event for the year. It is big! So much of what the PTA can do for the school depends on the success of this event. It's easy and fun to participate. [perch_og_image] => [perch_og_type] => article [perch_intro] => [perch__blocks] => Array ( [0] => Array ( [album] => Array ( [albumSlug] => jog-a-thon [_default] => jog-a-thon ) [_block_type] => imageGallery [_block_id] => o1eymy [_block_index] => 0 ) [1] => Array ( [align] => left [h2text] => Array ( [_flang] => markdown [raw] => On Your Marks... Get Set...JOG-A-Thon! [processed] => 
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so we need to find the album slug in the data. Something like this should do it:

$result = perch_blog_custom([
    'filter' => 'postSlug',
    'value' => perch_get('s'),
    'skip-template' => true,
]);
if (count($result) && isset($result[0]['_blocks'])) {
    $blog_album = false;
    foreach($result[0]['_blocks'] as $block) {
        if ($block['_block_type']=='imageGallery') {
            if (isset($block['album'])) {
                $blog_album = $block['album']['albumSlug'];
                break;
            }
        }
    }

    if ($blog_album) {
        perch_gallery_album($blog_album);
    }   
}

Yep that works! Awesome! Only problem is like other blocks it does not re-position on the page when moved in different order. It only shows on the bottom. I guess because I'm first calling the blog contents and then the gallery block?

<?php perch_blog_post(perch_get('s')); 

                $result = perch_blog_custom([
                    'filter' => 'postSlug',
                    'value' => perch_get('s'),
                    'skip-template' => true,
                ]);
                if (count($result) && isset($result[0]['_blocks'])) {
                    $blog_album = false;
                    foreach($result[0]['_blocks'] as $block) {
                        if ($block['_block_type']=='imageGallery') {
                            if (isset($block['album'])) {
                                $blog_album = $block['album']['albumSlug'];
                                break;
                            }
                        }
                    }

                    if ($blog_album) {
                        perch_gallery_album($blog_album);
                    }   
                }

            ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Where it's output depends on where you then include the

perch_gallery_album($blog_album);

Right but how can I get it to where it is part of the rest of the blocks on the page. As it is now it is in a block but I cant reorder on the page like a normal block. So it kind of defeats the purpose.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I would string replace it.

In your block template, add something like <!-- ALBUM HERE -->

Then output your post to a variable.

$post = perch_blog_custom([
    'filter' => 'postSlug',
    'value' => perch_get('s'),
], true);

and replace the string as you echo it.

echo str_replace('<!-- ALBUM HERE -->', perch_gallery_album($blog_album, [], true), $post);

That did it. Thanks for getting me through that, works great now.