Development of a custom field - documentation and/or help needed

  • Posts: 23
  • Thank you received: 1
2 months 1 week ago #341088

-- HikaShop version -- : 4.4.5
-- Joomla version -- : 4.1
-- PHP version -- : 7.4

Is there any documentation available for creation of custom custom fields? I am making progress by looking at the datepickerfield plugin, but various bits are unclear to me, such as

Method plgHikashopDatepickerfield::onFieldsLoad() of the datepickerfield plugin sets a list of options

$me->options = array('required', 'default', 'columnname', 'format', 'allow', 'datepicker_options')
I don't know what they are used for and whether I need all or any of them for my field.

For educational purposes I created a datepicker custom field to see how it's used and displayed. For some reason, that field not only displays the datepicker options, but also the options of my own custom field. I don't have a clue why that is.

Some pointers would be highly appreciated :)

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 1 week ago #341092

Hi,

This list of options allow you to ask HikaShop's custom field edit interface to display specific options.
If you check the HTML of the elements there, you can see an attribute "data-hk-display" :
i.imgur.com/oi2bebt.png
That attribute will contain one of the potential values for that options array. If your custom field type is selected for the current custom field, the system will automatically display all the options with that data-hk-display being equal to one of the options you added in that array and will hide the others.
For example, you can see a "placeholder" option in my screenshot. But this is only needed for text and textarea custom fields. That's why the advanced date picker plugin doesn't have it listed.
In the file administrator/components/com_hikashop/types/fields.php you can see the list of all the available types of custom fields along with their options (besides the advanced date picker field type since it comes from a plugin on the side).

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

  • Posts: 23
  • Thank you received: 1
2 months 1 week ago #341151

Thanx Nicolas.

A somewhat questionable, but creative way of hiding unused options :) Unfortunately It has (at least) one undesirable side effect. If no options are in effect for a group, like the Main Attributes for instance, an empty box, with a title but without any option fields, is displayed. Is there an easy way to prevent that?

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

  • Posts: 23
  • Thank you received: 1
2 months 1 week ago #341153

I have another question, concerning a field's preview in combination with its check() method. My custom field is slightly different the most, in that it returns a stringified JSON value. The value represents a matrix of values, along with some meta data. The number of rows and/or columns for the matrix are field options that need to be set before the field can be used. After they have been set, the preview displays an empty matrix, showing the configured number of rows and/or columns. So far, so good.

The field's check() method makes sure all matrix cells contain a valid, non empty value. It also does so when changing any of the options, i.e. changing the number of rows and/or columns. However, when saving the field, the check() method fails and does not allow the field to be saved. I tried to enter some dummy data in the preview matrix, but for some reason the resulting JSON value does not get passed into the check() method.

I have two questions:

  • Why is the field value of the preview field not passed into the check() method?
  • Is there a way to detect in the check() method if I'm editing the field's options or the actual item the field is meant to be used for?

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 1 week ago #341158

Hi,

1.

A somewhat questionable, but creative way of hiding unused options :) Unfortunately It has (at least) one undesirable side effect. If no options are in effect for a group, like the Main Attributes for instance, an empty box, with a title but without any option fields, is displayed. Is there an easy way to prevent that?

I'm sorry but I'm not following you here. Could you provide more information on your issue ?

2.
The preview area of the custom field edit interface is just for you to get a look at how the field will look like on the frontend.
There is no real saving done and thus no check called.
You must be mistaken with the "default" setting which also displays the your custom field in the custom field edit interface and is saved to be used as default value when the custom field is then used elsewhere.
In fact, if you don't have "default" in the options array, the default area's HTML will still be on the page, but hidden, and thus the check function will be called for the default input even if you don't see it, and thus that's normal that the value provided is empty as you didn't even had a chance to enter something in it in the interface.
So to check whether you're checking for a real element or the default value in the backend, you can look at the $field->field_namekey in your check function. That variable should contain "field_default" when checking the default value and otherwise it will be the field's column name.

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

  • Posts: 23
  • Thank you received: 1
2 months 1 week ago #341172

Thanks Nicolas. Your answer to my second question allows me to check on the default field. Because the field has no sensible default value, the check can no always return true.

For clarification of my first question, I added a screen shot of the custom field edit interface. As you can see the MAIN ATTRIBUTES area is completely empty. It would be user friendly to not display empty such empty areas.

Attachments:

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 1 week ago #341175

Hi,

Ah yes, now I understand what you mean.
You're right. There's actually a mechanism to handle the whole area. We just forgot to implement it for that area.
Change the line:

<div class="hkc-xl-4 hkc-lg-6 hikashop_tile_block hikashop_field_edit_attributes"><div>
to:
<div class="hkc-xl-4 hkc-lg-6 hikashop_tile_block hikashop_field_edit_attributes" data-hk-displays="required,regex,attribute,placeholder,inline,target_blank,default,add"><div>
in the file administrator/components/com_hikashop/views/field/tmpl/form.php and it will hide the area when empty.
We'll add that change on our end for the next version of HikaShop.

The following user(s) said Thank You: pjdevries

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

  • Posts: 23
  • Thank you received: 1
2 months 1 week ago #341208

Thanx again Nicolas!

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

  • Posts: 23
  • Thank you received: 1
2 months 3 days ago #341315

Yet another question.

I have a more or less working custom field type now. I can add it as a new custom field to table "product" and, when creating a new product, I can fill in the data for that field in the "CUSTOM FIELDS" section. The problem is I don't see that data in the product. Not in the parameter for the "onBeforeCalculateProductPriceForQuantity" event handler and also not when loading products using the product class like so: $productClass->getProducts([$productId]).

What am I missing here?

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 3 days ago #341316

Hi,

In onBeforeCalculateProductPriceForQuantity you get an element from the table hikashop_cart_product so it's normal that you don't have the data of custom product fields.
However, since you have the product_id, you can load it like this:

$productClass = hikashop_get('class.product');
$data = $productClass->get($product->product_id);
echo $data->xxx;
where xxx is the column name of your custom product field.
getProducts will also load that data but it's a bit more complex because it can load the data of several products, the variants, etc :
$productClass = hikashop_get('class.product');
$productClass->getProducts(array($product->product_id));
echo $productClass->products[$product->product_id]->xxx;

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

  • Posts: 23
  • Thank you received: 1
2 months 3 days ago #341324

Hi Nicolas,

Thanks again for the swift response. This time I feel really stupid, because I was testing with a faulty product; a product without my custom field. I feel embarrassed and apologize for wasting your time :blush:

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 3 days ago #341329

No worries.

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

  • Posts: 23
  • Thank you received: 1
2 months 1 day ago #341397

Hoping I haven't overlooked the obvious once again, I have a new question.

My custom field is not meant for the front end but specifically for the back end. When editing a product, the custom field shows up in the "CUSTOM FIELDS" area, like it is supposed to. However, if the product has Variants, it also shows up on Variant editing pages. Is there a way to prevent that? Not just hide it, with CSS or something, but not include it on the page at all? If it exists on the page, the validation will be triggered and most certainly fail, preventing the page from being saved.

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

  • Posts: 75924
  • Thank you received: 11857
  • MODERATOR
2 months 1 day ago #341399

Hi,

There is no option for that.

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

Time to create page: 0.078 seconds
Powered by Kunena Forum