Remove genders from the website (this will remove gender from signup form, user edit, add form). This will surely effect the graphs in the administrator dashboard.
This component is created on user request.
Thanks for the hint. Here is working solution without a direct return from the callback function:
<?php
function remover_gender_init() {
ossn_add_hook('user', 'default:fields', 'remove_gender');
ossn_register_callback('action', 'load', 'replace_genders_male');
ossn_register_callback('action', 'load', 'ensure_language_saved');
}
function replace_genders_male($callback, $type, $params) {
if (in_array($params['action'], ['user/register', 'admin/add/user', 'profile/edit', 'admin/edit/user'])) {
ossn_set_input('gender', 'male');
}
}
function ensure_language_saved($callback, $type, $params) {
if (!in_array($params['action'], ['profile/edit', 'admin/edit/user'])) {
return;
}
$language = input('language');
if (!empty($language) && in_array($language, ossn_get_available_languages())) {
$user = ossn_loggedin_user();
if ($user && $user->data->language !== $language) {
$user->data->language = $language;
$user->save();
}
}
}
function remove_gender($hook, $type, $fields) {
// Check if the fields array has a 'required' or 'non_required' key
if(isset($fields['required']) && is_array($fields['required'])) {
foreach ($fields['required'] as $index => $field) {
if ($field['name'] == 'gender') {
unset($fields['required'][$index]);
}
}
$fields['required'] = array_values($fields['required']);
}
if(isset($fields['non_required']) && is_array($fields['non_required'])) {
foreach ($fields['non_required'] as $index => $field) {
if ($field['name'] == 'gender') {
unset($fields['non_required'][$index]);
}
}
$fields['non_required'] = array_values($fields['non_required']);
}
// Use ossn_unset_hook instead of returning
ossn_unset_hook('user', 'default:fields', 'remove_gender');
ossn_add_hook('user', 'default:fields', function($hook, $type, $fields) {
return $fields;
});
return $fields;
}
// Register the initialization function
ossn_register_callback('ossn', 'init', 'remover_gender_init', 1001);
?>
Callbacks must not return anything it will make others callbacks stop working
I have found a solution:
<?php
function remover_gender_init() {
ossn_add_hook('user', 'default:fields', 'remove_gender');
ossn_register_callback('action', 'load', 'replace_genders_male');
ossn_register_callback('action', 'load', 'ensure_language_saved');
}
function replace_genders_male($callback, $type, $params) {
if (in_array($params['action'], ['user/register', 'admin/add/user', 'profile/edit', 'admin/edit/user'])) {
ossn_set_input('gender', 'male');
}
}
function ensure_language_saved($callback, $type, $params) {
if (!in_array($params['action'], ['profile/edit', 'admin/edit/user'])) {
return;
}
$language = input('language');
if (!empty($language) && in_array($language, ossn_get_available_languages())) {
$user = ossn_loggedin_user();
if ($user && $user->data->language !== $language) {
$user->data->language = $language;
$user->save();
}
}
}
function remove_gender($hook, $type, $fields) {
$toremove = array('gender');
return ossn_remove_field_from_fields($toremove, $fields);
}
ossn_register_callback('ossn', 'init', 'remover_gender_init', 1001);
?>
Very useful component, unfortunately you can no longer change the language in the profile once you activate it.
For completeness, yes - it removes genders from the profile page too.
Is it possible to remove the gender in peoples profiles too?