ReviewQuestionClarificationForm.php

PHOTO EMBED

Tue Oct 04 2022 20:05:24 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #mysql

<?php


namespace Drupal\pos_entity_review\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\pos_entity_cfp\Entity\CfpType;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ReviewQuestionClaficationForm
 *
 * @package Drupal\pos_entity_review\Form
 */
class ReviewQuestionClarificationForm extends ContentEntityForm {

  /**
   * Mail Manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * Constructs a new ReviewQuestionClarificationForm.
   *
   * @param EntityRepositoryInterface $entity_repository
   * @param EntityTypeBundleInfoInterface $entity_type_bundle_info
   * @param TimeInterface $time
   * @param MailManagerInterface $mailManager
   */
  public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, MailManagerInterface $mailManager) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->mailManager = $mailManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Instantiates this form class.
    return new static(
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
      $container->get('plugin.manager.mail'),
    );
  }

  /**
   * {@inheritDoc}
   */
  public function getFormDisplay(FormStateInterface $form_state) {
    $entity_form_display = EntityFormDisplay::create([
      'targetEntityType' => 'review',
      'third_party_settings' => [],
      'bundle' => $this->entity->bundle(),
      'content' => [
        'question' => [
          'type' => 'string_textarea',
          'region' => 'content',
          'weight' => 0,
          'settings' => [
            'rows' => 10,
            'placeholder' => '',
          ],
          'third_party_settings' => [],
        ]
      ],
    ]);
    return $entity_form_display;
  }

  /**
   * {@inheritDoc}
   */
  public function setFormDisplay(EntityFormDisplayInterface $form_display, FormStateInterface $form_state) {
    return parent::setFormDisplay($this->getFormDisplay($form_state), $form_state);
  }

  /**
   * {@inheritDoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    unset($actions['delete']);
    return $actions;
  }

  /**
   * Route title callback.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  public function titleCallback() {
    return $this->t('Clarify question');
  }

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    /** @var \Drupal\pos_entity_review\Entity\Review $review */
    $review = $this->entity;

    $cfp = $review->getCfpEntity();
    $cfp_type = CfpType::load($cfp->bundle());
    $reviewer = $review->getAssignee();

    // If admin is the one who is sending the mail, then it needs to be sent to reviewers email address.
    if ($this->currentUser()->hasPermission('admin view all call for proposal entities')) {
      $to = $reviewer->getEmail();
      $from = $cfp_type->get('mail_sender')->value ?? 'noreply@forsrpska.org';
    }
    else {
      $to = $cfp_type->get('notification_email')->value ?? 'info@forsrpska.org';
      $from = $reviewer->getEmail();
    }

    $this->mailManager->mail(
      'pos_entity_review',
      'reviewer_clarify_question',
      $to,
      \Drupal::languageManager()->getCurrentLanguage()->getId(),
      [
        'from' => $from,
        'subject' => t('Clarify question'),
        'message' => reset($form_state->getValue('question'))['value'],
      ],
      NULL,
      TRUE
    );

  }

}
content_copyCOPY