Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » My Gallery Module Preview Code

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1695 Views
  • Yellow7 Jon
    Avatar
    Community Member
    39 Posts

    My Gallery Module Preview Code Link to this post

    So I'm still pretty new to silverstripe (about 3 or 4 days), so this may be covered elsewhere. If it is, oh well, I still learned a good bit about how the pages interact.

    In my on going quest to find a gallery preview script, one that would display X number of images out of a gallery and link to it, I just opted to create my own.

    On with the code!

    In the GalleryPage.php insert

    static $db

          'PreviewLimit' => 'Int',
          'PreviewWidth' => 'Int',
          'PreviewHeight' => 'Int',
          'PreviewCols' => 'Int'

    static $defaults --- You can use what ever defaults fit your needs best.

          'PreviewLimit' => 4,
          'PreviewWidth' => 75,
          'PreviewHeight' => 75,
          'PreviewCols' => 4

    RUN /db/build?flush=1

    in function getCMSFields( $cms ) --- this will add a new tab on the gallery page to edit the preview settings.

          $fields->addFieldsToTab( 'Root.Content.Gallery Preview', array(
             new HeaderField( _t('GalleryPage.GALLERYPREVIEWLAYOUT','Child Gallery Preview Layout') ),
             new NumericField( 'PreviewLimit', _t('GalleryPage.GALLERYPREVIEWLIMIT','Child Gallery Preview Limit') ),
             new NumericField( 'PreviewWidth', _t('GalleryPage.GALLERYPREVIEWWIDTH','Child Gallery Preview Image Width') ),
             new NumericField( 'PreviewHeight', _t('GalleryPage.GALLERYPREVIEWHEIGHT','Child GalleryPreview Image Height') ),
             new NumericField( 'PreviewCols', _t('GalleryPage.GALLERYPREVIEWCOLS','Child Preview Images Per Row') )
          ));

    INSERT THIS FUNCTION into the GalleryPage class

       function GalleryPreview () {
          $parent = $this->getParent(); // Get the data from the page you are on.
          if ( $parent->class != "GalleryPage" ) // if for some reason the page you are on is not a gallery, use the child gallery info.
             $parent = $this;
             
          $items = $this->GalleryItems( $parent->PreviewLimit ); // get the gallery
          $output = "<table class=\"gallery_preview\"><tr>";
          
          $x = 0; // column counter
          foreach ( $items AS $item )
          {   
             // Taken for word for word from GalleryItems... saw no need to add code to GalleryItems if this would only be used in a preview
             $ext = $item->getExtension();
             if( ! is_bool( strpos( GalleryPage_Extension::$exts[ 'Images' ][ 'Extensions' ], $ext ) ) ) { // ...create/Get the formatted image...
                $imgForThumbnail = $item->newClassInstance( "GalleryPage_Image" );
             }   
             if( method_exists( $imgForThumbnail, "generate{$this->ThumbnailType}" ) )
                $imgThumbnail = $imgForThumbnail->getFormattedImage( $this->ThumbnailType, $parent->PreviewWidth, $parent->PreviewHeight );
                // Uses $this->ThumbnailType instead of $parent->ThumbnailType to get the same type of thumbnail that will be displayed in the gallery
             else // Fall back to the de facto image resize method
                $imgThumbnail = $imgForThumbnail->getFormattedImage( 'ResizedImage', $parent->PreviewWidth, $tparent->PreviewHeight );
             
             // New row
             if ( !($x % $parent->PreviewCols) )
                $output .= "</tr><tr>";
                
             $output .= "<td width=\"".round(100 / $parent->PreviewCols)."%\"><img src=\"".Director::baseURL().$imgThumbnail->Filename."\" /></td>";
             $x++;
          }
          
          $output .= "</tr></table>";
          return $output;
       }

    EXAMPLE USE

    <% control Children %>
       <div class="gallery_preview"><a href="$Link">$Title</a><br />
          $GalleryPreview
       </div>
    <% end_control %>

    Hope this helps some new or even experienced users out!

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: My Gallery Module Preview Code Link to this post

    Useful code snippets work well in the documentation! You might like to make a page like 'Gallery Modifications' or something and link to it from the modules:gallery

  • Yellow7 Jon
    Avatar
    Community Member
    39 Posts

    Re: My Gallery Module Preview Code Link to this post

    i may just do that! thanks for the feed back

    1695 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.