Skip to content

Commit

Permalink
Merge pull request #204 from nysenate/hotfix--cancel-subscriptions-af…
Browse files Browse the repository at this point in the history
…ter-dashboard-unfollow

Ensure subscriptions are canceled after unfollowing a bill from the dashboard
  • Loading branch information
kzalewski committed Jun 21, 2024
2 parents 4d8a4f4 + 7a24efe commit 7869658
Showing 1 changed file with 70 additions and 29 deletions.
99 changes: 70 additions & 29 deletions web/modules/custom/nys_dashboard/src/Form/ManageDashboardForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Drupal\flag\FlagService;
use Drupal\nys_bills\BillsHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -70,6 +71,13 @@ class ManageDashboardForm extends FormBase {
'committees' => '/senators-committees',
];

/**
* NYS Bills Helper service.
*
* @var \Drupal\nys_bills\BillsHelper
*/
protected BillsHelper $billsHelper;

/**
* Constructs a ManageDashboardForm object.
*
Expand All @@ -81,17 +89,21 @@ class ManageDashboardForm extends FormBase {
* Flag service.
* @param \Drupal\Core\Render\Renderer $renderer
* Renderer service.
* @param \Drupal\nys_bills\BillsHelper $billsHelper
* NYS Bills Helper service.
*/
public function __construct(
EntityTypeManager $entityTypeManager,
AccountProxy $currentUser,
FlagService $flagService,
Renderer $renderer,
BillsHelper $billsHelper,
) {
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->flagService = $flagService;
$this->renderer = $renderer;
$this->billsHelper = $billsHelper;
}

/**
Expand All @@ -103,6 +115,7 @@ public static function create(ContainerInterface $container): ManageDashboardFor
$container->get('current_user'),
$container->get('flag'),
$container->get('renderer'),
$container->get('nys_bill.bills_helper')
);
}

Expand All @@ -119,14 +132,14 @@ public function getFormId(): string {
public function buildForm(array $form, FormStateInterface $form_state): array {
// Confirmation form.
if (
$form_state->has('is_confimartion_step')
&& $form_state->get('is_confimartion_step') === TRUE
$form_state->has('is_confirmation_step')
&& $form_state->get('is_confirmation_step') === TRUE
) {
return $this->buildConfirmationForm($form, $form_state);
}

// Main form.
$form_state->set('is_confimartion_step', FALSE);
$form_state->set('is_confirmation_step', FALSE);
$form['#attached']['library'][] = 'core/jquery';
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#attached']['library'][] = 'core/jquery.form';
Expand Down Expand Up @@ -243,16 +256,20 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
$unfollow_content_list = [];
foreach ($content_to_unfollow as $unfollow_content) {
$flagged_entity_type_id = ($unfollow_content['flag_type'] !== 'follow_this_bill') ? 'taxonomy_term' : 'node';
$flagged_entity = $this->entityTypeManager
->getStorage($flagged_entity_type_id)
->load($unfollow_content['flagged_entity_id']);
$content_type = substr($unfollow_content['field_name'], 0, -1);
$unfollow_content_list[] = $content_type . ': ' . $flagged_entity->label();
try {
$flagged_entity = $this->entityTypeManager
->getStorage($flagged_entity_type_id)
->load($unfollow_content['flagged_entity_id']);
$content_type = substr($unfollow_content['field_name'], 0, -1);
$unfollow_content_list[] = $content_type . ': ' . $flagged_entity->label();
}
catch (\Throwable) {
}
}
$form_state->set('unfollow_content_list', $unfollow_content_list);

// Put form into confirmation step and trigger rebuild.
$form_state->set('is_confimartion_step', TRUE);
$form_state->set('is_confirmation_step', TRUE);
$form_state->setRebuild();
}

Expand Down Expand Up @@ -307,24 +324,31 @@ public function confirmFormModal(array &$form, FormStateInterface $form_state):
*
* @return array
* An array of entity ID keys and entity label values.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getUsersFlaggedEntitiesByFlagName(string $flag_machine_name): array {
$flag_ids = $this->entityTypeManager
->getStorage('flagging')
->getQuery()
->accessCheck(FALSE)
->condition('flag_id', $flag_machine_name)
->condition('uid', $this->currentUser->id())
->execute();
try {
$flag_ids = $this->entityTypeManager
->getStorage('flagging')
->getQuery()
->accessCheck(FALSE)
->condition('flag_id', $flag_machine_name)
->condition('uid', $this->currentUser->id())
->execute();
}
catch (\Throwable) {
$flag_ids = [];
}

$entity_ids_and_labels = [];
if (!empty($flag_ids)) {
$flags = $this->entityTypeManager
->getStorage('flagging')
->loadMultiple($flag_ids);
try {
$flags = $this->entityTypeManager
->getStorage('flagging')
->loadMultiple($flag_ids);
}
catch (\Throwable) {
$flags = [];
}
foreach ($flags as $flag) {
$entity_id = $flag->flagged_entity?->referencedEntities()[0]?->id();
$entity_label = $flag->flagged_entity?->referencedEntities()[0]?->label();
Expand Down Expand Up @@ -383,14 +407,25 @@ public function unfollowTopics(array &$form, FormStateInterface $form_state): vo
foreach ($content_to_unfollow as $unfollow_content) {
$flag = $this->flagService->getFlagById($unfollow_content['flag_type']);
$flagged_entity_type_id = ($unfollow_content['flag_type'] !== 'follow_this_bill') ? 'taxonomy_term' : 'node';
$flagged_entity = $this->entityTypeManager
->getStorage($flagged_entity_type_id)
->load($unfollow_content['flagged_entity_id']);

try {
// Load the flagged entity.
/** @var \Drupal\Core\Entity\ContentEntityBase $flagged_entity */
$flagged_entity = $this->entityTypeManager
->getStorage($flagged_entity_type_id)
->load($unfollow_content['flagged_entity_id']);

// Remove the entity's flag.
$this->flagService->unflag($flag, $flagged_entity, $this->currentUser);

// If this item is a bill, cancel the nys_subscriptions record.
if ($unfollow_content['flag_type'] === 'follow_this_bill') {
$this->billsHelper->findSubscription($flagged_entity, $this->currentUser)
?->cancel()
?->save();
}
}
catch (\Exception $error) {
catch (\Throwable) {
$this->messenger()->addError('There was an error unfollowing '
. $flagged_entity->label() . '. Please try again later.');
continue;
Expand All @@ -402,10 +437,16 @@ public function unfollowTopics(array &$form, FormStateInterface $form_state): vo
'#type' => 'ul',
'#items' => $form_state->get('unfollow_content_list'),
];
$unfollow_content_ul = $this->renderer->render($unfollow_content_render_array);
$message = 'Successfully unfollowed the followed content:' . $unfollow_content_ul;
try {
$unfollow_content_ul = $this->renderer->render($unfollow_content_render_array);
$message = 'Successfully unfollowed the followed content:' . $unfollow_content_ul;
}
catch (\Throwable) {
$message = 'An error occurred while unsubscribing. Please try again later.';
}
$rendered_message = Markup::create($message);
$this->messenger()->addStatus($this->t('@rendered_message', ['@rendered_message' => $rendered_message]));
$this->messenger()
->addStatus($this->t('@rendered_message', ['@rendered_message' => $rendered_message]));
}

}

0 comments on commit 7869658

Please sign in to comment.