public function onAutoPopulateCustomFields(array &$data, array $fields, mixed $user) : void

Fires while binding the values of the custom fields.


Description

Trigger hook to allow external plugins to auto-populate the custom fields with other details that are not supported by default by the user instance.


Parameters

&$data

(array)  Where to inject the fetched data. The keys of the array are equals to the name of the custom fields.

$fields

(array)  An array of custom fields.

$user

(mixed)  The user instance.

Return Value

None.


Example

This example auto-fill the address custom field with the street name assigned to the currently logged-in user, assuming that $user->address has been supported by a third party plugin.

/**
 * Trigger hook to allow external plugins to auto-populate the custom fields
 * with other details that are not supported by default by the user instance.
 *
 * @param   mixed  &$data   Where to inject the fetched data.
 * @param   array  $fields  The custom fields list to display.
 * @param   mixed  $user    The details of the user.
 *
 * @return  void
 */
public function onAutoPopulateCustomFields(&$data, $fields, $user)
{
    if (empty($user->address))
    {
        // make sure the address is not empty
        return;
    }

    $addrFields = array();

    // search for an address field
    foreach ($fields as $cf)
    {
        if ($cf['rule'] == 'address')
        {
            $addrFields[] = $cf;
        }
    }

    if ($addrFields)
    {
        // fetch field key
        $key = $addrFields[0]['name'];

        // register address value with the address assigned to the user
        $data[$key] = $user->address;
    }
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-08 12:56
Helpful?