Removing DisplayName (or Settings) from a Part in Migrations

Topics: Writing modules
Jun 5, 2013 at 9:06 AM
Is it possible to remove settings or DisplayName from a part in Migrations? It's easy enough to add, but how does one undo a mistake?

In my case, I added .WithDisplayName to a field, and now need to remove it via a migration.
Jun 5, 2013 at 11:11 AM
nunez wrote:
Is it possible to remove settings or DisplayName from a part in Migrations? It's easy enough to add, but how does one undo a mistake?

In my case, I added .WithDisplayName to a field, and now need to remove it via a migration.
Ive made mistakes in .WithSettings before.
I just ended up removing the fields like:
ContentDefinitionManager.AlterPartDefinition("CookieControlPart", alter =>
                alter
                    .RemoveField("Intro")
                    .RemoveField("FullText"));
Then add them back again with the correct settings.
ContentDefinitionManager.AlterPartDefinition("CookieControlPart", alter =>
                alter
                    .WithField("IntroInput", cfg =>
                        cfg.OfType("TextField")
                           .WithDisplayName("Introductory Text")
                           .WithSetting("TextFieldSettings.Required", true.ToString())
                           .WithSetting("TextFieldSettings.Hint", "Some short, sharp copy to introduce the role of cookies on your site.")
                           .WithSetting("TextFieldSettings.Flavor", "HTML")
                    )
                    .WithField("FullText", cfg =>
                        cfg.OfType("TextField")
                           .WithDisplayName("Additional text")
                           .WithSetting("TextFieldSettings.Required", true.ToString())
                           .WithSetting("TextFieldSettings.Hint", "Describe in general terms what your cookies are used for.")
                           .WithSetting("TextFieldSettings.Flavor", "HTML"))
            );
Looking at core migration code you should be able to just use .WithField(name... again without the need to drop first as it removes it for you if the name key matches. I imagine that if there is data associated with the field already it will just match as the keys will be the same.