Forum

Thread tagged as: Question, Gallery

Gallery support for blocks

Hi,

A few weeks ago you helped me add galleries via blocks to blog posts...

https://forum.grabaperch.com/forum/01-21-2016-gallery-in-a-block

I'm trying to do the same but on pages. So I'm using the gallery list field in a block to select a gallery to display on a page.

On on template I have the select field...

<perch:content id="album" type="albumlist" label="Image Gallery"  suppress="true" />

On my page I have...

<?php perch_content('Page Content'); ?>

<?php
    $album = perch_content_custom('Page Content', array('skip-template'=>true));
    if (is_array($album)) {
        // ['album'] is the ID of the field to get the slug from
        $albumSlug = $album[0]['album']; 
        perch_gallery_album($albumSlug);
    }
?>

This works fine but not inside a block.

On the gallery change log it says support was added for blocks. Is there a new function for this? Or do I need to do it in a similar way that it was done on blog posts (https://forum.grabaperch.com/forum/01-21-2016-gallery-in-a-block).

Thanks, Shawn

Shawn North

Shawn North 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

How is it failing? Have you inspected the contents of $album ?

The contents of the album seem to be ok.

Array ( [0] => Array ( [_id] => 23 [_blocks] => Array ( [0] => Array ( [pageHeader] => Membership [_block_type] => pageHeader [_block_id] => o2nen3 [_block_index] => 0 ) [1] => Array ( [album] => Array ( [albumSlug] => family-bingo-night-2015 [_default] => family-bingo-night-2015 ) [_block_type] => imageGallery [_block_id] => o2pqhd [_block_index] => 1 ) 

But it does not display on the page if I have it set within a block

<perch:block type="imageGallery" label="Image Gallery">
    <!-- Image Gallery -->
         <perch:content id="album" type="albumlist" label="Image Gallery"  suppress="true" />
</perch:block>

If I set it outside the block it does display

<perch:content id="album" type="albumlist" label="Image Gallery"  suppress="true" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

So have you updated your code to match the shape of the data?

$albumSlug = $album[0]['album']; 

I'm not sure I follow.

My id in the field tag is album

<perch:content id="album" type="albumlist" label="Image Gallery"  suppress="true" />

Is it not correct to have 'album' here?

$albumSlug = $album[0]['album'];
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you look at the print_r() result, $album[0]['album'] doesn't exist. You need to update that reference to target the real location within the blocks.

So you are saying I have to filter through the blocks like we did on the blog post I referenced? I'm able to get and show the gallery in a block using the same way I did there. Is that the correct way to do it on a regular page?

if (count($album) && isset($album[0]['_blocks'])) {
        $page_album = false;
        foreach($album[0]['_blocks'] as $block) {
            if ($block['_block_type']=='imageGallery') {
                if (isset($block['album'])) {
                    $page_album = $block['album']['albumSlug'];
                    break;
                }
            }
        }

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

    }

Got it to work. Thanks!

Page...

<?php

    $page = perch_content_custom('Page Content',[], true);

    $results = perch_content_custom('Page Content', array('skip-template'=>true));

    if (count($results) && isset($results[0]['_blocks'])) {
        $page_album = false;
        foreach($results[0]['_blocks'] as $block) {
            if ($block['_block_type']=='imageGallery') {
                if (isset($block['album'])) {
                    $page_album = $block['album']['albumSlug']; 
                    break;
                }
            }
        }
    }
    echo str_replace('<!-- ALBUM HERE -->', perch_gallery_album($page_album, [], true), $page);

?>

Template...

<perch:block type="imageGallery" label="Image Gallery">
        <!-- Image Gallery -->
            <perch:content id="album" type="albumlist" label="Image Gallery"  suppress="true" />
            <!-- ALBUM HERE -->
</perch:block>