Commit 322e78da authored by Miguel Guerreiro's avatar Miguel Guerreiro Committed by Miguel Guerreiro
Browse files

Issue #3165144 by gueguerreiro, developerweeks: Drupal 9 compatibility release

parent 3a3788e6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
name: Helpfulness
description: 'Provides a block for the user to leave feedback'
core: 8.x
core_version_requirement: ^8.8 || ^9
configure: helpfulness.admin_form
type: module
+18 −1
Original line number Diff line number Diff line
@@ -4,12 +4,29 @@ namespace Drupal\helpfulness\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a form for the administration values.
 */
class HelpfulnessAdminForm extends ConfigFormBase {

  /**
   * An email validator service instance.
   *
   * @var \Drupal\Component\Utility\EmailValidator
   */
  protected $emailValidator;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->emailValidator = $container->get('email.validator');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
@@ -127,7 +144,7 @@ class HelpfulnessAdminForm extends ConfigFormBase {
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $notification_email = trim($form_state->getValue('helpfulness_notification_email'));
    if (!empty($notification_email) && !valid_email_address($notification_email)) {
    if (!empty($notification_email) && !$this->emailValidator->isValid($notification_email)) {
      $form_state->setErrorByName('helpfulness_notification_email', $this->t('The email address you entered is not valid.'));
    }
  }
+11 −4
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ namespace Drupal\helpfulness\Form;
use Drupal\Core\Url;
use Drupal\Core\Form\FormBase;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -21,6 +20,13 @@ class HelpfulnessBlockForm extends FormBase {
   */
  protected $database;

  /**
   * A time service instance.
   *
   * @var \Drupal\Component\Datetime\Time
   */
  protected $time;

  /**
   * A current path service instance.
   *
@@ -48,6 +54,7 @@ class HelpfulnessBlockForm extends FormBase {
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->database = $container->get('database');
    $instance->time = $container->get('datetime.time');
    $instance->currentPath = $container->get('path.current');
    $instance->mailManager = $container->get('plugin.manager.mail');
    $instance->languageManager = $container->get('language_manager');
@@ -133,7 +140,7 @@ class HelpfulnessBlockForm extends FormBase {
    }

    // Validate the comments.
    $comments = Unicode::substr(trim(strip_tags($form_state->getValue('helpfulness_comments'))), 0, 1024);
    $comments = mb_substr(trim(strip_tags($form_state->getValue('helpfulness_comments'))), 0, 1024);
    if (empty($comments)) {
      $comments = $this->t('None');
    }
@@ -151,7 +158,7 @@ class HelpfulnessBlockForm extends FormBase {
      'helpfulness' => $form_state->getValue('helpfulness_rating'),
      'message' => $comments,
      'useragent' => $user_agent,
      'timestamp' => REQUEST_TIME,
      'timestamp' => $this->time->getRequestTime(),
    ];

    // Insert the data to the helpfulness table.
@@ -177,7 +184,7 @@ class HelpfulnessBlockForm extends FormBase {
      $this->mailManager->mail('helpfulness', 'new_feedback_notification', $notification_email, $language, $params, $site_mail);
    }

    drupal_set_message($this->t('Thank you for your feedback.'));
    $this->messenger()->addMessage($this->t('Thank you for your feedback.'));
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ class HelpfulnessConfirmDeleteFeedbackForm extends ConfirmFormBase {
      $query->execute();
    }

    drupal_set_message(t('The selected feedbacks have been deleted.'));
    $this->messenger()->addMessage(t('The selected feedbacks have been deleted.'));
    $form_state->setRedirect('helpfulness.report_form');
  }

+10 −2
Original line number Diff line number Diff line
@@ -18,6 +18,13 @@ class HelpfulnessReportDownloadForm extends FormBase {
   */
  protected $database;

  /**
   * A date formatter service instance.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * An entity type manager service instance.
   *
@@ -31,6 +38,7 @@ class HelpfulnessReportDownloadForm extends FormBase {
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->database = $container->get('database');
    $instance->dateFormatter = $container->get('date.formatter');
    $instance->entityTypeManager = $container->get('entity_type.manager');
    return $instance;
  }
@@ -159,7 +167,7 @@ class HelpfulnessReportDownloadForm extends FormBase {
      $csv_output .= '"' . $row->useragent . '",';

      // Time of submission.
      $csv_output .= '"' . format_date($row->timestamp, 'custom', "Y-m-d H:i:s") . '",';
      $csv_output .= '"' . $this->dateFormatter->format($row->timestamp, 'custom', "Y-m-d H:i:s") . '",';

      // That should be everything for this submission.
      $csv_output .= "\n";
@@ -167,7 +175,7 @@ class HelpfulnessReportDownloadForm extends FormBase {

    // Build the filename and start the download.
    $prefix = $this->t('feedbacks');
    $filename = $prefix . "_" . format_date(time(), 'custom', "Y-m-d_His");
    $filename = $prefix . "_" . $this->dateFormatter->format(time(), 'custom', "Y-m-d_His");
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: filename=" . $filename . ".csv");
    print $csv_output;
Loading