Drupal - List User's Author
Drupal by default donot provide user's author detail in views or any where in data base. Once I went through this kind of requirnment to list users with detail, who created those users.
A simple way in D7 is to add text field in "user_register_form". That field will be hidden and will get current user's detail in it. We will do this using a module with hook_form_alter.
Say you added a text field in user registration form with name "field_authortxt"
Add the following code in .module file
Now we need to hide the field... we will be using after build handler for this..
This will be calling "_dothis_fix_disabled"
Then in the views, you can list the users with "field_authortxt" as users author.
A simple way in D7 is to add text field in "user_register_form". That field will be hidden and will get current user's detail in it. We will do this using a module with hook_form_alter.
Say you added a text field in user registration form with name "field_authortxt"
Add the following code in .module file
function custom_user_author_form_alter(&$form, &$form_state, $form_id) {
if(( $form_id == "user_register_form") || ( $form_id == "user_profile_form")){
$lang = $form['field_authortxt']['#language'];
global $user;
$form['field_authortxt'][$lang][0]['value']['#default_value'] = $user->name;
$form['#after_build'][] = '_tansnippet_after_build';
}
}
function _dothis_after_build($form, &$form_state) {
// Use this one if the field is placed on top of the form.
if(!empty($form['field_authortxt'])){
_dothis_fix_disabled($form['field_authortxt']);
}
return $form;
}
function _dothis_fix_disabled(&$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
// Recurse through all children elements.
_dothis_fix_disabled($elements[$key]);
}
}
//Adding readonly attributes to $elements
if (!isset($elements['#attributes'])) {
$elements['#attributes'] = array();
}
$elements['#attributes']['Hidden'] = 'Hidden';
}
Comments
Post a Comment