I have some fields type integer that are already populated, is possible to change the fields from integer to decimal?
4 Answers
Ok, I found a good solution to solve my problem.
Modules required
Steps
- Enable Helper module;
- Enable Devel module;
- Enable the block Execute PHP;
- Paste and execute this code in the block:
FieldChangeHelper::changeType('field_myintegerfield', 'number_decimal');
- Edit display format for node where the field is being used;
- Edit Views where the field is being used to fix the format settings (If necessary);
If you don't want use Devel and can use Drush:
- Enable Helper module;
create a new file in root Drupal folder, i.e. name it: intToField.php
<?php FieldChangeHelper::changeType('field_myintegerfield', 'number_decimal'); ?>
launch the drush command: drush src intToField.php
- Delete the file;
- Edit display format for node where the field is being used;
- Edit Views where the field is being used to fix the format settings (If necessary);
Changing the field type as you asked is not possible I think (fear).
However a possible alternative to achieve a similar result is like so (in the order specified):
- Create a new field, so that "from now on" any future node updates (or new nodes being created) will use that new field.
- Use the Rules module to create a rule like so:
- Rules event:
before saving content
. - Rules conditions:
entity has field
, with field = the old field.- AND NOT (
entity has field
, with field = the new field).
- Rules Action:
set Drupal message
, which contains some instructions that the old field must be blanked out, and the new field should contain the appropriate value(s.
- Rules event:
Obviously, this approach will take "some" time if this has to be done manually, 1 node at a time. But using Views (to build a list of similar nodes to be updated) and VBO (to mass update such lists) you might (should!) be able to speed up this process quite a bit. Refer to my answer to "How to bulk update select list fields?" for an example with way more details about that.
You would have to go to your database and export your field as a CSV file. Then open file with excel.
Note: If you're using php my admin for your export, do not use
CSV for MS Excel. Use the CSV option. AsCSV for MS Exceldisplays the values in a comma format rather than columns.
In Drupal delete the field and create a new decimal field. Go to a node and enter a value for your decimal field.
Go back to your database and find the decimal field, export as CSV. Then open on excel.
Use the values from your 1st excel to copy the node id's and the field value and paste them onto your 2nd excel and save.
Import 2nd excel to your decimal field and check-mark replace table data with file.
Clear/flush the cache to see changes.
-
2Looks like a good solution. I wish I had asked this a few months ago. Commented Mar 18, 2016 at 18:00
-
1Looks like a good but I have found another good solution. I added an answer.– kb8Commented Mar 18, 2016 at 19:33
you can do that by hook_form_alter.
-
2