Skip to content

Commit

Permalink
VACMS-14623: Staff profile has no alias on publish (#16820)
Browse files Browse the repository at this point in the history
* VACMS-14623: Checks for path alias before disabling pathauto

* VACMS-14623: Adds a comment about the preg_match pattern

* VACMS-14623: Adds explicit alias toggle
  • Loading branch information
omahane authored Jan 18, 2024
1 parent 813e556 commit 19e338d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions docroot/modules/custom/va_gov_backend/va_gov_backend.module
Original file line number Diff line number Diff line change
Expand Up @@ -1933,12 +1933,24 @@ function _va_gov_backend_disable_autopath_alias(FieldableEntityInterface $entity
$toggled_on_this_save = $current_use_alias_pattern && !$original_use_alias_pattern;
$active_entity = \Drupal::entityTypeManager()->getStorage('node')->load($entity->id());
$published_previously = ($active_entity instanceof NodeInterface) ? $active_entity->isPublished() : FALSE;

if ($published_previously && !$toggled_on_this_save) {
// This was published and this was not an intentional toggle on.
$path = '/node/' . (int) $active_entity->nid->value;
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($path, $langcode);
// If the path_alias is the node path, alias is not set.
$path_alias_set = (bool) preg_match('/\/node\/\d+/', $path_alias) ? FALSE : TRUE;

if ($path_alias_set && $published_previously && !$toggled_on_this_save) {
// This has a path alias, was published,
// and was not an intentional toggle on.
// Disable the pathauto pattern.
$entity->path->pathauto = 0;
}
elseif (!$path_alias_set) {
// If this is not set pathauto already is 0,
// so we need to explicitly set it to 1
// to get an alias generated.
$entity->path->pathauto = 1;
}
}
}

Expand Down

0 comments on commit 19e338d

Please sign in to comment.