Skip to content

Commit

Permalink
DDST-166: Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bibliophileaxe committed Jun 19, 2024
1 parent 2f33640 commit 8910d1e
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ function _dgi_actions_handle_constraints_suffix_validation_add_constraint(array
try {
$config = \Drupal::config('dgi_actions_handle_constraints.settings');
$constraint_settings = $config->get('constraint_settings');
// Constraint settings is an array of values for each field that should be modified.
foreach ($constraint_settings as $constraintSetting) {
if (!isset($fields[$constraintSetting['field_name']]) || $constraintSetting['entity_type'] !== $entity_type->id()) {
continue;
}
// Both identifier and suffix fields should be unique.
$fields[$constraintSetting['field_name']]->addConstraint('UniqueField');

// If the field is a suffix field, set it as required.
if ($constraintSetting['field_usage'] === 'suffix') {
$fields[$constraintSetting['field_name']]->setRequired(TRUE);
}
Expand Down Expand Up @@ -63,8 +67,12 @@ function dgi_actions_handle_constraints_entity_presave(EntityInterface $entity):
try {
$config = \Drupal::config('dgi_actions_handle_constraints.settings');
$constraint_settings = $config->get('constraint_settings');
// Constraint settings is an array of values for each field that should be modified.
foreach ($constraint_settings as $constraintSetting) {
if ($constraintSetting['field_usage'] !== 'suffix' || $entity->getEntityTypeId() !== $constraintSetting['entity_type'] || $entity->isNew() || !$entity->hasField($constraintSetting['field_name'])) {
// Only revert the suffix field if it is changed.
if ($constraintSetting['field_usage'] !== 'suffix'
|| $entity->getEntityTypeId() !== $constraintSetting['entity_type']
|| $entity->isNew() || !$entity->hasField($constraintSetting['field_name'])) {
continue;
}
$original_entity = Drupal::entityTypeManager()
Expand Down Expand Up @@ -93,7 +101,9 @@ function dgi_actions_handle_constraints_form_alter(array &$form, FormStateInterf
try {
$config = \Drupal::config('dgi_actions_handle_constraints.settings');
$constraint_settings = $config->get('constraint_settings');
// Constraint settings is an array of values for each field that should be modified.
foreach ($constraint_settings as $constraintSetting) {
// If the form id is not of the content form or the content edit form, return.
if (!($form_id === $constraintSetting['entity_type'] . '_' . $constraintSetting['entity_bundle'] . '_form'
|| $form_id === $constraintSetting['entity_type'] . '_' . $constraintSetting['entity_bundle'] . '_edit_form')) {
return;
Expand All @@ -102,16 +112,19 @@ function dgi_actions_handle_constraints_form_alter(array &$form, FormStateInterf
if (!$entity instanceof ContentEntityInterface || !isset($form[$constraintSetting['field_name']])) {
continue;
}
// For the suffix field, set the description and access.
if ($constraintSetting['field_usage'] === 'suffix') {
$form[$constraintSetting['field_name']]['widget'][0]['value']['#description'] = t('This field is used as the handle suffix.
The suffix field, once set, cannot be changed.');
$form[$constraintSetting['field_name']]['#access'] = TRUE;

// Disable the suffix field if it has a value.
if ($entity->{$constraintSetting['field_name']}->value && !$entity->isNew()) {
$form[$constraintSetting['field_name']]['#disabled'] = TRUE;
}
}

// If the field is an identifier field and the entity is not new, disable it.
if ($constraintSetting['field_usage'] === 'identifier' && !$entity->isNew()) {
$form[$constraintSetting['field_name']]['#disabled'] = TRUE;
}
Expand Down

0 comments on commit 8910d1e

Please sign in to comment.