News module custom field document format Topic is solved

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

News module custom field document format

Post by jakovbak »

Hello everyone!
I'd like to ask you if it's possible to add custom fields in News module to contain downloadable documents in doc, xls or pdf format?
So far I was able to find out News module does not distinguish different document formats, assuming everything is some kind of image but I'm really not convinced about it... I was also playing around with Custom Field Type switching between "linkedfile" and "file" but came out with nothing. ;D
If it's not possible, would you be so kind and recommend an easy to use news-or-blog-like module with support for different file formats for download?

Thank you in advance and have a nice rest of the day!

Jakovbak
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: News module custom field document format

Post by DIGI3 »

The field be for any kind of file you like, it's the template that assumes it's an image. There's a comment in the sample template showing where it loops through the custom fields, and if it finds a 'file' type, it creates an image tab. You could replace that with a link instead. If you want a (low-tech) way to tell what kind of file it is, perhaps to show an icon or something, you can use Smarty to check the file extension.
Not getting the answer you need? CMSMS support options
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

Re: News module custom field document format

Post by jakovbak »

Yes, I have noticed that comment but unfortunately I'm not good in Smarty and stuff like that... But I did try to replace "file" with "linkedfile" and came up with - nothing! :-[ And now, as you mentioned, an icon for corresponding document format and text "Download file" would be more than enough. But I don't know how to achieve that. Do you have some example you'd share with me so I could try to figure it out and change that part of the code in my template?
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: News module custom field document format

Post by velden »

Although I think you should stay away from the foreach loop looping over fields I will use it for this example:

Code: Select all

{if isset($entry->fields)}
  {foreach from=$entry->fields item='field'}
     <div>
        {if $field->type == 'file'}
           {$pathinfo = $field->value|pathinfo} 
           {$extension = $pathinfo.extension}
           <a href='{$entry->file_location}/{$field->value}' alt=''>Download file <img src="{root_url}/assets/images/icons/icon_{$extension}.gif" alt="" /></a>
        {else}
 ...
In the folder /assets/images/icons/ you should then have an icon_xxx.gif file for every supported file extension in your case. E.g. icon_pdf.gif and icon_doc.gif. Or change the code accordingly.
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1627
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: News module custom field document format

Post by DIGI3 »

You should just be able to replace the <img> tags with <a> tags. Using the News Summary Sample:

Code: Select all

  {foreach from=$entry->fields item='field'}
     <div class="NewsSummaryField">
        {if $field->type == 'file'}
          {if isset($field->value) && $field->value}
            Link to file: <a href="{$entry->file_location}/{$field->value}">{$field->value}</a>
          {/if}
        {elseif $field->type == 'linkedfile'}
          {if !empty($field->value)}
            Link to file: <a href="{file_url file=$field->value}">{$field->value}</a>
          {/if}
        {else}
          {$field->name}:&nbsp;{$field->value}
        {/if}
     </div>
  {/foreach}
You might be overthinking the file vs linkedfile field types. Those don't determine what kind of file it is, they are for choosing whether you present the editor with an upload field or a filepicker field. The above will create a link regardless of which one you choose, but you can remove the section for whichever field type you're not using if you like.

For getting the file type (via extension), this is a quick and dirty method, there are more robust options but a bit out of scope for my free help:

Code: Select all

{if preg_match('/\.jpg$/i',$field->value)}
  file is a jpg
{elseif preg_match('/\.pdf$/i',$field->value)}
  file is a pdf
{/if}
Not getting the answer you need? CMSMS support options
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

Re: News module custom field document format

Post by jakovbak »

Thank you Velden! I have tried your solution and that's exactly what I was looking for!
However, there is one tiny problem I wasn't able to solve (guess it's Smarty related):

- in frontend there is a question mark instead of icon and output generates "icon_.gif" path (like {$extension} is not working).

Icons have been placed in exact same way as in your example and naming is also correct so it must be something Smarty related but I also could be very wrong about it... ;D
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: News module custom field document format

Post by velden »

Can you post here the output of:

Code: Select all

...
  {if $field->type == 'file'}
...
  <pre>
    filename: {$field->value}
    print_r:
    {$field->value|pathinfo|print_r} 
  </pre>
...
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

Re: News module custom field document format

Post by jakovbak »

Should I add this to existing code at some place or replace the part of the code that controls extra fields?
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

Re: News module custom field document format

Post by jakovbak »

I was playing around with suggested code and I don't know if this helps but at some point I came up with output as in attached file... So far hovering on text link shows proper path to document and download itself works too. Same thing if one hovers on question mark. It only doesn't show the icon.
Attachments
Screenshot 2022-11-11 at 20.49.49.png
jakovbak
Forum Members
Forum Members
Posts: 225
Joined: Thu Dec 13, 2012 2:54 pm

Re: News module custom field document format [SOLVED]

Post by jakovbak »

Solved the icon problem! Stupid me... I placed "assets" folder in "uploads" instead of "root" so only the path to icon was wrong and I have overseen it!!!
Thank you all for help and advices and best regards!
Post Reply

Return to “Modules/Add-Ons”