ai_upgrade_assistant-0.2.0-alpha2/src/Form/ForumReplyDeleteForm.php

src/Form/ForumReplyDeleteForm.php
<?php

namespace Drupal\ai_upgrade_assistant\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\ai_upgrade_assistant\Service\CommunityLearningService;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * Form for deleting forum replies.
 */
class ForumReplyDeleteForm extends ConfirmFormBase {

  /**
   * The community learning service.
   *
   * @var \Drupal\ai_upgrade_assistant\Service\CommunityLearningService
   */
  protected $communityLearningService;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The reply ID to delete.
   *
   * @var int
   */
  protected $replyId;

  /**
   * The topic ID the reply belongs to.
   *
   * @var int
   */
  protected $topicId;

  /**
   * Constructs a new ForumReplyDeleteForm.
   *
   * @param \Drupal\ai_upgrade_assistant\Service\CommunityLearningService $community_learning_service
   *   The community learning service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(
    CommunityLearningService $community_learning_service,
    MessengerInterface $messenger
  ) {
    $this->communityLearningService = $community_learning_service;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('ai_upgrade_assistant.community_learning'),
      $container->get('messenger')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ai_upgrade_assistant_forum_reply_delete_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $reply_id = NULL) {
    $this->replyId = $reply_id;

    // Load the reply to confirm it exists and get the topic ID
    $reply = $this->communityLearningService->getForumReply($reply_id);
    if (!$reply) {
      throw new NotFoundHttpException();
    }

    $this->topicId = $reply->topic_id;

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete this reply?');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('ai_upgrade_assistant.forum.topic', ['topic_id' => $this->topicId]);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->communityLearningService->deleteForumReply($this->replyId);
    $this->messenger->addStatus($this->t('The reply has been deleted.'));
    $form_state->setRedirect('ai_upgrade_assistant.forum.topic', ['topic_id' => $this->topicId]);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc