Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have developed a custom module that creates a new content type with several fields. I would like some of those fields to be automatically moved into field groups (tabs, fieldsets, accordion, etc). Is there a way to obtain this through code, in such a way that the configuration set by code can then be changed in the field management page as with groups created directly in the UI?
Comments
Comment #1
spydmobile CreditAttribution: spydmobile commentedI also am looking for a snippet of code to demonstrate this.
Comment #2
spydmobile CreditAttribution: spydmobile commentedI found a perfect example that really helped here:
http://drupal.org/node/1142344
Comment #3
Norberto Ostallo CreditAttribution: Norberto Ostallo commentedThanks, that is exactly what I was looking for. In my code I was missing the hook_ctools_plugin_api() implementation.
Comment #4
jonhattanCtools is not needed to create a fieldgroup.
What I do is creating a field group via web and then dump the raw object by running the following via devel's php block.
Then, this is the code I put in my hook_install():
Comment #6
haggins CreditAttribution: haggins commentedI'm trying the way jonhattan suggested and I see the field group in the database. However, the group is not visible on the entitys field managment page.
Am I missing something?
Comment #7
jonhattanI'm using the method explained extensively with no problem. Only difference I see with your code is that I'm using this for content types, not for profile2. Perhaps there's a difference.
Comment #8
bsandor CreditAttribution: bsandor commentedI have the same issue as @haggings in #6:
I can see the group in the database and not on my user entity.
I also got following error message during install:
Fatal error: __clone method called on non-object in .../sites/all/modules/field_group/field_group.module on line 1711
Comment #9
tadityar CreditAttribution: tadityar commenteddoes this applies for the Drupal 8 version?
Comment #10
Ivan Zugec CreditAttribution: Ivan Zugec commentedI had the same problem. I could see the group in the database but not on the manage field page. I had to run the following code to enable the group after
field_group_group_save()
.Comment #11
JayKandari#4 worked for me.
Comment #12
jimmyko CreditAttribution: jimmyko as a volunteer commentedThanks @Ivan Zugec, #10 save my time!
Comment #13
akozoriz CreditAttribution: akozoriz commentedNone of this solutions don't work for me (Drupal 7.54). My solution for hook_install in custom module.
For the first (#4) get field_group object with php code (You can use /devel/php link in this case - need enabled Devel module)
Then put code to hook_install
Comment #14
ram4nd CreditAttribution: ram4nd as a volunteer commentedHow about when you want to update an existing one? Than you #10 and #4.
Comment #15
hodba CreditAttribution: hodba commented#14 worked for me, others didn't. thank you @akozoriz
Comment #16
rajesh.vishwakarma CreditAttribution: rajesh.vishwakarma as a volunteer commentedOnly #13 worked for me.