Project

General

Profile

Custom fields for plugin

Added by Kirill Bezrukov (RedmineUP) over 13 years ago

Is in redmine any API to use core custom fields in plugins?

For example I need to add custom fields for my plugin contacts for IM data or something else


Replies (8)

RE: Custom fields for plugin - Added by Marco Gutsche over 13 years ago

I've done this in an own plugin (hope my way is correct, but it works):

  • you add to your model (let's call it test): "acts_as_customizable"
  • you create a new model: "test_custom_field.rb"
    class TestCustomField < CustomField
      def type_name
        :label_test
      end
    end
    
  • you overwrite the function "custom_fields_tabs" in the "custom_field_helper" (sou have a new tab in administration -> custom fields) :
    require_dependency 'custom_fields_helper'
    
    module CustomFieldsHelperPatch
      def self.included(base) # :nodoc:
        base.send(:include, InstanceMethods)
    
        base.class_eval do
          alias_method_chain :custom_fields_tabs, :test_tab
        end
      end
    
      module InstanceMethods
        # Adds a rates tab to the user administration page
        def custom_fields_tabs_with_test_tab
          tabs = custom_fields_tabs_without_test_tab
          tabs << {:name => 'TestCustomField', :partial => 'custom_fields/index', :label => :label_test}
          return tabs
        end
      end
    end
    
    CustomFieldsHelper.send(:include, CustomFieldsHelperPatch)
    
  • in a view you can access the custom fields:
      <% @test.custom_values.each do |custom_value| %>
        <% if !custom_value.value.blank? %>
          <li><%= custom_value.custom_field.name%>: <%=h show_value(custom_value) %></li>
        <% end %>
      <% end %>
    

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) over 13 years ago

Thank you, look's good, I will try it

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) about 13 years ago

Marco Gutsche wrote:

I've done this in an own plugin (hope my way is correct, but it works):

  • you add to your model (let's call it test): "acts_as_customizable"
  • you create a new model: "test_custom_field.rb"
    [...]
  • you overwrite the function "custom_fields_tabs" in the "custom_field_helper" (sou have a new tab in administration -> custom fields) :
    [...]
  • in a view you can access the custom fields:
    [...]

I will try your method on contacts plugin and redmine 1.2.1 and it is not works
there no tabs in admin menu and no error

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) about 13 years ago

The tab shows, but I can't add field. First time page for new action opens and do nothing, but second time it is not opened

RE: Custom fields for plugin - Added by Marco Gutsche about 13 years ago

I'm using redmine 0.9.3, I don't know what has changed in redmine 1.x.x or why it doesn't work on redmine 1.x.x.

Maybe you can post your code?

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) almost 13 years ago

Marco Gutsche wrote:

I'm using redmine 0.9.3, I don't know what has changed in redmine 1.x.x or why it doesn't work on redmine 1.x.x.

Maybe you can post your code?

Here is my code in patch:

require_dependency 'custom_fields_helper'

module RedmineContacts
  module Patches    

    module CustomFieldsHelperPatch
      def self.included(base) # :nodoc:
        base.send(:include, InstanceMethods)

        base.class_eval do
          alias_method_chain :custom_fields_tabs, :test_tab
        end
      end

      module InstanceMethods
        # Adds a rates tab to the user administration page
        def custom_fields_tabs_with_test_tab
          tabs = custom_fields_tabs_without_test_tab
          tabs << {:name => 'ContactCustomField', :partial => 'custom_fields/index', :label => :label_contacts_custom_fields}
          return tabs
        end
      end

    end

  end
end

Dispatcher.to_prepare do  
  unless CustomFieldsHelper.included_modules.include?(RedmineContacts::Patches::CustomFieldsHelperPatch)
    CustomFieldsHelper.send(:include, RedmineContacts::Patches::CustomFieldsHelperPatch)
  end

end            

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) almost 13 years ago

Kirill Bezrukov wrote:

Marco Gutsche wrote:

I'm using redmine 0.9.3, I don't know what has changed in redmine 1.x.x or why it doesn't work on redmine 1.x.x.

Maybe you can post your code?

Here is my code in patch:
[...]

And sorry, tab is present, but field don't saved to db.

RE: Custom fields for plugin - Added by Kirill Bezrukov (RedmineUP) almost 13 years ago

it's ok

It have to add "unloadable" to model

    (1-8/8)