sailormoon
sailormoon

Reputation: 335

How to fix php error "Cannot pass parameter 1 by reference"?

I know that on stackoverflow there was a lot of similar problems with this error, but none solve my problem. I have a function in high_contrast.module file:

function high_contrast_install() {
  $background = \Drupal::config('high_contrast.settings')->get('colors_background');
  $text = \Drupal::config('high_contrast.settings')->get('colors_text');
  $hyperlinks = \Drupal::config('high_contrast.settings')->get('colors_hyperlinks');

  \Drupal::service('file_system')->prepareDirectory(HIGH_CONTRAST_CSS_FOLDER, FileSystemInterface::CREATE_DIRECTORY); // LINE 17!!!
  $css = _high_contrast_build_css();
  file_save_data($css, HIGH_CONTRAST_CSS_LOCATION, FileSystemInterface::EXISTS_REPLACE);
}

and when I want to install the module, I have en error:

Error: Cannot pass parameter 1 by reference in high_contrast_install() (line 17 of modules\high_contrast\high_contrast.install).

How can I fix it? :(

Line 17 is that one:

  \Drupal::service('file_system')->prepareDirectory(HIGH_CONTRAST_CSS_FOLDER, FileSystemInterface::CREATE_DIRECTORY);

Upvotes: 1

Views: 1893

Answers (1)

EricLavault
EricLavault

Reputation: 16095

As the error suggests, FileSystem::prepareDirectory takes its first argument by reference :

public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS) {
  # $directory being assigned new value
}

So PHP throws an error if you pass it a constant :

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script

You need to assign the constant value to a variable before the function call :

$dir = HIGH_CONTRAST_CSS_FOLDER;
\Drupal::service('file_system')->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);

Upvotes: 5

Related Questions