create override for admin classes/field.php

  • Posts: 17
  • Thank you received: 1
9 years 4 months ago #207620

-- HikaShop version -- : 2.5.0
-- Joomla version -- : 3.4.3
-- PHP version -- : 5.5
-- Browser(s) name and version -- : Chrome
-- Error-message(debug-mod must be tuned on) -- : no error message present

I want to add a css class to address labels in the checkout.
It seems that HTML output for these labels are created in administrator/components/com_hikashop/classes/field.php

function getFieldName($field,$requiredDisplay = false){
		$app = JFactory::getApplication();
		if($app->isAdmin())
			return $this->trans($field->field_realname);
		$required = '';
		if($requiredDisplay && !empty($field->field_required))
			$required = '<span class="hikashop_field_required_label">*</span>';
		return '<label for="'.$this->prefix.$field->field_namekey.$this->suffix.'">'.$this->trans($field->field_realname).$required.'</label>';
	}

should be changed into
function getFieldName($field,$requiredDisplay = false){
		$app = JFactory::getApplication();
		if($app->isAdmin())
			return $this->trans($field->field_realname);
		$required = '';
		if($requiredDisplay && !empty($field->field_required))
			$required = '<span class="hikashop_field_required_label">*</span>';
		return '<label for="'.$this->prefix.$field->field_namekey.$this->suffix.'" class="col-sm-2 control-label">'.$this->trans($field->field_realname).$required.'</label>';
	}

Next change is in the class hikashopText on line 1522 and further.
class hikashopText extends hikashopItem{
	var $type = 'text';
	var $class = 'inputbox';
	function display($field, $value, $map, $inside, $options = '', $test = false, $allFields = null, $allValues = null){
		$size = empty($field->field_options['size']) ? '' : 'size="'.intval($field->field_options['size']).'"';
		$size .= empty($field->field_options['maxlength']) ? '' : ' maxlength="'.intval($field->field_options['maxlength']).'"';
		$size .= empty($field->field_options['readonly']) ? '' : ' readonly="readonly"';
		$size .= empty($field->field_options['placeholder']) ? '' : ' placeholder="'.JText::_($field->field_options['placeholder']).'"';
		$js = '';
		if($inside && strlen($value) < 1){
			$value = addslashes($this->trans($field->field_realname));
			$this->excludeValue[$field->field_namekey] = $value;
			$js = 'onfocus="if(this.value == \''.$value.'\') this.value = \'\';" onblur="if(this.value==\'\') this.value=\''.$value.'\';"';
		}
		$buffInput = '<input class="'.$this->class.'" id="'.$this->prefix.@$field->field_namekey.$this->suffix.'" '.$size.' '.$js.' '.$options.' type="'.$this->type.'" name="'.$map.'" value="'.$value.'"';
		if(!empty($field->field_required) && !empty($field->registration_page))
			$buffInput.=' aria-required="true" required="required" />';
		else
			$buffInput .= ' />';
		return $buffInput;
	}
	function show(&$field,$value){
		if($field->field_table=='address') return $value;
		return $this->trans($value);
	}
}
should be changed into
class hikashopText extends hikashopItem{
	var $type = 'text';
	var $class = 'form-control';
	function display($field, $value, $map, $inside, $options = '', $test = false, $allFields = null, $allValues = null){
		$size = empty($field->field_options['size']) ? '' : 'size="'.intval($field->field_options['size']).'"';
		$size .= empty($field->field_options['maxlength']) ? '' : ' maxlength="'.intval($field->field_options['maxlength']).'"';
		$size .= empty($field->field_options['readonly']) ? '' : ' readonly="readonly"';
		$size .= empty($field->field_options['placeholder']) ? '' : ' placeholder="'.JText::_($field->field_options['placeholder']).'"';
		$js = '';
		if($inside && strlen($value) < 1){
			$value = addslashes($this->trans($field->field_realname));
			$this->excludeValue[$field->field_namekey] = $value;
			$js = 'onfocus="if(this.value == \''.$value.'\') this.value = \'\';" onblur="if(this.value==\'\') this.value=\''.$value.'\';"';
		}
		$buffInput = '<input class="'.$this->class.'" id="'.$this->prefix.@$field->field_namekey.$this->suffix.'" '.$size.' '.$js.' '.$options.' type="'.$this->type.'" name="'.$map.'" value="'.$value.'"';
		if(!empty($field->field_required) && !empty($field->registration_page))
			$buffInput.=' aria-required="true" required="required" />';
		else
			$buffInput .= ' />';
		return $buffInput;
	}
	function show(&$field,$value){
		if($field->field_table=='address') return $value;
		return $this->trans($value);
	}
}


How can this be done without a core hack?

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

  • Posts: 26159
  • Thank you received: 4028
  • MODERATOR
9 years 4 months ago #207624

Hi,

You can use the class/helper override system ; which is explained in that post.
www.hikashop.com/forum/product-category-...atermark.html#184987

As long as you just override the main class of the file (hikashopFieldClass) it will work.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 17
  • Thank you received: 1
9 years 1 month ago #216497

It seems that the post you referring to is trying to create an override on a helper file in administrator.
This is working for me when I would like to create an override on a helper file too.

But... I would like to create an override on a class file.
How can this be arranged. Not by following the instructions you gave me. I cannot see the result of my override

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
9 years 1 month ago #216506

Hi,

The same type of override will work for classes.
To override an HikaShop class , you can create an "image.override.php" in the folder :
YOUR_FRONT_END_TEMPLATE/html/com_hikashop/administrator/classes/

