Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » media filter

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

Page: 1
Go to End
Author Topic: 602 Views
  • Anatol
    Avatar
    126 Posts

    media filter Link to this post

    Hi,

    I just thought this might be worth sharing. I created a media filter that parses the page content for any links to media files (such as .mp4, .m4v, .flv, etc.) and then automatically embeds a player.

    This is just some very quick code that can be improved a lot and I might write a proper module/extension for this if I find the time, but so far it this seems to work well.

    Put this into your Page class in /mysite/code/Page.php

    function Content() {
       return $this->MediaFilter($this->Content);
    }
       
    function MediaFilterCallback($matches) {
       $mediaplayer_id = 'mediaplayer-'.md5($matches[2].$matches[3]);

       $output = $matches[0].'<br />';
       $output .= '<div id="'.$mediaplayer_id.'"></div>';
       $output .= '<script type="text/javascript">';
       $output .= "jwplayer('".$mediaplayer_id."').setup({";
       $output .= "'flashplayer': 'mediaplayer/player.swf',";
       $output .= "'image': 'mediaplayer/preview.jpg',";
       $output .= "'file': '".$matches[2].".".$matches[3]."',";
       $output .= "'controlbar': 'over',";
       $output .= "'duration': '34',";
       $output .= "'fullscreen': 'true',";
       $output .= "'screencolor': 'eeeeee',";
       $output .= "'stretching': 'uniform',";
       $output .= "'width': '540',";
       $output .= "'height': '405'";
       $output .= "});";
       $output .= "</script>";
       
       return $output;
    }

    function MediaFilter($subject) {
       // links with the following file extensions will be replaced by an embedded player
       $search = '/<a(.*?)href=\"([^<]+)\.(m4v|mp4|mov|flv|3gp|3g2|mp3|aac|m4a)\"([^>]*)>(.*?)<\/a>/is';
       return preg_replace_callback($search,array('Page','MediaFilterCallback'),$subject);
    }

    Add this line into the <head> of your template file /themes/mytheme/templates/Page.ss :

    <script type="text/javascript" src="mediaplayer/jwplayer.js"></script>

    Then download the JWPlayer into your silverstripe directory, unzip and name the downloaded folder "mediaplayer".

    Done. All code should now get parsed for media links and be replaced by an embedded media player.

    As I said this could still be improved a lot, i.e. create a proper module with a config file for file types and player configuration, Requirements::javascript, etc. I'll post this if I get around to do that.

    Cheers!
    Anatol

    602 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.