Problem/Motivation

The module does not run on Drupal 9 yet, due to the use of deprecated functions.

Steps to reproduce

Stand up a drupal 9 site, then wget or curl the tar.gz release into the module/contrib folder. Enable the module, place the block, and attempt to use it.

Proposed resolution

- Replace the use of `Unicode::substr` with `mb_substr`
- Replace the use of `drupal_set_message` with `\Drupal::messenger()->addMessage`

Remaining tasks

The module now performs as desired on my site.

Comments

developerweeks created an issue. See original summary.

developerweeks’s picture

developerweeks’s picture

StatusFileSize
new7.71 KB

Now with Report pages patched too.

drupal_get_user_timezone() is deprecated, replaced with date_default_timezone_get(); but it makes no sense to call _get_ just to immediately _set_

Replace `format_date` with `$date_formatter->format` and a service definition above it.

Unclear why, but the 'value' index was deemed illegal on the D9 instance, so removed it from the line
->set('helpfulness_comment_required', $form_state->getValue('helpfulness_comment_required')['value'])

gueguerreiro’s picture

Status: Needs review » Needs work

Thank you for providing an initial patch that allows users to install this module on D9 :)

However, I think it still needs small changes before it can be accepted into the module's codebase:

On HelpfulnessBlockForm:

-    drupal_set_message(t('Thank you for your feedback.'));
+    \Drupal::messenger()->addMessage(t('Thank you for your feedback.'));

In this case, the \Drupal::messenger call is not necessary, since we are already extending FormBase, which uses MessengerTrait. You should just be able to do $this->messenger() and get the same result.

This also applies to HelpfulnessConfirmDeleteFeedbackForm, HelpfulnessReportForm and HelpfulnessReportOptionsForm.

On HelpfulnessReportForm:

-    date_default_timezone_set(drupal_get_user_timezone());
+    //date_default_timezone_set(drupal_get_user_timezone());
+    // drupal_get_user_timezone() is deprecated, replaced with date_default_timezone_get();
+    // makes no sense to call _get_ just to immediately _set_

Agree that that code is no longer necessary since https://www.drupal.org/node/3009387. We can remove the comments too, however.

Also related, this particular change means that there would have to be a different module release for 8.8+|9 versions of Drupal and the rest, as this seems to be a breaking change (Meaning, for inferior versions, that previous code was correct and in fact necessary).

Which would also mean that a more correct value for helpfulness.info.yml would be:

core_version_requirement: ^8.8 || ^9

Also on this file:

+      $date_formatter = \Drupal::service('date.formatter');

This service should be injected. We should avoid \Drupal:: calls in classes. The same applies for HelpfulnessReportDownloadForm.

Your last point:

Unclear why, but the 'value' index was deemed illegal on the D9 instance, so removed it from the line
->set('helpfulness_comment_required', $form_state->getValue('helpfulness_comment_required')['value'])

Is actually a bug that has been discussed here (And is also affecting D8): https://www.drupal.org/project/helpfulness/issues/3163343. A patch was also provided there. So yours is out of scope and should be removed from the patch you provided.

gueguerreiro’s picture

Running drupal-check on the module, I found a couple more (Truncated the output to show only the relevant deprecations that haven't been addressed yet):

 ------ ---------------------------------------------------------------------------------- 
  Line   src/Form/HelpfulnessAdminForm.php                                                 
 ------ ---------------------------------------------------------------------------------- 
  130    Call to deprecated function valid_email_address():                                
         in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.                         
         Use \Drupal::service('email.validator')->isValid().                               
 ------ ----------------------------------------------------------------------------------
------ -------------------------------------------------------------------------------------
  Line   src/Form/HelpfulnessBlockForm.php                                                                           
 ------ ------------------------------------------------------------------------------------                                                                           
  114    Call to deprecated constant REQUEST_TIME: Deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use  
         \Drupal::time()->getRequestTime();            
 ------ ------------------------------------------------------------------------------------
gueguerreiro’s picture

Title: replaced deprecated functions for Drupal9 compatibility » Drupal 9 compatibility release
gueguerreiro’s picture

Version: 8.x-1.0-rc2 » 2.0.0
Status: Needs work » Fixed

Not sure why the commit didn't pop up on the issue, but regardless.

Committed 322e78d and pushed to 2.x. Thanks!

Every website running on Drupal 8.8 or higher should update to the new 2.0.0 release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.