I'm building a Drupal 8 module which will create several content types (with several fields) when the module is installed. The content types, field storages field instances, form displays & view displays are created with yml files in mymodule/config/install.
More details on the content types and fields:
- Let there are two node types, A and B.
- B has field called reference which is required, can have 1 value and the reference is to a node of type A. So basically B always has exactly one parent of type A.
What I want to do is to make the reference field read-only after the node is created.
In Drupal 7 I did this with Form API and hook_node_validate basically as follows:
- I first checked if the form submission was for a new node or an existing node
- If it was an existing node, I loaded the node (which is the node before the form submission at hand) and compared the value of the form submission to the value found on the actual node.
- If the values were not the same, I returned an error.
Now, in Drupal 8 we have the Validation API which is really good thing because it kicks in with also other mechanisms than form submits. I'm only wondering how to implement the read-only locking described above.
Should I create a Constraint Plugin which would do similar loading of the previous version like I explained above and add that constraint with hook_entity_bundle_field_info_alter()
or is there some other way?
Edit: Just to clarify, I'm interested to make the reference field non-editable on data level so that the value can't be changed even if updating the node programmatically. In other words, just making the field read-only on UI level on node edit form is not enough.