News module custom field document format Topic is solved
News module custom field document format
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.
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
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.
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
Re: News module custom field document format
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
Re: News module custom field document format
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?
Re: News module custom field document format
Although I think you should stay away from the foreach loop looping over fields I will use it for this example:
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.
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}
...
Re: News module custom field document format
You should just be able to replace the <img> tags with <a> tags. Using the News Summary Sample:
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
{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}: {$field->value}
{/if}
</div>
{/foreach}
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
Re: News module custom field document format
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...
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...
Re: News module custom field document format
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>
...
Re: News module custom field document format
Should I add this to existing code at some place or replace the part of the code that controls extra fields?
Re: News module custom field document format
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.
Re: News module custom field document format [SOLVED]
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!
Thank you all for help and advices and best regards!