Elgg returning ElggObject instead of ElggFiles

Hi,

I'm new to working with Elgg. For my workplace I am attempting to create a system that can upload motion capture data and output the animation inside a custom widget. I am having difficulty retrieving the data once I upload it.

$uploaded_file = elgg_get_uploaded_file('upload');
$file = new ElggFile();
$file->subtype = 'motion_data';
if (!$file->acceptUploadedFile($uploaded_file))
{
return elgg_error_response(elgg_echo('file:uploadfailed'));

}
$file->save();

It appears that Elgg will upload my file just fine. I can also see that the file is now stored inside the data folder. Also the database shows that the file was uploaded.

However, when I call:

echo elgg_list_entities(array(

'type' => 'object',
'subtype' => 'motion_data',

));

I am given a list of ElggObjects instead of ElggFiles. Because of this whenever I call,
$vars['entity']->getDownloadURL()

it will throw an error.

If I try to validate it with this:

if (!$entity instanceof ElggFile) {
return elgg_error_response("This is not a file.");
}

It always returns false. Is there something that I am missing? This doesn't seem to be expected behavior.

  • echo elgg_list_entities(array(

    'type' => 'object',
    'subtype' => 'motion_data',

    ));

    This will return a list of your files, not the object.

    Use elgg_get_entities. This is how I do it:

    First I get all my files:

    $featured = elgg_get_entities(array(
    	'type' => 'object',
    	'subtype' => 'file',
            'category' => 'featured',
            'owner_guid' => $blog->guid,
    	//'full_view' => false,
            'limit' => 1,
    	'no_results' => elgg_echo("file:none"),
    	'preload_owners' => true,
    	'preload_containers' => true,
    	'distinct' => false,
    ));

    Then I iterate through my array to get the download url:

    foreach ($featured as $t) {
                     $file_og = get_entity($t->guid);
    
                      $image_url = $file_og->getIcon('large');
                      $icon= elgg_get_inline_url($image_url);
                      $image_url = elgg_format_url($image_url);
                      $download_url_image = elgg_get_download_url($file_og);
                      ?>
                     
                     <meta property="og:image" content="<?php echo $icon;?>">
                     <?php
    //echo $file_og->getIconURL('large');
                     }

     

    Check the complete code here:

    https://github.com/rjcalifornia/softland_theme/blob/master/views/default/resources/posts/view.php

  • Thanks for your response. I tested out the functionality you suggested and Elgg is still returning a ElggObject instead of a ElggFile.... :/

     

    This throws an error:

    Argument 1 passed to elgg_get_download_url() must be an instance of ElggFile, instance of ElggObject given

     

    code:

    $content = elgg_get_entities(array(
    'type' => 'object',
    'subtype' => 'motion_data',
        'owner_guid' => elgg_get_page_owner_entity(),
        
    'no_results' => elgg_echo("file:none"),
    'preload_owners' => true,
    'preload_containers' => true,
    'distinct' => false,
    ));
     
     
    foreach ($content as $t)
    {
     
    $File = get_entity($t->guid);
     
    //throws an error
    $url = elgg_get_download_url($File);
    //also throws an error
    $url = elgg_get_download_url($t);
    }
  • Yeah, I also had that issue.

    Here's the thing, if you are dealing with files, as far as I know, you cannot use another subtype because elgg will not recognized as a file.

    If you see at my code, in the elgg_get_entities() I am filtering my files with an specific category. So in my action, I keep the file subtype, but I add a category:

    $file = new ElggFile();

    $file->title = $file->getFilename();

    $file->subtype = "file";

    $file->category = "featured";

    Then when I want to get the files I need, I just add the category:

    elgg_get_entities(array( 'type' => 'object', 'subtype' => 'file', 'category' => 'featured')

    You can view how I do my action here:

    https://github.com/rjcalifornia/elggpress/blob/master/actions/posts/save.php

  • @gwcoleman Extend ElggFile using your own Class.

    Example:

    namespace MyPlugin;
    
    use ElggFile;
    
    class MotionData extends ElggFile {
    
        const SUBTYPE = 'motion_data';
    
        public function initializeAttributes() {
          parent::initializeAttributes();
          $this->attributes['subtype'] = self::SUBTYPE;
        }
    
    
       public function getDisplayName() {
          $name = parent::getDisplayName();
          
          if (!$name) {
            $name = elgg_echo('untitled');
         }
    
         return $name;
       }
    
    }