set Value of custom field by plugin

  • Posts: 138
  • Thank you received: 4
1 month 1 day ago #364475

hello,
I have a custom field (dropdown) on User cloumn.
I want set default value and readonly it by plugin.
my plugin codes:

<?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.

  • Posts: 83007
  • Thank you received: 13398
  • MODERATOR
1 month 2 hours ago #364484

Hi,

First, the 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:
$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.

  • Posts: 138
  • Thank you received: 4
1 month 2 hours ago #364490

thank you,
I updated plugin codes by:

<?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.

  • Posts: 83007
  • Thank you received: 13398
  • MODERATOR
4 weeks 1 day ago #364493

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.

  • Posts: 138
  • Thank you received: 4
4 weeks 1 day ago #364497

I done by this codes:

<?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.066 seconds
Powered by Kunena Forum