Skip to content

Commit

Permalink
Add dpl_opening_hours_taxonomy_term_predelete
Browse files Browse the repository at this point in the history
Implemented `hook_taxonomy_term_predelete()` to automatically delete associated opening hours when a category term is deleted. This ensures that all related opening hours are cleaned up upon term deletion.
  • Loading branch information
kasperbirch1 committed Sep 17, 2024
1 parent 56069e2 commit 713d0c3
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions web/modules/custom/dpl_opening_hours/dpl_opening_hours.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\dpl_opening_hours\Model\OpeningHoursRepository;
use Drupal\dpl_react_apps\Controller\DplReactAppsController;
use Drupal\drupal_typed\DrupalTyped;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;

/**
* Implements hook_preprocess_paragraph__TYPE().
Expand Down Expand Up @@ -50,12 +53,10 @@ void {
}

/**
* Implements hook_form_FORM_ID_alter() for the taxonomy term deletion form
* related to opening hours.
* Implements hook_form_FORM_ID_alter() for the taxonomy term deletion form related to opening hours.
*
* This hook modifies the deletion confirmation form for taxonomy terms within
* a specific vocabulary, adding custom messages and handling the deletion of
* associated opening hours.
* the "opening_hours_categories" vocabulary, adding custom messages.
*
* @param array $form
* The form structure array.
Expand All @@ -75,52 +76,51 @@ function dpl_opening_hours_form_taxonomy_term_opening_hours_categories_delete_fo
return;
}

$term_id = (int) $term->id();
$term_id = $term->id();
if (!is_int($term_id) && !is_string($term_id)) {
return;
}

$term_label = $term->label();

$form['#title'] = t('Are you sure you want to delete all "@term_label" opening hours?', [
'@term_label' => $term_label,
]);

$opening_hours_repository = \Drupal::service('dpl_opening_hours.repository');
$opening_hours_in_category = $opening_hours_repository->loadMultiple(categoryId: $term_id);
$opening_hours_repository = DrupalTyped::service(OpeningHoursRepository::class, 'dpl_opening_hours.repository');
$opening_hours_in_category = $opening_hours_repository->loadMultiple(categoryId: (int) $term_id);
$opening_hours_count = count($opening_hours_in_category);

$form['description']['#markup'] = t('@term_label is associated with @count opening hours. Deleting @term_label will also delete these opening hours. Are you sure you want to continue?', [
'@term_label' => $term_label,
'@count' => $opening_hours_count,
]);

// Add a custom submit handler to delete the opening hours associated with the term.
array_unshift($form['actions']['submit']['#submit'], 'dpl_opening_hours_term_delete_submit');
}

/**
* Custom submit handler for deleting opening hours associated with a taxonomy
* term.
* Implements hook_taxonomy_term_predelete().
*
* @param array $form
* The form structure array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* Deletes associated opening hours when a category term is deleted.
*
* @param \Drupal\taxonomy\TermInterface $term
* The term being deleted.
*/
function dpl_opening_hours_term_delete_submit(array &$form, FormStateInterface $form_state): void {
$form_object = $form_state->getFormObject();
if (!$form_object instanceof EntityFormInterface) {
function dpl_opening_hours_taxonomy_term_predelete(TermInterface $term): void {
if ($term->bundle() !== 'opening_hours_categories') {
return;
}

$term = $form_object->getEntity();
if (!$term instanceof Term) {
$term_id = $term->id();
if (!is_int($term_id) && !is_string($term_id)) {
return;
}

$term_id = (int) $term->id();

$opening_hours_repository = \Drupal::service('dpl_opening_hours.repository');
$opening_hours_repository = DrupalTyped::service(OpeningHoursRepository::class, 'dpl_opening_hours.repository');
$opening_hours_in_category = $opening_hours_repository->loadMultiple(categoryId: (int) $term_id);

$opening_hours_instances = $opening_hours_repository->loadMultiple(categoryId: $term_id);
foreach ($opening_hours_instances as $instance) {
$opening_hours_repository->delete((int) $instance->id);
foreach ($opening_hours_in_category as $instance) {
if ($instance->id && $instance->repetition->id) {
$opening_hours_repository->delete($instance->id, $instance->repetition->id);
}
}
}

0 comments on commit 713d0c3

Please sign in to comment.