Formatting custom file field

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #320947

-- url of the page with the problem -- : shower-trays-direct.under-development.in...slate-effect-800x800
-- HikaShop version -- : 4.3.0
-- Joomla version -- : 3.9.19

Hello,
I have a series of custom fields, one of which is a link to a file for installation dimensions. This is a file that can be freely downloaded no matter if a product has been purchased. I understand I could use the Files option on the Product but this is being used for another PDF and displayed in another location on the page. If you look at:

shower-trays-direct.under-development.in...slate-effect-800x800

Under the Specifications tab is where I am displaying the custom fields (from Barcode down) one of this is Dimensions. However I cannot find where I can modify the php loop in the show_block_custom_main.php file so that when it is displaying the file field I can modify the CSS to not simply display the file name but to show a button or similar to the one found on the Instructions tab (this is where the product file is being shown)

Where are the files that control the display of the custom field depending on what type of field it is?

I had tried to use this method:

<span id="hikashop_product_custom_name_<?php echo $oneExtraField->field_id;?>" class="hikashop_product_custom_name">
   <strong>Barcode:</strong><br /><?php echo $this->element->barcode; ?>
</span>
<span id="hikashop_product_custom_name_<?php echo $oneExtraField->field_id;?>" class="hikashop_product_custom_name">
   <strong>Colour / Finish:</strong><br /><?php echo $this->element->colour_finish; ?>
</span>
<span id="hikashop_product_custom_name_<?php echo $oneExtraField->field_id;?>" class="hikashop_product_custom_name">
   <strong>Shape:</strong><br /><?php echo $this->element->shape; ?>
</span>

<span id="hikashop_product_custom_name_<?php echo $oneExtraField->field_id;?>" class="hikashop_product_custom_name">
    <strong>Dimensions:</strong><br /><a href="/media/com_hikashop/upload/safe/<?php echo $this->element->dimension_file; ?>">link</a>
</span>

But the URL to the file is not correct and will not download the document.

Thanks for the help!

Please Log in or Create an account to join the conversation.

  • Posts: 83407
  • Thank you received: 13502
  • MODERATOR
4 years 8 months ago #320949

Hi,

