Skip to content

Commit

Permalink
Merge pull request #1892 from GSA/1866-do-not-archive-criteria-if-used
Browse files Browse the repository at this point in the history
1866 do not archive criteria if still used
  • Loading branch information
scottqueen-bixal authored Oct 23, 2024
2 parents d9007c1 + a266eab commit 9f6f585
Showing 1 changed file with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,41 +214,86 @@ function _usagov_benefit_finder_content_check_agency_usage() {
/**
* Implements hook_form_FORM_ID_alter().
*/
function usagov_benefit_finder_content_form_node_bears_criteria_delete_form_alter(array &$form, FormStateInterface $form_state) {
_usagov_benefit_finder_content_check_criteria_usage($form);
function usagov_benefit_finder_content_form_node_bears_criteria_edit_form_alter(array &$form, FormStateInterface $form_state) {
$form['#validate'][] = '_usagov_benefit_finder_content_node_bears_criteria_edit_form_validate';
$form['#validate'][] = '_usagov_benefit_finder_content_criteria_archived';
}

/**
* It checks criteria usage in benefits and life event forms.
* If still used, it lists the benefits and life event forms and disables the criteria delete confirmation button.
* It checks criteria usage in benefits and life event forms when a criteria to be archived.
* If still used, it lists the benefits and life event forms and prevents the criteria to be archived.
*
* @param array $form
* Form array.
* @param FormStateInterface $form_state
* Form state object.
*/
function _usagov_benefit_finder_content_check_criteria_usage(array &$form) {
$description = '';
function _usagov_benefit_finder_content_criteria_archived(array &$form, FormStateInterface $form_state) {
$line = 0;
$moderation_state = $form_state->getValue('moderation_state');
$state_value = $moderation_state[0]['value'];
if ($state_value == 'archived') {
$result = _usagov_benefit_finder_content_check_criteria_usage();
if (!empty($result)) {
$form_state->setErrorByName(++$line, t("This criteria cannot be archived as it is still used in following contents:"));
foreach ($result as $row) {
$form_state->setErrorByName(++$line, "$row[type]: $row[title] ($row[nid])");
}
}
}
}

/**
* Implements hook_form_FORM_ID_alter().
*/
function usagov_benefit_finder_content_form_node_bears_criteria_delete_form_alter(array &$form, FormStateInterface $form_state) {
$return = _usagov_benefit_finder_content_check_criteria_usage();
if (!empty($return)) {
$description = '';
foreach ($return as $row) {
$description .= "<li>$row[type]: $row[title] ($row[nid])</li>";
}
$description = '<div class="entity-skip">'
. '<span>This criteria cannot be deleted as it is still used in following contents:</span>'
. "<ul>$description</ul>"
. '</div>';
$form['description']['#markup'] = $description;
$form['actions']['submit']['#access'] = FALSE;
}
}

/**
* It checks criteria usage in benefits and life event forms.
* If still used, it returns array of node ID and title of these benefits and life event forms.
*
* @return array
* An array containing node ID and title.
*/
function _usagov_benefit_finder_content_check_criteria_usage() {
$return = [];

$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->id();

$result = _usagov_benefit_finder_content_check_criteria_usage_in_life_event_form($nid);
foreach ($result as $row) {
$description .= "<li>Life event form: $row[title] ($row[nid])</li>";
$return[] = [
'type' => 'Life event form',
'nid' => $row['nid'],
'title' => $row['title'],
];
}

$result = _usagov_benefit_finder_content_check_criteria_usage_in_benefit($nid);
foreach ($result as $row) {
$description .= "<li>Benefit: $row[title] ($row[nid])</li>";
$return[] = [
'type' => 'Benefit',
'nid' => $row['nid'],
'title' => $row['title'],
];
}

if (!empty($description)) {
$description = '<div class="entity-skip">'
. '<span>This criteria cannot be deleted as it is still used in following content:</span>'
. "<ul>$description</ul>"
. '</div>';
$form['description']['#markup'] = t($description);
$form['actions']['submit']['#access'] = FALSE;
}
return $return;
}

/**
Expand Down Expand Up @@ -340,18 +385,6 @@ function _usagov_benefit_finder_content_check_criteria_usage_in_benefit(int $nid
return $return;
}

/**
* Implements hook_form_FORM_ID_alter().
*
* @param array $form
* Form array.
* @param FormStateInterface $form_state
* Form state object.
*/
function usagov_benefit_finder_content_form_node_bears_criteria_edit_form_alter(array &$form, FormStateInterface $form_state) {
$form['#validate'][] = '_usagov_benefit_finder_content_node_bears_criteria_edit_form_validate';
}

/**
* Validates criteria edit form hasChild field.
*
Expand Down

0 comments on commit 9f6f585

Please sign in to comment.