Skip to main content

set Value of custom field by plugin

1 year 11 months ago #364475 by aminweb
hello,
I have a custom field (dropdown) on User cloumn.
I want set default value and readonly it by plugin.
my plugin codes:
Code:
<?php defined('_JEXEC') or die; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Factory; class PlgSystemHikaShopCustomize extends CMSPlugin { public function onHikashopBeforeDisplayView(&$view) { // Only modify the user registration form view if ($view->getName() !== 'user' || $view->getLayout() !== 'form') { return; } $user = Factory::getUser(); // Check if the logged-in user is in the 'Teacher' group (group ID 15) if (in_array(15, $user->getAuthorisedGroups())) { // Ensure Hikashop helper is loaded if (!class_exists('hikashopFieldClass')) { if (!include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/helpers/helper.php')) { return; } } $class = hikashop_get('class.field'); $field = $class->getField('teacher_name', 'user'); $lenew = new stdClass(); $lenew->value = "azur"; $lenew->disabled = 0; $field->field_value['bleue'] = $lenew; $class->addValue($field->field_id, 'bleue', 'azur', 0); } } }

But i not working as well. can you help me? thank you

Joomla Developer | am.ebrahimzadeh[at]gmail.com

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

1 year 11 months ago #364484 by nicolas
Hi,

First, the code:
Code:
// Ensure Hikashop helper is loaded if (!class_exists('hikashopFieldClass')) { if (!include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/helpers/helper.php')) { return; } }
should not do anything. The event onHikashopBeforeDisplayView is only called by the main helper file of HikaShop. So if your method is called, HikaShop is already loaded. Thus, you can remove it.

Second, in onHikashopBeforeDisplayView the field data is already loaded in the view and the view will use the data already loaded. So there is no need to load the field data, and you need instead to update the field data in the view.

So, you want to use something like this:
Code:
$lenew = new stdClass(); $lenew->value = "azur"; $lenew->disabled = 0; $view->extraFields['user']['teacher_name']->field_value['bleue'] = $lenew;
The following user(s) said Thank You: aminweb

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

1 year 11 months ago #364490 by aminweb
thank you,
I updated plugin codes by:
Code:
<?php defined('_JEXEC') or die; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Factory; class PlgSystemHikaShopCustomize extends CMSPlugin { public function onHikashopBeforeDisplayView(&$view) { // Only modify the user registration form view if ($view->getName() !== 'user' || $view->getLayout() !== 'form') { return; } $user = Factory::getUser(); // Check if the logged-in user is in the 'Teacher' group (group ID 15) if (in_array(15, $user->getAuthorisedGroups())) { $class = hikashop_get('class.field'); $field = $class->getField('teacher_name', 'user'); $lenew = new stdClass(); $lenew->value = $user->id; $lenew->disabled = 0; $view->extraFields['user']['teacher_name']->field_value[$user->id] = $lenew; } } }

But not affect on field atall (not set value and disable)

Joomla Developer | am.ebrahimzadeh[at]gmail.com

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

1 year 11 months ago #364493 by nicolas
Hi,

Well, you need to do some debugging.
Do a var_dump of $view->extraFields and check that the code goes past your user group check. That should be a good start.
The following user(s) said Thank You: aminweb

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

1 year 11 months ago #364497 by aminweb
I done by this codes:
Code:
<?php defined('_JEXEC') or die; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Factory; class PlgSystemHikaShopCustomize extends CMSPlugin { public function onHikashopBeforeDisplayView(&$view) { // Only modify the user registration form view if ($view->getName() !== 'user' || $view->getLayout() !== 'form') { return; } $user = Factory::getUser(); // Check if the logged-in user is in the 'Teacher' group (group ID 15) if (in_array(15, $user->getAuthorisedGroups())) { // Ensure Hikashop helper is loaded if (!class_exists('hikashopFieldClass')) { if (!include_once(JPATH_ADMINISTRATOR . '/components/com_hikashop/helpers/helper.php')) { return; } } $fieldClass = hikashop_get('class.field'); $field = $fieldClass->getField('teacher_name', 'user'); // Set the field default value to the current user's ID $field->field_default = $user->id; // Generate the field HTML with the correct number of arguments $field->field_html = $fieldClass->display($field, $user->id, 'data[user][teacher_name]'); // Modify the field HTML to disable the field if (!empty($field->field_html)) { // Disable the select field $field->field_html = str_replace('<select', '<select disabled="disabled"', $field->field_html); } // Update the field in the view $view->fields['user']['teacher_name'] = $field; // Set the value in the form data if (isset($view->user)) { $view->user->teacher_name = $user->id; } else { $view->user = new stdClass(); $view->user->teacher_name = $user->id; } $document = Factory::getDocument(); $script = " document.addEventListener('DOMContentLoaded', function() { var field = document.querySelector('select[name=\"data[user][teacher_name]\"]'); if (field) { field.disabled = true; } }); "; $document->addScriptDeclaration($script); } } }

Joomla Developer | am.ebrahimzadeh[at]gmail.com
The following user(s) said Thank You: nicolas

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

Time to create page: 0.052 seconds
Powered by Kunena Forum