Drupal Tutorial - Creating an embedded video or audio player for a link field
Use Case:
Objective
- As an administrator or a content editor you want to add a link to your content type.
- To the end user you want the link to be displayed as an embedded player.
Example of Adminstrative Workflow
- Creating a new field
- Add the url of the media file.
- The end result. An embedded audio player
Question
How do we do this?
Enter the custom formatters module
With the custom formatters module you can create custom displays of the link
- After you downloaded and enabled the module goto Admin -> Structure -> Formatter. Then click add. Note: you may need to clear the cache to see the Formatters menu button
- Under “Field type(s)” -> Enter link_field into the text field
- Under “Formatter” I've added this code below. Note: if you are copying and pasting, do not include the <?php ?> tags.
<!--?php
foreach ($variables['#items'] as $item) {
$url = $item['display_url'];
print "<div class='embedded-item'-->"; print "
"; print "
"; print ""; } ?>
- Now goto Admin -> Structure -> Content type -> [type] -> Manage Display
And viola! You now have a podbean player embedded on your site. If you want another player, embed its code into the custom formatter field.
Comments
Mind if I link to this?
Hey there!
I'm the maintainer of the Link module, and I sometimes have people asking me for how to do something like this. Would you mind if I linked to this blog post from the link module page?
Yes go ahead :)
Yes go ahead :)
Syntax Error
I tried this, and keep getting an unexpected T_STRING in (Pathname) \sites\all\modules\custom_formatters\engines\php.inc(81) : eval()'d code on line 5. Any tips? I've tried removing various bits of code to no avail. I'm using D7.17 with version 7.x-2.2 of Custom Formatters.
how does this work behind the scenes?
Just out of sheer curiousity:
What happens 'behind the scenes' so to speak when somebody clicks that link if is an external link? Is the media file directly streamed from the external site? Or will drupal actually load an instance every time the page is requested?
Re format
Could you please reformat the code part in there?
Wow, thanks
I looked for this formatter. Thanks
Add new comment