14

Suppose I have a content type Person with fields: Name, Age, Email

This content type is controlled by a Feature called test so I can source control it and push it to the live site.

My workflow goes like this:

  • I add a new field on Person called Phone
  • locally I do: drush fu test -y
  • I edit the test.info to increase the version number
  • I commit the changes to my repository
  • I push the feature to the live site (copy the feature files)
  • on the live site I do: drush fr test -y
  • on the live site I do: drush cc all

Then the new field appears on the live site's Person.

If on the first step I remove a field of Person the field will not be deleted from the live site. How do I solve this? I don't want to have to manually delete it from the UI on the live site.
Is this workflow correct?

  • 1
    Use drush fu test -y --version-increment to automatically increment the version number – Mike Mar 10 '15 at 11:20
9

This is a double edged sword, it's like this to prevent data deletion which is always a good thing. There's nothing really to solve, this is expected behaviour.

It's the same workflow we use and have been doing for over a year and it works really well. I'd say it's correct but that's a very subjective answer.

  • And if I really wanted to delete the field? Should I look into update hooks? (I'm a D7 newbie). – cherouvim Apr 4 '12 at 9:54
  • 3
    Update hooks would be ideal, or manually remove it. – digital Apr 4 '12 at 9:56
  • 2
    OK, I could also do drush field-delete field – cherouvim Apr 6 '12 at 16:04
  • @cherouvim So you'd have to do that every time you recreate a feature? – AlxVallejo Jun 3 '14 at 20:49
  • A hook_update_N() in the feature's .install file would work. – Mike Mar 10 '15 at 11:32
10

The correct way to remove Features-created content is via hook_update_N (which should be implemented in the file your_module.install).


The main idea with Features is to manage configuration with code. Using hook_update_N is consistent with that, since the field deletion is in code (which can be managed with version control) — whereas the equivalent command drush field-delete field is not being called from managed code; it's a separate command-line step you need to perform (and to remember to perform).

  • Why? Won't drush field-delete field work well? – cherouvim Apr 9 '12 at 11:26
  • 5
    Sure, that would work, and may be a simpler solution. But the main idea with Features is to manage configuration with code. Using hook_update() is consistent with that, since the field deletion is in code (which can be managed with version control) — whereas the drush command is not being called from managed code; it's a separate command-line step you need to perform (and to remember to perform). – smokris Apr 9 '12 at 15:39
  • In which file of a feature will I use hook_update()? hook_update() takes a node object as parameter, so it is not possible to provide it a node object while enabling the feature. – subhojit777 Jun 12 '12 at 7:40
  • @subhojit777: Oops, I linked to the wrong hook documentation page. I just fixed the link; please see the docs for hook_update_N. – smokris Jun 12 '12 at 14:06
  • @smokris :) I mentioned that hook in my answer – subhojit777 Jun 15 '12 at 4:59
2

Create a new file called "your_feature_name.install"

Inside the file declare hook_update_N, there you can use field_delete_field() and specify the name of field that you want to delete as parameter.

Run update.php and the fields will be deleted.

0

If you are using field_phone in other areas, you might just want to remove it from your Person content type, but not delete it entirely from your site. Manually create a YOUR_FEATURE_NAME.install file in your feature's folder like so:

/**
 * Implements hook_update_N().
 *
 * Removes field_phone from Person content type.
 */
function YOUR_FEATURE_NAME_update_7001() {
  if ($instance = field_info_instance('node', 'field_phone', 'person')) {
    field_delete_instance($instance);
  }
  field_purge_batch(500);
  features_revert_module('YOUR_FEATURE_NAME');
}

If you were deleting this from a Person Paragraph instead of a Person Content Type, you could change the first conditional statement to something like this:

if ($instance = field_info_instance('paragraphs_item', 'field_phone', 'person')) {
  ...
}

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.