I got Fatal error: Cannot use "static" when no class scope is active in /var/www/mndor/docroot/modules/contrib/govdelivery/govdelivery.module on line 41 just after enabling the module from the admin UI. Made another attempt using drush command but returned the same error.
Fatal error

Comments

alfattal created an issue. See original summary.

drupal.ninja03’s picture

Status: Active » Needs work

I am using a VM that runs Ubuntu so when I run the drush command to install the module I get the error:

vagrant@drupalvm:/var/www/drupalvm/drupal$ drush en govdelivery
PHP Fatal error:  Cannot use "static" when no class scope is active in /var/www/drupalvm/drupal/web/modules/contrib/govdelivery/govdelivery.module on line 41

The module install fails but next time I run the install command again then no error shows and the module gets installed.

The error points to the following in the govdelivery.module file

<?php
/**
 * Determine if a host to which the request is made is trusted
 */
function govdelivery_trusted_host() {
  $request = \Drupal::request();
  $host_patterns = $request->getTrustedHosts();
  if (PHP_SAPI !== 'cli' && !empty($host_patterns)) {
    if (static::setupTrustedHosts($request, $host_patterns) === FALSE) {
      return FALSE;
    }
  }
  return $request->getHttpHost();
}
?>

The line of contention is:
if (static::setupTrustedHosts($request, $host_patterns) === FALSE)

Not sure why it is throwing error on first installation and not on second time.

drupal.ninja03’s picture

Status: Needs work » Active
drupal.ninja03’s picture

Status: Active » Needs review
StatusFileSize
new636 bytes

I have created a patch to address the error on this line:

if (static::setupTrustedHosts($request, $host_patterns) === FALSE)

from the .module file. As we cannot use "static" if it is not inside a class.

This seems to solve the error.

alfattal’s picture

Thank you drupal.ninja03, the patch in #4 solved this issue!

alfattal’s picture

Now, I'm getting warning Undefined variable: account_fieldset in Drupal\govdelivery\Form\GovDeliverySettingsForm->buildForm() (line 195 of modules/contrib/govdelivery/src/Form/GovDeliverySettingsForm.php). on this page /admin/config/services/govdelivery/tms_settings.

Full warning message:

Notice: Undefined variable: account_fieldset in Drupal\govdelivery\Form\GovDeliverySettingsForm->buildForm() (line 195 of modules/contrib/govdelivery/src/Form/GovDeliverySettingsForm.php).
Drupal\govdelivery\Form\GovDeliverySettingsForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 520)
Drupal\Core\Form\FormBuilder->retrieveForm('govdelivery_settings', Object) (Line: 277)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 91)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 91)
Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
drupal.ninja03’s picture

StatusFileSize
new509 bytes
new1.12 KB

I was able to reproduce the error. It is because as the error says, there is an undefined variable: $account_fieldset being used

 $form['govdelivery_tms_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Mail server settings'),
      '#tree' => TRUE,
      'accounts' => $account_fieldset,
    );

But the part where $account_fieldset is being assigned that part of the code is commented out. Moreover, I don't know what that 'accounts' key stands for in a fieldset. Commenting out that line removes the error. Submitting the patch for the same.

alfattal’s picture

StatusFileSize
new2.02 MB

Hi @drupal.ninja03. Patch in #7 failed to apply (See the attached screenshot). Have you tested it?

Patch failed to apply

drupal.ninja03’s picture

Issue summary: View changes
StatusFileSize
new96.17 KB

Hi @alfattal,

Yes, I have tested it again just now and it applies. I used the git apply command:

git apply ~/Downloads/3137652-7.patch

Also tested by adding to composer.json file:

"patches": {
            "drupal/govdelivery": {
                "Fatal error after enabling module": "https://www.drupal.org/files/issues/2020-06-04/3137652-7.patch"
            }
        }

Patch

alfattal’s picture

Thank you @drupal.ninja03. My bad, I was try to apply both patches in #4 and #7!