sfMediaLibraryPlugin - 0.8.0

Asset management plugin

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source | Show as Markdown

sfMediaLibrary plugin

The sfMediaLibraryPlugin provides an easy-to-use interface to manage web assets (images, PDF documents, Flash objects, and so on). It also provides an extension to tinyMCE so that the media inclusion feature of rich text editors uses the assets uploaded through the plugin's module.

This plug-in consists of:

  • one module

  • one helper group

  • one JavaScript script


To install the plugin for a symfony project, the usual process is to use the symfony command line:

$ php symfony plugin-install http://plugins.symfony-project.com/sfMediaLibraryPlugin

Alternatively, if you don't have PEAR installed, you can download the latest package attached to this plugin's wiki page and extract it under your project's plugins/ directory. You will also have to copy the contents of the myproject/plugins/sfMediaLibraryPlugin/web/ directory into a myproject/web/sfMediaLibraryPlugin/ directory.

Enable the new module in your application, via the settings.yml file.

// in myproject/apps/frontend/config/settings.yml
    enabled_modules:        [sfMediaLibrary](default,)

Clear the cache to enable the autoloading to find the new classes:

$ php symfony cc

Start using the new module by making a request to:


The sfMediaLibrary module

The module allows for the upload, renaming and deletion of all types of files (image, pdf, spreadsheet, etc). Click on an asset name to rename it, click on the trashcan icon to delete it.

You can create, rename and delete subdirectories as well, resulting in a real tree structure that will keep your media assets organized.

Uploaded assets end up under the myproject/web/uploads/assets/ directory. This means that when you want to include them in a template with, for instance, an image_tag(), you must use an absolute path, as follows:

<?php echo image_tag('/uploads/assets/path_to_asset.suffix') ?>

Using the media library with TinyMCE

If you want to use the sfMediaLibrary plugin as a replacement for tinyMCE's file browser for image insertion, you must first initiate the plugin with a helper in the template:

<?php use_helper('sfMediaLibrary') ?>
<?php echo init_media_library() ?>

The next thing to do is to pass a special JavaScript callback function to the TinyMCE object at initialization. This is done with the tinymce_options option of the textarea_tag() helper:

<?php echo textarea_tag('content', *, array (
  'rich' => true,
  'tinymce_options' => 'file_browser_callback:"sfMediaLibrary.fileBrowserCallBack"',
  )); ?>

That's it, the TinyMCE file browser is now the sfMediaLibrary's one.

Tip: For use with an admin generated module, place the init_media_library() call in the _edit_header.php partial in the templates/ directory of the module. To define the callback in a generato.yml, do as follows:

  class:              sfPropelAdminGenerator
    model_class:      Post
    theme:            default

        content: { params: rich=true tinymce_options=\'file_browser_callback:"sfMediaLibrary.fileBrowserCallBack"\' }

Using the Media Library input_file helper

Traditionally, when an asset has to be referenced in a form, a file input is used (<input type='file'>). It allows the user to upload a file from his computer to the server, and this file can be further embedded in a rich text content.

Using the sfMediaLibrary, you can change this control into some sort of file input that wouldn't refer to the client's computer, but to the media library itself, or, to put it differently, to the files located in web/uploads/assets/. The plugin comes with a special helper for that purpose, the input_asset_tag(). Use is just like a regular input_tag():

<?php echo form_tag('foo/bar') ?>
  <?php echo input_asset_tag('my_asset_field', *) ?>

The input is not a real file input tag, meaning that the chosen asset file will not be posted woth the request. It is iseless anyway, since the file is already on the server. Instead, the action will be able to retrieve the asset file path relative to the web root, exactly what is needed to display it.

Optionally, you can restrict the choice of possible assets in this input to images only, as follows:

<?php echo input_asset_tag('my_asset_field', '', array('images_only' => true)) ?>


  • Add screenshots to the wiki description
  • Improve CSS and design
  • Improve compatibility
  • Allow to move an asset to another directory
  • Make path clickable to go up in the tree structure


2007-04-13 | 0.8.0 Beta

  • francois: Initial release