Error activating a module in Drupal 9 - Translations and invalid arguments of type resource
I get up in the morning, make myself an excellent coffee, install a module, and, BOOM! I can't believe it, we already started with an error that I don't even know what it's about...
TypeError: fgets(): Argument #1 ($stream) must be of type resource, bool given in fgets() (line 248 of /var/www/htm l/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php).
You will say, well, it doesn't seem like much either, but of course, the console shows it to you in this way, and surely more than one has had a heart attach when seeing it...
It is not the first time that I get this error when enabling a Drupal module, but what is the reason? and how can we solve it? The truth is that there can be several reasons, but it will always be oriented to either access to folders, file permissions, or established locations of both temporary and translation folders. This time the error was the following (I have not included everything).
srvazquez@Desktop:~/Projects/vazcell$ ddev drush en field_permissions > [notice] Checked es translation for field_permissions. > [error] The specified file 'temporary://file74h1sh' could not be copied because the destination directory 'translations://' is not properly configured. This may be caused by a problem with file or directory permissions. > [error] Unable to download translation file https://ftp.drupal.org/files/translations/all/field_permissions/field_permissions-8.x-1.1.es.po. > [warning] fopen(translations://field_permissions-8.x-1.1.es.po): Failed to open stream: "Drupal\locale\StreamWrapper\TranslationsStream::stream_open" call failed PoStreamReader.php:154 > [error] TypeError: fgets(): Argument #1 ($stream) must be of type resource, bool given in fgets() (line 248 of /var/www/html/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php) #0 /var/www/html/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php(248): fgets(false) > #1 /var/www/html/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php(186): Drupal\Component\Gettext\PoStreamReader->readLine() > #2 /var/www/html/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php(217): Drupal\Component\Gettext\PoStreamReader->readItem() > #3 /var/www/html/web/core/lib/Drupal/Component/Gettext/PoStreamReader.php(155): Drupal\Component\Gettext\PoStreamReader->readHeader() > #4 /var/www/html/web/core/modules/locale/src/Gettext.php(56): Drupal\Component\Gettext\PoStreamReader->open() > #5 /var/www/html/web/core/modules/locale/locale.bulk.inc(213): Drupal\locale\Gettext::fileToDatabase(Object(stdClass), Array) > #6 /var/www/html/web/core/modules/locale/locale.batch.inc(191): locale_translate_batch_import(Object(stdClass), Array, Object(DrushBatchContext)) > #7 /var/www/html/vendor/drush/drush/includes/batch.inc(261): locale_translation_batch_fetch_import('field_permissio...', 'es', Array, Object(DrushBatchContext)) > #8 /var/www/html/vendor/drush/drush/includes/batch.inc(206): _drush_batch_worker() > #9 /var/www/html/vendor/drush/drush/includes/batch.inc(100): _drush_batch_command('48') > #10 /var/www/html/vendor/drush/drush/src/Drupal/Commands/core/BatchCommands.php(22): drush_batch_command('48') > #11 [internal function]: Drush\Drupal\Commands\core\BatchCommands->process('48', Array) > #12 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(257): call_user_func_array(Array, Array)
In my case, this error is related to the translation system in which you can see if we continue reading the error until we reach the next point.
> [error] The specified file 'temporary://file74h1sh' could not be copied because the destination directory 'translations://' is not properly configured. This may be caused by a problem with file or directory permissions.
Well, the error is very simple to solve. We have to pay attention to several things:
- Translations folder: It is not the first time that the translations folder is not available due to an error and Drupal does not have the privileges to create it, sometimes simply creating it in our system and confirming that everything works correctly is enough to solve the problem.
- Permissions: Sometimes something as simple as having the permissions in order can lead to more than one disappointment. We access the route on our server /web/sites/default/files/translations and we confirm that both the owner/group and the access to the folder is allowed and correct.
- File System configuration: In my case, something as simple as accessing the file system and saving the configuration solved this error since it created the missing files so that everything was as it should be. To confirm it we will access the following route from the administration panel. SITE_URL/admin/config/media/file-system .
Once in the File system page, if we observe that the folder is not correct, we must change it to one that is accessible from our Drupal environment.
Once all these steps have been carried out, we should have our Drupal working correctly. To confirm this, we can "uninstall" the module and reinstall them, observing that, now, the translations have been downloaded correctly and everything has gone smoothly.