Skip to content
Snippets Groups Projects

Feature/istwcms 7017 kpaxman prevent deleting catalogs in use

1 file
+ 41
0
Compare changes
  • Side-by-side
  • Inline
+ 41
0
@@ -9,6 +9,7 @@ use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_preprocess_views_view().
@@ -125,6 +126,46 @@ function uw_ct_catalog_views_pre_render($view) {
* Implements hook_form_FORM_ID_alter().
*/
function uw_ct_catalog_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// Prevent deleting Catalogs that are in use.
if ($form_id == 'taxonomy_term_uw_vocab_catalogs_delete_form') {
// Get the current URL without query strings.
$url = parse_url(\Drupal::request()->getRequestUri(), PHP_URL_PATH);
// Get taxonomy term alias by removing '/delete' from url.
$alias = substr($url, 0, -7);
// Get taxonomy term path from alias.
$path = \Drupal::service('path_alias.manager')->getPathByAlias($alias);
// Get params from path.
$params = Url::fromUri("internal:" . $path)->getRouteParameters();
// Get tid from the URL.
$tid = $params['taxonomy_term'];
// Get node storage.
$nodeStorage = \Drupal::entityTypeManager()->getStorage('node');
// Get only one node by querying the database for entities
// matching conditions (catalog item nodes using tid).
$result = $nodeStorage->getQuery()
->condition('type', 'uw_ct_catalog_item')
->condition('field_uw_catalog_catalog', $tid)
->pager(1)
->execute();
// If the result isn't empty, we have a match.
if (!empty($result)) {
// Warn the user.
\Drupal::messenger()->addWarning(t('You cannot delete this catalog as it is in use. Delete or reassign any catalog items assigned to this catalog in order to be able to remove it.'));
// Redirect to the Catalogs overview page.
$url = Url::fromUri('internal:/admin/structure/taxonomy/manage/uw_vocab_catalogs/overview');
$response = new RedirectResponse($url->toString());
$response->send();
exit();
}
}
// If we are on a catalog node add/update/clone form,
// add states for the catalog tabs.
Loading