Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Use UploadField for upload media


Reply


3 Posts   517 Views

Avatar
SilverPeed

Community Member, 13 Posts

12 April 2014 at 2:30am

I use Silverstripe 3.1.4

Recently i mad a custom HTML5 mp3 player So it looks nice. What i want to do next is create a MusicObject witch i can use in a gridfield to upload a music file and give it a description, title, etc. The problem is thad i don't know how to use the UploadField for a mp3 file. Do i need to create a new data type? How would a mp3 data type look like or is there another way to use UploadFiled for uploading a mp3 file.
Something like this would be handy.

static $has_one = array(
   'musicfile' => 'mp3file',
);

I hope you guys have some tips on how to deal whit this.

Avatar
SilverPeed

Community Member, 13 Posts

18 April 2014 at 7:41pm

Edited: 25/04/2014 1:29am

I found the code beneath, i can upload now and save but the template is not showing the variable. probably because the "extends File" is not supported for SS3.1.4.

<?php

class MP3 extends File
{
   static $allowed_file_types = array (
      'mp3'
   );

   private static $player_count = 0;
      
   public function Player()
   {
      self::$player_count++;
      return $this->customise(array(
         'MP3Link' => Director::absoluteURL($this->URL),
         'Count' => self::$player_count
      ))->renderWith(array('mp3'));
   }
   
   public function forTemplate()
   {
      return $this->Player();
   }   
}
?>

--------------------------------------------------------------------
This is my MusicObject

<?php
class MuziekObject extends DataObject {
   
   static $db = array(
      'SortOrder'=>'Int',
'Titel' => 'Varchar',
'Eigenaar' => 'Varchar',
'GenreTag' => 'Varchar',
   );

   static $has_one = array(
      'Attachment' => 'MP3',
   'ConnectionToSoundPage' => 'SoundPage',
);
public static $summary_fields = array(
      'Titel' => 'Plaatsnaam',
      'Eigenaar' => 'Eigenaar'
   );

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields-> addFieldToTab('Root.Main', new UploadField('Attachment', 'Muziek bestand'));
$fields->removeFieldFromTab("Root.Main","ConnectionToSoundPageID");
$fields->removeFieldFromTab("Root.Main","SortOrder");
return $fields;
}
function link() {
      return $this->Attachment()->Filename;
   }
}
----------------------------------------------------------------------------------
In the template file is use this

<% loop MuziekObject %>
   <div class="audio-player">
       <div class="audio-title"><h2>Gitaarhero</h2><h2>$Titel</h2></div>
    <audio id="audio-player" src="assets/Muziek/$Attachment" type="audio/mp3" controls="controls"></audio>
      <% loop Attachment %> $MP3Link <% end_loop %>
   </div>
<% end_loop %>

Avatar
SilverPeed

Community Member, 13 Posts

3 May 2014 at 1:01am

I got an answer thanks to a friend of mine.

There is a class "File".

So you could build something like this:

<?php
class Mp3 extends DataObject {

private static $has_one = array(
      "Bestand" => "File"
'ConnectionToLogoPage' => 'MusicPage',
   );

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new UploadField('Bestand','Bestand'));
return $fields;
}
}