I think this is impossible. However, is it possible to change a field into fields on Drupal 7? I have a content type of author. We used the "Title" field for user full name with their title. Is it possible to change it to "First Name", "Middle Name", "Last Name", and "Title" fields?

  • Could it be, that you actually want to achieve the opposite: combine three fields ("First Name", "Last Name" and "Title") to one and let them be displayed as "author"? – tillinberlin Aug 27 '14 at 17:07
  • @tillinberlin The one you mentioned can be done by Views. I really have a "Title" field, which contained author full name and their title. Someone created years ago and want me to changed now. :( – CocoSkin Aug 27 '14 at 17:12
  • @tillinberlin it actually has "Middle name" too. sigh. – CocoSkin Aug 27 '14 at 17:25
  • oh no! I see… I hope you'll find your way through that 'mess' :/ Good luck! – tillinberlin Aug 27 '14 at 20:36

It's definitely possible, but will require some coding. I can't work out the whole thing for you but I can point you in a direction.

You would have to create three new fields for 'first name', 'last name' and 'title' in the content type.

Then create a script that loads all of the nodes of that type with node_load_multiple then iterates through them, taking the old value of 'title' and splitting it using regex or the php 'explode' string function. Then using entity_metadata_wrapper, stick the individual values into the new fields and save the node.

Using drush, you can run the script like this: drush php-script scriptname.php

  • not easy as I'm not the web developer lol. FYI, it also contained "Middle Name" and not all author has that. – CocoSkin Aug 27 '14 at 17:29
  • Yeah, this is always the kind of trouble you get into when trying to move old unformatted data. How many nodes are you dealing with? – user3166999 Aug 27 '14 at 17:43
  • about 300 authors – CocoSkin Aug 27 '14 at 17:45
  • Is the order of items within the field always the same? ie Title First Middle Last. – user3166999 Aug 28 '14 at 18:11
  • Yes, they are always the same, but some of them does not have middle name. The format of that field is "First Middle Last, Title". – CocoSkin Aug 28 '14 at 18:24

You might use Field Collection. It provides fieldable entities that, themselves, can be added to other fieldable entities.

  • Does it mean it can split a data of field into 4? – CocoSkin Aug 27 '14 at 18:08
  • It depends on what you mean by that. Because it is fieldable, you can put any number of fields on it you want. If you have content in your other field already, you can migrate content from that field into your new field collection. – beth Aug 27 '14 at 18:12

There's no way to do this without some coding, as far as I can see. Work with the following assumptions:You know that there is always 3 or 4 elements, and in the case where there is 3 elements, the missing element is always the middle name. So load the node, get the value of the old field, explode it into an array, count the number of elements and if it's 3, then insert an empty element at array[1]. Then you have an array with 0 => First, 1 => Middle, 2 => Last, 3 => Title. Now use entity_metadata_wrapper to insert the individual array elements into the new fields. 0 into First Name, 1 into Middle Name (regardless of if it's empty or not), 2 into Last Name, and 3 into Title. Once you have the new fields populated, delete the old field from the content type, and use display rules or theming to decide when to display the Middle Name field, based on whether or not it has content. Something like that should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.