Skip to content

Commit

Permalink
Issue #269: Only validate field_data_set_type when it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
lkmorlan committed Mar 5, 2024
1 parent 93d4f9b commit e681690
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions html/modules/custom/bc_dc/bc_dc.module
Original file line number Diff line number Diff line change
Expand Up @@ -588,16 +588,18 @@ function _bc_dc_form_node_data_set_edit_form_submit(array &$form, FormStateInter

// Check that the root ancestor of the field_data_set_type has not changed.
// Old field_data_set_type.
$node = $form_state->getFormObject()->getEntity();
$old_type = (int) $node->field_data_set_type?->first()?->getValue()['target_id'] ?? NULL;
// New field_data_set_type.
$new_type = (int) $form_state->getValue('field_data_set_type')[0]['target_id'] ?? NULL;
// If the type has changed, check if their root ancestors have changed.
if ($new_type !== $old_type) {
$old_type_root = _bc_dc_get_term_root_ancestor($old_type);
$new_type_root = _bc_dc_get_term_root_ancestor($new_type);
if (!$new_type_root || $new_type_root->id() !== $old_type_root->id()) {
$form_state->setErrorByName('field_data_set_type', t('The metadata root record type cannot be changed.'));
if ($form_state->hasValue('field_data_set_type')) {
$node = $form_state->getFormObject()->getEntity();
$old_type = (int) $node->field_data_set_type?->first()?->getValue()['target_id'] ?? NULL;
// New field_data_set_type.
$new_type = (int) $form_state->getValue('field_data_set_type')[0]['target_id'] ?? NULL;
// If the type has changed, check if their root ancestors have changed.
if ($new_type !== $old_type) {
$old_type_root = _bc_dc_get_term_root_ancestor($old_type);
$new_type_root = _bc_dc_get_term_root_ancestor($new_type);
if (!$new_type_root || $new_type_root->id() !== $old_type_root->id()) {
$form_state->setErrorByName('field_data_set_type', t('The metadata root record type cannot be changed.'));
}
}
}
}
Expand Down

0 comments on commit e681690

Please sign in to comment.