Adding and keeping related content (plugin)

I’m working on a plugin to update related content. Is there any way to prevent overwriting the existing related contents in a field, when adding more related contents? I could of course get all the existing related content keys before updating, and add them to the update, but as the plugin is going to be used with different contenttypes, the related content may exist at different xpaths. Any better solutions?

Also I’ve been looking around for a demokit (4.7), but it’s nowhere to be found? :sweat:

Hi Karine,

I don’t have an answer to your initial question, but as far as the 4.7 demokit is concerned, you can find the 4.7.12 demokit on the public Enonic repo:

1 Like

Hi Karine.

Have you tried setting the ContentDataInputUpdateStrategy to REPLACE_NEW? Im a bit rusty on this; here is the javadoc:

* There are two possible settings for the updateStrategy: <code>REPLACE_ALL</code> and <code>REPLACE_NEW</code>.
* <code>REPLACE_NEW</code> may be used, when only one or a few fields should to be changed.  The provided values
* will be changed, and all others will be left unchanged.
* With <code>REPLACE_ALL</code>, every field in the new content must have a value, and will be set to whatever
* value is provided.  This is the only way to remove the data for a field that has had a value that should be
* changed to a blank value or no value.  If <code>REPLACE_NEW</code> is used, a field will no value will not be
* changed.
* <p/>
* These strategies apply only to the data set in the <code>contentData</code> field.
* <code>publishFrom</code>, <code>publishTo</code>, <code>createNewVersion</code> and <code>setAsCurrentVersion</code>
* are metadata that are not affected by this update strategy.

Yes, I’m using REPLACE_NEW, but as I understand, this only tells the plugin to only update the contentdata fields that I’m adding/changing, but if the field already contains data, this will still be overwritten :unamused:

Hi Karine!

I am afraid what you are asking is not possible with our current API. You are right. REPLACE_NEW or REPLACE_ALL, have nothing to say for overwriting. When creating a new version, a template is created. This may be blank (REPLACE_ALL) or contain the values of the last version (REPLACE_NEW). Then the values in UpdateContentParams are written to the template. There is no addValue(...) or other functionality for modifying the existing values in the last version, so if there is a field with multiple values and you are just going to replace, add or remove one of them, I am afraid your own code have to do the heavy lifting of retrieving old values, modifying them and storing all the new values together. Sorry about that. :frowning: