Force value of field based on values of other fields on an entity
- drupal 8 validate multiple fields on an entity
Back in Drupal 7, whether this was a good idea or not even then, it seems we always did munging of values before saving in the validation step. In Drupal 8 the Symfony constraint and constraint validator approach and i think it’s designed to not change values on an entity, but only enforce constraints when validating.
The right way to manipulate values on save in Drupal 8 is with hook_entity_presave or it’s slightly more specific colleague, hook_ENTITY_TYPE_presave.
Credit to:
- https://drupal.stackexchange.com/questions/194456/how-to-use-presave-hook-to-save-a-field-value-as-node-title
- https://stackoverflow.com/questions/50482679/how-to-manipulate-value-before-node-is-saved-in-drupal-8
For straight validating, the constraint stuff looks pretty cool, if a bit more complex:
- https://jigarius.com/blog/drupal-entity-validation-api
- https://github.com/jigarius/drupal-custom-validation-example
- https://drupal.stackexchange.com/questions/215068/how-do-i-do-a-required-if-other-field-x-using-the-entity-validation-api/215280#215280
- https://www.philfrilling.com/blog/2018-09/custom-validation-multiple-dependent-entity-fields-drupal-8
- https://www.drupal.org/docs/8/api/entity-api/entity-validation-api/entity-validation-api-overview