With the code
include_once $originalFile;
class hikashopFieldClassOverride extends hikashopFieldClass {
}
You can re-declare just the function you want thanks to PHP extends.

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

  • Posts: 17
  • Thank you received: 1
9 years 1 month ago #216626

Created /templates/[my-template]/html/com_hikashop/classes/field.override.php
Added the following code:

<?php
defined('_JEXEC') or die('Restricted access');

include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/classes/field.php');

class hikashopFieldClassOverride extends hikashopFieldClass {

}

class hikashopTextOverride extends hikashopText {
    var $type = 'text';
    var $class = 'inputbox form-control';

    function display($field, $value, $map, $inside, $options = '', $test = false, $allFields = null, $allValues = null) {

        $size = '';
        if(!empty($field->field_options['size']))
            $size .= ' size="'.intval($field->field_options['size']).'"';
        if(!empty($field->field_options['maxlength']))
            $size .= ' maxlength="'.intval($field->field_options['maxlength']).'"';
        if(!empty($field->field_options['readonly']))
            $size .= ' readonly="readonly"';
        if(!empty($field->field_options['placeholder']))
            $size .= ' placeholder="'.JText::_($field->field_options['placeholder']).'"';

        $js = '';
        if($inside && strlen($value) < 1) {
            $value = addslashes($this->trans($field->field_realname));
            $this->excludeValue[$field->field_namekey] = $value;
            $js = ' onfocus="if(this.value == \''.$value.'\') this.value = \'\';" onblur="if(this.value==\'\') this.value=\''.$value.'\';"';
        }

        if(!empty($field->field_required) && !empty($field->registration_page))
            $size .= ' aria-required="true" required="required"';

        return '<input class="'.$this->class.'" id="'.$this->prefix.@$field->field_namekey.$this->suffix.'"'.$size.$js.' '.$options.' type="'.$this->type.'" name="'.$map.'" value="'.$value.'" />';
    }

    function show(&$field, $value) {
        if(in_array($field->field_table,array('address','order','item')))
            return $value;
        return $this->trans($value);
    }
}

I have replaced
include_once $originalFile;
with
include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/classes/field.php');
Since that is working with my override in html/com_hikashop/helpers/cart.override.php

I have added
class hikashopTextOverride extends hikashopText
since the function I want to override is inside class hikashopText

but... it's not working.

(When I copy and paste your code and add the function inside class hikashopFieldClassOverride it's not working too)

Can you help me out?

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

  • Posts: 26159
  • Thank you received: 4028
  • MODERATOR
9 years 1 month ago #216634

Hi,

Yes, it can't work this way.
The class override allow you to override the main class, so for "classes/field.php" you can override "hikashopFieldClass".
But you can't override "hikashopTextOverride" except if you modify "hikashopFieldClassOverride to use your override class instead of the original one.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 127
  • Thank you received: 1
8 years 8 months ago #231924

Hello,

I am trying to override the field.php class and it is not working for me. This is the code that I have placed in field.php in template/html/com_hikashop/classes/:

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.6.1
 * @author	hikashop.com
 * @copyright	(C) 2010-2016 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/classes/field.php');

class hikashopFieldClassOverride extends hikashopFieldClass {
	
	function display(&$field, $value, $map, $inside = false, $options = '', $test = false, $allFields = null, $allValues = null, $requiredDisplay = true) {
		$field_type = $field->field_type;
		if(substr($field->field_type,0,4) == 'plg.') {
			$field_type = substr($field->field_type,4);
			JPluginHelper::importPlugin('hikashop', $field_type);
		}
		$classType = 'hikashop'.ucfirst($field_type);
		if(!class_exists($classType))
			return 'Plugin '.$field_type.' missing or deactivated';

		$class = new $classType($this);
		if(is_string($value))
			$value = htmlspecialchars($value, ENT_COMPAT,'UTF-8');

		$html = $class->display($field,$value,$map,$inside,$options,$test,$allFields,$allValues);

		if($requiredDisplay && !empty($field->field_required))
			$html .=' <span class="hikashop_field_required">*</span>';
		return $html;
	}

}

Any changes I make to the code fails to appear on the page. What do I need to do to get it working? Also, how can a I wrap each of the label + input pairs in a div to control layout. I do not want to use tables. Any help is greatly appreciated. Thank you.

Mike

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

  • Posts: 82906
  • Thank you received: 13378
  • MODERATOR
8 years 8 months ago #231946

Hi,

The file should be with the path:
templates/YOUR_TEMPLATE/html/com_hikashop/administrator/classes/field.override.php
As the path is not correct, the file is just not used by the system.

However, your question doesn't require the override of that class.
modifying the label+input display can be made with a simple view override.
But since you didn't provide information on how you configured your custom fields, it's hard to say what view file to edit exactly.
In fact, for custom fields of the table "user" or "address", the system will automatically display them on the registration page with either a table layout or a div layout based on whether the "use bootstrap design" setting is activated or not in the HikaShop configuration. For that, it will use either the custom_fields file or the custom_fields_boostrap file of the view "user" that you can edit via the menu Display>Views.

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

  • Posts: 127
  • Thank you received: 1
8 years 8 months ago #232053

Hello,

Thanks for the clarification and the information. I believe I have it figured out with using template overrides instead of the class override. I will post again if I need any more assistance.

Mike

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

Time to create page: 0.081 seconds
Powered by Kunena Forum