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.
Hi,
Here's the use case:
I have a content type with two fields: Start Year and End Year which I want them to be just integers and not going to use the Date field.
I want to create just one integer field and clone it so its settings can be cloned and use in the same content type.
I realized that Drupal doesn't come with this in the core. Is there a possibility for this module to achieve it?
Many thanks!!
Comment | File | Size | Author |
---|---|---|---|
#28 | field_tools-clone_fields_same_bundle-1350808-28.patch | 19.76 KB | star-szr |
#27 | field_tools-clone_fields_same_bundle-1350808-27.patch | 20.38 KB | colan |
Comments
Comment #1
joachim CreditAttribution: joachim commentedInteresting idea.
Though the current UI doesn't clone fields, but instances.
And AFAIK you can't put a field twice on one bundle -- for starters, the $field['bundles'] data array isn't constructed in a way that would support it.
So to allow this we'd need a new tab for cloning the entire field. You'd need a form element to specify a new field name too.
As it says on the project page -- patches welcome :D
Comment #2
kenyangzj CreditAttribution: kenyangzj commentedYes, I agreed. After some research, the "CLONE" function actually mean:
1. Create a new field with the same setting
2. Create an instance of this new field
3. Attach the new field instance to the bundle
I would love to contribute a patch when I have time to do it :p
Cheers!
Comment #3
Yuri CreditAttribution: Yuri commentedVery much interested in this too. I use nodes as being long multistep webforms, and reusing the same field is a must.
Comment #4
joachim CreditAttribution: joachim commented> I use nodes as being long multistep webforms,
Use webform. Nodes are not meant to be webforms...
> and reusing the same field is a must.
That's not doable. At best we can clone an entire field, but we can't make new instances of it.
Comment #5
wizonesolutions+1 for being able to clone the configuration for a field but give it a new name. In my case, I have a few page template content types that are similar but which may diverge in the future. We don't want to share the fields so we have flexibility if/when this happens.
This is the meaning of "clone fields" to me, but right now maybe it should be called "bulk-attach fields."
Comment #6
wizonesolutionsIncidentally - what's the effort in making this patch, if you know? Trying to see if I can get it sponsored. To clone without attaching is a lot of new code needed or is there already code in place I could leverage/reuse?
Comment #7
wizonesolutionsOh yeah, and for now a workaround is to export code with Bundle copy and then manually edit the code and change the field names. Make sure none of the new ones are longer than 32 characters to avoid import errors, and make sure the content type they'll be imported into is correct.
Comment #8
joachim CreditAttribution: joachim commentedYou can do this with drush BTW.
> Incidentally - what's the effort in making this patch, if you know? Trying to see if I can get it sponsored. To clone without attaching is a lot of new code needed or is there already code in place I could leverage/reuse?
You'd need:
- a new tab on the field
- a new admin form that asks for the new name
- a form submit handler that does the cloning. That's just a case of getting the field definition, changing the field name, saving that, then doing the same for the field instance definition.
Comment #9
bharata CreditAttribution: bharata commentedHas any progress been made with this Field Clone functionality? Seems like a very natural and very awesome addition to "add new field" and "add existing field". Also a serious time saver.
Wish I were a coder..... ;-(
Any help or pointers welcom ;-)
Comment #10
mstrelan CreditAttribution: mstrelan commentedIf you want to do this via code (eg, via /devel/php) here are some pointers.
Load the existing field definition and field instance with field_info_field() and field_info_instance().
Example:
Make modifications to the $field variable and call field_create_field(). Make sure you change the field name otherwise you won't be able to create the new field. Make modifications to the instance, probably just changing the field name to reflect the new field you created, and call field_create_instance().
Example:
Please note these examples are untested.
Comment #11
benjamindamron CreditAttribution: benjamindamron as a volunteer commentedI can confirm #10 works like a charm
Comment #12
erikphanson CreditAttribution: erikphanson commentedDid the ability to clone fields in the same content type make it into the module. Can't tell from the module description and this thread died out after some inconclusive patch talk? Would also be great to clone field groups in to the same content types.
Comment #13
joachim CreditAttribution: joachim commentedNope, this hasn't made it in yet
Comment #14
gbirch CreditAttribution: gbirch commentedFor anyone looking at this issue (how to copy fields within a bundle), this module provides a very nice workaround.
Go to the "Export fields" sub-task on the Tools tab. Pick the fields and field groups you want to copy. Export the code. Edit the exported code in a text editor to change the field and group names using global search/replace. Tweak any other settings you want to tweak, but changing the field and group names is essential.
Now go to the "Import fields" sub-task, paste in your edited code, and import.
Worked like a charm for me and saved at least an hour of tedious, error-prone re-configuration. Kudos to joachim!
Comment #15
jryanj21 CreditAttribution: jryanj21 commentedfor #14 exporting code also export id's, and the id's are the same as the previous fields. Did that create a conflict for you?
Comment #16
gbirch CreditAttribution: gbirch commentedjryanj21: No, it did not. I believe that when you look at the import code, you'll find that it quite deliberately ignores the ids.
Comment #17
jryanj21 CreditAttribution: jryanj21 commentedThanks for the quick response gbirch. Yesterday I did this and it worked amazingly. I have a content type where I need 6 fields to show up again (up to 3 times) based upon if the user wants to submit an additional item. This saved a lot of time.
I'm also using conditional fields that uses dependencies on when these fields show up. No conflicts!
Comment #18
colanMoving to D8. (There's no code here anyway.)
I'm currently exploring #2717319: Provide better default configuration when re-using an existing field and the corresponding sandbox, but getting a solution in this module makes sense as well.
Comment #19
colanHere's what I've got so far. What still needs to be done:
FieldCloner->cloneFieldConfig()
to set the new field name in the existing config, and thenThe new instance can then be created.
Any feedback is welcome.
Comment #20
colanThe more I got into this, the less my plan above made sense. However, I eventually figured it out. Most of the work was already being done when cloning to different entity types; this just needed to be expanded to include the situation where we're cloning to different field names.
As expected, the form now has an additional field for entering the destination field ID, which defaults to the current field ID. However, this must be changed if cloning to the same bundle (which is now allowed). I've added documentation on the form explaining this.
As a bonus, I was able to redirect the browser back to the Manage Fields page after the cloning happens, which solves one of the
@todo
s in the code.Comment #21
colanThe form now checks for field names that are too long (leading to an Exception being thrown).
Comment #22
selwynpolit CreditAttribution: selwynpolit commentedcolan Patch at #21 works great. Nice work!
Comment #23
selwynpolit CreditAttribution: selwynpolit commentedComment #24
colanComment #25
joachim CreditAttribution: joachim as a volunteer commentedSorry for taking time to get to this patch. It looks a bit overwhelming to me as a maintainer, but I appreciate that it probably can't be broken down into smaller pieces and that a lot of the diff is churn caused by code moving around and changing its indentation. I don't have the time to run it through manual testing, so I'm trusting the reviews its had during its development and the final RTBC. Any other problems can come out from people testing the dev release ;)
Thanks @colan for all your work on this, and everyone else also who's commented and reviewed.
I particularly appreciate the attention to detail with form errors and the redirect!
Comment #26
joachim CreditAttribution: joachim as a volunteer commentedUrgh, except it doesn't apply any more.
Comment #27
colanHere's a reroll.
Interdiff mostly failed:
git am --3way /tmp/field_tools-clone_fields_same_bundle-1350808-21.patch
did most of the work, but it couldn't handle the missing service dependency, as one was removed. That part had to be done manually.Works well in my tests. Would somebody else kindly try it?
Comment #28
star-szrRerolled again, only manual changes were
t()
to$this->t()
.Comment #29
joelpittetThis worked thanks for the patch, two things we had to do still to get it to work.
🐑🐏