Customize Users display format
Added by Frank Enders over 13 years ago
Hi everybody,
is there a possibility to customize the Users display format? I would like to add the custom user field "Company". Currently only combinations of sur- and lastname seem to be possible.
Thanks!
Frank
Replies (7)
RE: Customize Users display format - Added by Felix Schäfer over 13 years ago
You could add it as a custom user format here: source:/app/models/user.rb#L29, not sure how that would affect the display of pages with lots of user names on it, as the custom fields are not stored in the same table. It might be better solved in a plugin.
RE: Customize Users display format - Added by Max Arnold almost 13 years ago
Can someone show an example of how this could be implemented? We want to change how user is displayed and show custom field along with username, otherwise visual separation between internal users and external ones is problematic and error prone.
Currently we add company name to user first name in square brackets, but this is really ugly.
RE: Customize Users display format - Added by Y Z almost 11 years ago
Would be interested in figuring out how to display a custom field value as a part of the display format. Is there any plugin or solution?
RE: Customize Users display format - Added by Guru M almost 11 years ago
Hi,
Following links might prove useful to determined individuals :
1) (ForumPost) Trying to create a custom field type : http://www.redmine.org/boards/3/topics/33353
2) (Article) Custom field view customization : http://projects.andriylesyuk.com/projects/extended-fields/wiki/Custom-fields-view-customization
Last but not least this book "Redmine Plugin Extension and Development" will be better : http://www.packtpub.com/redmine-plugin-extension-and-development/book
I've pre-ordered the book as it's at a 80% discounted price in ebook format.
Hopefully it should be better.
Hope this helps someone.
Regards,
Guru M.
RE: Customize Users display format - Added by Guru M almost 11 years ago
Hi,
Some other learning resources:
0) Source of redmine_contacts : https://github.com/cole/redmine_contacts
1) Extended Fields : http://projects.andriylesyuk.com/projects/extended-fields
2) Browse extended_fields plugin source online : http://subversion.andriylesyuk.com/extended-fields
3) API docs for CustomFieldFormat : http://www.rubydoc.info/github/asoltys/redmine/Redmine/CustomFieldFormat
Regards,
Guru M.
RE: Customize Users display format - Added by Uli Hecht over 9 years ago
I know this is an old thread, but it's the first thing you find when you google on this topic. I want to share my solution with you:
1. Create a plugin¶
ruby bin/rails generate redmine_plugin my_plugin
2. Create a file lib/user_patch.rb in your plugin¶
require_dependency "user"
module UserPatch
# The available user formats are stored in User::USER_FORMATS (app/models/user.rb)
# We cannot modify User::USER_FORMATS, as it's a constant
# Therefore we define an own hash which will be merged with USER_FORMATS
MY_PLUGIN_USER_FORMATS = {
:my_custom_name_format => {
:string => '#{firstname} #{lastname} #{!company.to_s.empty? ? "("+company.to_s+")":""}',
:order => %w(firstname lastname id),
:setting_order => 8
},
}
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
extend ClassMethods
class << self
# This line is responsible for letting redmine call name_formatter_with_company_format
# defined below instead of User::name_formatter
alias_method_chain :name_formatter, :company_format
end
end
end
module InstanceMethods
# This method will be included in the User-class to retrieve the field value of your custom field
def company
# look for our custom field in the database
cf = CustomField.where(["name = ?", "company"]).first
if cf
# get the value for the custom field (the method is provided by act_as_customizable behavior plugin)
custom_value_for(cf)
end
end
end
module ClassMethods
# this method is called when formatting user names
# We change its behavior to additionally look for user formats in our new hash "MY_PLUGIN_USER_FORMATS"
def name_formatter_with_company_format(formatter = nil)
User::USER_FORMATS[formatter || Setting.user_format] || User::MY_PLUGIN_USER_FORMATS[formatter || Setting.user_format] ||USER_FORMATS[:firstname_lastname]
end
end
end
# include our patch
User.send(:include, UserPatch)
3. Create a file lib/settings_controller_patch.rb in your plugin¶
We added the ability to apply our new name format above. Now we have to add it to the name-format settings list.
require_dependency "settings_controller"
require_dependency "user_patch"
module SettingsControllerPatch
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
alias_method_chain :edit, :my_plugin_user_format
end
end
module InstanceMethods
# Most of this code is copied from app/controllers/settings_controller.rb
def edit_with_my_plugin_user_format
@notifiables = Redmine::Notifiable.all
if request.post? && params[:settings] && params[:settings].is_a?(Hash)
settings = (params[:settings] || {}).dup.symbolize_keys
settings.each do |name, value|
Setting.set_from_params name, value
end
flash[:notice] = l(:notice_successful_update)
redirect_to settings_path(:tab => params[:tab])
else
@options = {}
# The following two code lines are different from app/controllers/settings_controller.rb
# We merge the original user formats list with our list of additional formats
user_formats = User::USER_FORMATS.merge(User::MY_PLUGIN_USER_FORMATS);
user_format = user_formats.collect{|key, value| [key, value[:setting_order]]}.sort{|a, b| a[1] <=> b[1]}
@options[:user_format] = user_format.collect{|f| [User.current.name(f[0]), f[0].to_s]}
@deliveries = ActionMailer::Base.perform_deliveries
@guessed_host_and_path = request.host_with_port.dup
@guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank?
@commit_update_keywords = Setting.commit_update_keywords.dup
@commit_update_keywords = [{}] unless @commit_update_keywords.is_a?(Array) && @commit_update_keywords.any?
Redmine::Themes.rescan
end
end
end
end
SettingsController.send(:include, SettingsControllerPatch)
4. Edit init.rb in your plugin directory¶
require "redmine"
require "user_patch"
require "settings_controller_patch"
Redmine::Plugin.register :my_plugin do
name 'My plugin'
author 'Example author'
description 'Provide a user name format which contains the value of custom field company'
version '0.0.1'
url 'http://www.redmine.org/boards/2/topics/24065'
author_url 'http://example.com'
end
5. Restart your webserver¶
You can now select the new user format in the settings.
RE: Customize Users display format - Added by tonyq wang about 4 years ago
I know it's a really old thread but I want to update this to date from Uli Hecht's solution.
(My redmine version: Redmine 4.1.1.stable)
1. The code has been changed in "app/controllers/settings_controller.rb" , so "SettingsControllerPatch" in the plugin need to update for this.
def edit_with_my_plugin_user_format @notifiables = Redmine::Notifiable.all if request.post? errors = Setting.set_all_from_params(params[:settings].to_unsafe_hash) if errors.blank? flash[:notice] = l(:notice_successful_update) redirect_to settings_path(:tab => params[:tab]) return else @setting_errors = errors # render the edit form with error messages end end @options = {} user_formats = User::USER_FORMATS.merge(User::MY_PLUGIN_USER_FORMATS); user_format = user_formats.collect{|key, value| [key, value[:setting_order]]}.sort{|a, b| a[1] <=> b[1]} @options[:user_format] = user_format.collect{|f| [User.current.name(f[0]), f[0].to_s]} @deliveries = ActionMailer::Base.perform_deliveries @guessed_host_and_path = request.host_with_port.dup @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank? @commit_update_keywords = Setting.commit_update_keywords.dup @commit_update_keywords = [{}] unless @commit_update_keywords.is_a?(Array) && @commit_update_keywords.any? Redmine::Themes.rescan end
2. if you want better support for rails5 (although the redmine core didn't support rails 5 so far, i have upgrade my one to support rail5. )
since rails5 already remove the "alias_method_chain", there's some code change. (it could also work in rails4 so it's recommanded to write this way directly whether you are in rails 5 environment or not.)
UserPatch
# replace this line with following two lines # alias_method_chain :name_formatter, :custom_format alias_method :name_formatter_without_company_format, :name_formatter alias_method :name_formatter, :name_formatter_with_company_format
SettingsControllerPatch
# replace this line with following two lines # alias_method_chain :edit, :my_plugin_user_format alias_method :edit_without_edit_with_my_plugin_user_format, :edit alias_method :edit, :edit_with_my_plugin_user_format