Custom Fields

Camaleon CMS Custom Fields is the perfect solution for any website which needs more flexible data.

Visually create your Fields
Select from multiple input types (text, textarea, wysiwyg, image, file, post object, category object, user object, select, checkbox, radio buttons, date picker, color picker, repeater, translatable, ...)

Assign your grouped fields to categories in a specific content type, , posts in a specific content type, post tags in a specific content type, users, site settings, menus, current theme, single post, or a custom model.

Field Attributes:
Repeater - Permit to enter multiple times the value for the custom field
Required - The value for the custom field is mandatory
Translatable - The value of the custom field can be entered in multiple languages configured for the site

Group Attributes:
name - Title for the Grouped Field
is_repeat - Permit to enter multiple values (duplicate) the current field group
description - Text description for the field group
where to display: Object class where will be added the custom field group

Visual details:
https://camaleon.website/custom-fields-4.html

  • How to add custom fields by code?
    # add a custom field group to current theme
    my_group = current_theme.add_field_group({name: "My Group", slug: "my_group_key"})
    # add a custom field group to specific post type
    my_group = my_post_type.add_field_group({name: "My Group", slug: "my_group_key"})
    # add custom field to specific group
    my_group.add_field({name: "Web Site", slug: "web_site"}, {field_key: "url"})

    # add custom fields into default group multiple support
    current_theme.add_field({"name"=>"Home Slider 3", "slug"=>"home_slider3"}, {field_key: "my_slider", translate: true, multiple: true, default_value: [{"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.38.pm.png","title":"Slider 1","descr":"This is a sample description"}, {"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.47.pm.png","title":"Slider 2","descr":"This is a sample description 2"}]})
    # set values multiple kind for previous field (field with multiple values support)
    current_theme.save_field_value("home_slider3", [{"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.38.pm.png","title":"Slider 1","descr":"This is a sample description"}, {"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.47.pm.png","title":"Slider 2","descr":"This is a sample description 2"}])
    # set single value (for no multiple support)
    current_theme.save_field_value("home_slider3", 'Single Value')

    # add a custom field no multiple with value for current theme (one line)
    current_theme.add_field({"name"=>"Home Slider 3", "slug"=>"home_slider3"}, {field_key: "my_slider", translate: true, multiple: false, default_value: 'default value'}) # for fields no multiple

    # add a custom field with multiple support and set values for current theme (one line)
    current_theme.add_field({"name"=>"Home Slider 3", "slug"=>"home_slider3"}, {field_key: "my_slider", translate: true, multiple: true, default_values: [{"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.38.pm.png","title":"Slider 1","descr":"This is a sample description"}, {"image":"https://camaleon.website/media/132/test_images/screen.shot.2016-05-12.at.6.47.47.pm.png","title":"Slider 2","descr":"This is a sample description 2"}]})
  • Samples how to add custom field group to specific models
    # add a custom field group to categories in a specific content type,
    my_group = my_post_type.add_field_group({name: "My Group", slug: "my_group_key"}, 'Category')
    # add a custom field group to posts in a specific content type
    my_group = my_post_type.add_field_group({name: "My Group", slug: "my_group_key"}, 'Post')
    # add a custom field group to post tags in a specific content type
    my_group = my_post_type.add_field_group({name: "My Group", slug: "my_group_key"}, 'PostTag')
    # add a custom field group to all post types
    my_group = my_post_type.add_field_group({name: "My Group", slug: "my_group_key"}, '')
    # add a custom field group to users
    my_group = current_site.custom_field_groups.create(object_class: 'User')
    # add a custom field group to site settings
    my_group = current_site.add_field_group({name: "My Group", slug: "my_group_key"}, '')
    # add a custom field group to menus
    my_group = my_menu.add_field_group({name: "My Group", slug: "my_group_key"}, '')
    # add a custom field group to current theme
    my_group = current_theme.add_field_group({name: "Home Slider", slug: "home_slider", is_repeat: true}) # is_repeat permit to duplicate the field group to enter multiple values
    # add a custom field group to single post
    my_group = current_site.custom_field_groups.create(object_class: 'MyClass')
  • How to filter data by custom fields?
    The following filters posts that includes specific custom field values
    Cama::PostType.first.posts.includes(:custom_field_values).merge(CamaleonCms::CustomFieldsRelationship.where(custom_field_slug: 'my_slug_key'))

  • How to order data by custom fields?

    The following permits to order posts by specific custom field value (Note: reorder(nil) to reset default ordering)
    Cama::PostType.first.posts.joins(:custom_field_values).reorder(nil).merge(CamaleonCms::CustomFieldsRelationship.reorder(nil).where(custom_field_slug: 'my_slug_key').order(value: :asc))
  • How to preload custom field values?
    The following preloads custom field values for posts
    my_posts = Cama::PostType.first.posts.eager_load(:custom_field_values).to_a
    my_posts.first.custom_field_values # no query (returns preloaded)
     
  • How to add my custom own fields?
    Simple field: https://gist.github.com/owen2345/b9cb0437b338ac712be59f6a7a7ebf70
    Complex field: https://gist.github.com/owen2345/fdec8eb47db6db91a76c05827bc64c5b
Created at: 19 Nov 08:20 | Updated at: 01 Aug 07:53