ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Hack: Extending the file field tag in EE 2.2

June 24, 2011 5:39pm

Subscribe [3]
  • #1 / Jun 24, 2011 5:39pm

    trondus

    3 posts

    I’m creating a blog and want to use the new features in EE 2.2, but the file field tag didn’t give me the options of showing the title or caption of an image in a channel entry. So i decided to try to create a hack for it. I know changing the core files isn’t a smart way in the long run, but it does what I want for now.

    By adding this piece of code on line 271 between “$file_info[‘fullname’]...” and “return $file_info;” in the ft.file.php:

    $file_info['fullname'] = $file_info['filename'] . '.' . $file_info['extension'];
            
    $query = $this->EE->db->query("SELECT file_id, title, caption FROM exp_files WHERE file_name = '" . $file_info['fullname'] . "' LIMIT 1");
            
    if ($query->num_rows() > 0)
    {
        foreach($query->result_array() as $row)
        {
            $file_info['id']  = $row['file_id'];
            $file_info['title'] = $row['title'];
            $file_info['caption'] = $row['caption'];
        }
    }

    ..you’re able to use {id} {title} {caption} and {fullname} in a tagpair from an filefield.

    Example (used inside the channel entries tag):

    {my_img}
        {path}{fullname}
        ID: {id}
    
        Filename: {fullname}
    
        Title: {title} 
    
        Caption: {caption} 
    
    {/my_img}

    Would love to get any opinions or suggestions on this hack.

  • #2 / Jun 24, 2011 5:46pm

    trondus

    3 posts

    also, by adding:

    elseif(isset($params['short']))
    {
        return $file_info['path'] . '_' . $params['short'] . '/' . $file_info['filename'] . '.' . $file_info['extension']; 
    }

    on line 330 in the same file, you can quickly get the url of a manipulated image.

    Example:

    {my_img short="medium"}
  • #3 / Aug 06, 2011 8:35pm

    Bransin

    157 posts

    If any are interested, I just wrote a simple plugin that will do this without hacking the core files. I’ll provide a link as soon as I add the finishing touches.

  • #4 / Aug 07, 2011 1:27pm

    Bransin

    157 posts

    One thing I noticed.

    The file manager can contain identical file names but different upload location ids. The file data retrieved may not be accurate in this instance.

    The solution I found was: first find the upload_location_id of the file, then query from exp_files.

    Entry File Data v0.9

    Support or discussion on the plugin can be done here.

  • #5 / Sep 07, 2011 2:09pm

    neuralynx

    62 posts

    Or you could just do this right in the template:

    {my_img}
    
      {exp:query sql="SELECT file_id FROM exp_files WHERE file_name = '{filename}.{extension}' LIMIT 1"}
    
        {exp:file:entries file_id="{file_id}"}
    
          {file_url}
    
          ID: {file_id}
    
          Filename: {filename}.{extension}
    
          Title: {title} 
    
          Caption: {caption} 
    
    
          {medium_file_url}
    
        {/exp:file:entries}
    
      {/exp:query}
        
    {/my_img}

    Which gives you the image, id, filename, title, caption, and all other file tag variables, including the associated manipulated image variables.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases