Issue #2815513 - Forbid deletion of attribute values that are in use #868
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since it's not really possible in Drupal to nicely prevent entity deletions at API level, a good first start is to prevent users from even accidentally deleting attribute values that are in used by product variation types.
So in this PR, I basically make the necessary query to determine if the attribute value is in use or not.
This is a POC, untested, and can potentially be improved. For example, here I only prevent the Delete button but we can also argue the value itself (or the entire row) to be disabled. I am not sure. Or we could use database queries instead of the entity API to determine in one single query the usage of ALL attribute values across all variation type. Might be more performant.
Please let me know if this approach is ok and i will also provide functional tests.