Indeed directly download up uploaded files is not authorized (so that you don't give access to files from one user to someone else trying to circumvent the system.
So you need to let the system generate the link with the call to the show function.
What you can do is replace the line:

<?php echo $this->fieldsClass->show($oneExtraField,$value); ?>
by:
<?php echo str_replace('target="_blank" class="', 'target="_blank" class="btn btn-info', $this->fieldsClass->show($oneExtraField,$value)); ?>
That way the classes btn and btn-info will be injected in the HTML returned by the show function.

Please Log in or Create an account to join the conversation.

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #320983

Nicolas,
Thanks for the reply, but not sure I understand. Do I add and amend the code running in the loop of custom fields?

Currently the code I have above has replaced the php loop of the original template file.

Can I 'revert' to the original template file and simply replace this one line? What happens to the other fields that are not files? I do not want them to render as links?

Please Log in or Create an account to join the conversation.

  • Posts: 83407
  • Thank you received: 13502
  • MODERATOR
4 years 8 months ago #320984

Hi,

Yes, you can revert to the original. On the views listing, you have a bin icon on the column "remove customization" on the right for each view file which was customized for that. Then, just change that one line as instructed.
The code I gave will just add the classes btn and btn-icon to links that are displayed by the custom fields. So for other custom fields which aren't links, it won't change anything.
Now you can always adapt the code to change more or less fields based on your needs...

Please Log in or Create an account to join the conversation.

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #321280

Nicolas,
Thanks for the help and support most appreciated. I've developed and taken your code and the template a little further!

Probably not the greatest bit of PHP but I need a different value instead of the filename value so I've modifed the code to be:

defined('_JEXEC') or die('Restricted access');
?><?php
$this->fieldsClass->prefix = '';
$displayTitle = false;
ob_start();
foreach ($this->fields as $fieldName => $oneExtraField) {
	$value = '';
	if(empty($this->element->$fieldName) && !empty($this->element->main->$fieldName))
		$this->element->$fieldName = $this->element->main->$fieldName;
	if(isset($this->element->$fieldName))
		$value = trim($this->element->$fieldName);
	if(!empty($value) || $value === '0' || $oneExtraField->field_type == 'customtext') {
		$displayTitle = true;
	?>
// Set$value if a PDF filename to generic text
<?php
  $mxdimensionspdf = $this->fieldsClass->show($oneExtraField,$value);
        if (preg_match('/\pdf\b/', $mxdimensionspdf)) {
    $value = '&nbsp;Dimensions';
}     
?>
		<span id="hikashop_product_custom_name_<?php echo $oneExtraField->field_id;?>" class="hikashop_product_custom_name">
			<?php echo $this->fieldsClass->getFieldName($oneExtraField).':<br />'; ?>
            <?php echo str_replace('target="_blank" class="', 'target="_blank" class="btn btn-info', $this->fieldsClass->show($oneExtraField,$value)); ?>
            
		</span>
	<?php
	}
}
$specifFields = ob_get_clean();
?>
<?php echo $specifFields; ?>

This now displays a button in the same style as the one used for product PDF files!

Thanks

Last edit: 4 years 8 months ago by ltempest.
The following user(s) said Thank You: nicolas

Please Log in or Create an account to join the conversation.

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #321428

Well that code actually fails, it creates a different URL tot he document that then does not load?

How can I set a 'standard' text value for the button when it is link?

Please Log in or Create an account to join the conversation.

  • Posts: 83407
  • Thank you received: 13502
  • MODERATOR
4 years 8 months ago #321439

Hi,

I'm sorry but I don't understand the issue.
The str_replace code change we talked about here won't change the link that is generated.
How is the URL different ?
Could you provide an example ?
Note that this code might be the problem you have:

if (preg_match('/\pdf\b/', $mxdimensionspdf)) {
    $value = '&nbsp;Dimensions';
}
The $value variable is used to generate the URL. If you change it, it will create an invalid link.
Now I don't know why you're trying to change it but you probably want to do it differently. For example, if your goal is to change the text for the link, you could use a str_replace on the returned HTML of the show function call, like you already have for the classes.

Please Log in or Create an account to join the conversation.

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #321525

Nicolas,
Apologies for the confusion, yes I want to change the returned HTML of the file, currently it is showing the file name between the <a></a> tags which is not very nice, I want to have a standard piece of text that replaces the file name.

Forgive me what is str_replace? how can I implement this to change the filename?

Thanks again

Please Log in or Create an account to join the conversation.

  • Posts: 83407
  • Thank you received: 13502
  • MODERATOR
4 years 8 months ago #321528

Hi,

I gave you this line of code previously:

<?php echo str_replace('target="_blank" class="', 'target="_blank" class="btn btn-info', $this->fieldsClass->show($oneExtraField,$value)); ?>
which you changed accordingly. As you can see, the change consists on calling the "str_replace" function there to replace target="_blank" class=" by target="_blank" class="btn btn-info
If you want to have a generic text in that link, you can do it like that:
<?php echo preg_replace('#><\/a>#','>XXX</a>',str_replace('target="_blank" class="', 'target="_blank" class="btn btn-info', $this->fieldsClass->show($oneExtraField,$value))); ?>
where XXX is the text you want to have in the a tag.

Please Log in or Create an account to join the conversation.

  • Posts: 232
  • Thank you received: 4
  • Hikaserial Standard Hikashop Business
4 years 8 months ago #321574

Nicolas,
I'm forever indebted to you for your time and help. Thanks for sending the code, I'm just about understanding what it is doing, but my PHP knowledge is limited!

I have tried that code but it does not seem to work? Do I have to remove the hastags? What do the hastags mean?

Thanks

Please Log in or Create an account to join the conversation.

  • Posts: 83407
  • Thank you received: 13502
  • MODERATOR
4 years 8 months ago #321578

Hi,

Ah, I forgot a piece. Try like that instead:

<?php echo preg_replace('#>.*<\/a>#','>XXX</a>',str_replace('target="_blank" class="', 'target="_blank" class="btn btn-info', $this->fieldsClass->show($oneExtraField,$value))); ?>
The hashtags are normal. It's to separate the main expression from the modifiers ( www.php.net/manual/en/reference.pcre.pattern.modifiers.php ), but there is no modifier here.
It's a regex thing.

Please Log in or Create an account to join the conversation.

Time to create page: 0.079 seconds
Powered by Kunena Forum