AnsweredAssumed Answered

Set cm:person association default value in form filter

Question asked by theonax on Aug 1, 2016
Hi,
I am trying to set default value for cm:person association in custom type create form based on logged in user.
I am using form filter to achieve that, but for some reason, the value is not displayed.


public class TestSetDefaultsFilter extends AbstractFilter<Object, NodeRef> {

   private static Log logger = LogFactory.getLog(TestSetDefaultsFilter.class);
   private AuthenticationService authenticationService;
   private PersonService personService;

   @Override
   public void afterGenerate(Object item, List<String> fields, List<String> forcedFields, Form form, Map<String, Object> context) {
      String userName = this.authenticationService.getCurrentUserName();
      logger.debug(">>> Current user: " + userName);
      NodeRef actualUser = this.personService.getPerson(userName);
      if (actualUser == null) {
          logger.error("User " + actualUser + " not found!");
          return;
      }
      form.getFormData().addFieldData("assoc_sse_testAssoc", actualUser, true);
   }

}


Setting properties this way works fine

form.getFormData().addFieldData("prop_sse_testProp", "Testing property initialized by filter", true);


I tried also to set default value for FieldDefinition, which displayed the default user correctly but after the form was submitted, the association was not set on node.

for (FieldDefinition def : form.getFieldDefinitions()) {
   if (def.getName().equals("sse:testAssoc")) {
      def.setDefaultValue(actualUserNodeRef);
      logger.debug(">>> Binding: " + def.getBinding());
      break;
   }
}


So what is the right way to set the association in form so the value is displayed in create form and unless changed, the association is set on node after submit?
Could anyone help?

Thanks

Outcomes