Drupal Development tips for Common Problems
26 Aug
Jarkko Oksanen
Some problems when developing are simply annoying, and show up again and again. Many of these are relatively tricky to solve without knowing the best solution for the problem. I’ve combined my answers to a few of these problems that I've found I run into often.
1. You need to access Drupal site with no login details
Sometimes it just happens that there will be a website that you need to manage, and you don't have the administration password for it. You might think that there is a reset password functionality, but if the email of the administrator is invalid, getting the password is not so simple.
Use Drush to generate an one-time login link
This is a very quick and easy way to get logged into Drupal. It only requires that you have server access and Drush (http://www.drush.org/) is installed. The drush command you would use is:
drush uli
Drush will then output you a one-time user login link for this particular website. The Drush command has options that you can add to it that are listed below.
--browser : Optional value denotes which browser to use (defaults to operating system default). Set to 0 to suppress opening a browser.--uid : A uid to log in as.--redirect-port : A custom port for redirecting to (e.g. when running within a Vagrant environment)--name : A user name to log in as.--mail : A user mail address to log in as.
A more advanced command example would be:
drush uli --browser=firefox --name=admin node/2
This would log you in using firefox, for the admin user, and redirect you to node/2.
For more to read on Drush check out http://www.drush.org/en/master/
Update password using the database
In the case Drush isn’t available, your only option may be accessing the database. The idea is to simply change the encrypted password to another one through the database. If you’re using a GUI such as Sequel Pro, all you need to do is to navigate to users and change the encrypted password of the user admin.
Without a GUI, you can do it with a simple MySQL query.
UPDATE users SET name='admin', pass='$S$DfQ/y58nGpZvyRLYd3LSyJ.s82xSC3Z.2oxdCIL4EHKAYcQnDl9T' WHERE uid = 1;
This would set the password of admin, if its the uid 1, to “lol”. This is an encrypted password string. To create your own password hash you can navigate to Drupal docroot and run the following command.
php scripts/password-hash.sh 'yourpassword'
And it will generate you an encrypted password.
Use a module to hack your way through
This is a solution I would only use on local setups to change my password. There is no real use case for using this on production servers, as it logs everyone in as user 1 that try to access the site. However, if you don’t have any server access, and can only push code to the server, this might be your only shot.
- global $user;
- $user = user_load(1);
Then you can go change the account settings or promote other users to uid 1.
2. No images show up on my local development site
As we know, Drupal consists of three components, the database, the codebase and the files. The third one is considered the least important when developing, but to thoroughly test your work you do need the images to show up.
There is often an issue that the Drupal site that you are working on has hundreds if not thousands of large images which could end up as large as 10gb downloads, and downloading this for your local setup is simply not worth it. Fortunately there are a few solutions that you can use to conquer the problem.
Stage file proxy
https://www.drupal.org/project/stage_file_proxy
Stage file proxy regenerates the image links in a way that they will use the production server to get the images, and doesn't make you download them to your local setup. However default behavior of the module downloads the files when they’re missing. I encourage using the “Hotlink” mode of the module which does as previously explained.
Installing Stage File Proxy can be as simple as :
- drush en stage_file_proxy -y
- drush variable-set stage_file_proxy_origin "http://www.example.com"
But in most cases I find that saving the settings manually from the configuration is needed. It supports even locked in sites, with more documentation at https://www.drupal.org/project/stage_file_proxy.
Using JS to populate images with a dummy image
If you are working without an internet connection or there are issues with stage file proxy, then what you can do is to use JS to populate broken images with dummy images. This doesn't respect Drupal image styles, but it is a way to play with images. Add it to your JS after document ready.
- $('img').error(function(){
- $(this).attr('src', "/dummy-image.jpg");
- });
3. Getting a database without server access
Getting a database, files and code from your Drupal website can sometimes be tricky, especially when you have no server access. For example; you are working on a client site that has been forgotten somewhere in the cloud for a year, and no one has access to the server, there is still a solution you can try to get a copy of the site.
Use the Backup and Migrate module.
https://www.drupal.org/project/backup_migrate
Fortunately most cloud servers allow modules to be installed on the fly. All you need to do is to use Drupal UI to install this module and you’ll be able to get a copy of the website easily. To do this you need to enable Update Manager, and then install the file using /admin/modules/install user interface. If the server where your website is does not support this, then you need to gain access to the server.
The latest version of the Backup and Migrate module allows you to get a whole site with database, files and code. For simpler sites, using this module is great.
4. Moving modules around in a Drupal setup causes errors
Drupal gets angry when you move modules around in a setup. When you as a developer get your hands on a drupal website that other people have worked on, often the first instinct is to move the modules into a correct directory. Doing this can cause errors on the site and often results in a WSOD.
The error that results is often something like follows:
Fatal error: require_once(): Failed opening required '/profiles/profile/modules/contrib/entity/includes/entity.inc' in /profile/includes/bootstrap.inc on line 3161
This is due to Drupal looking at functions where they were before, and now don’t exist anymore. To fix this issue you need to fix the module paths.
Repair paths via drush registry rebuild
Drush to the rescue again! Compared to the manual solution explained after, this is definitely faster and a safer solution. Install Drush rebuild registry by running the following command (you need to have drush installed first):
drush dl registry_rebuild
After you’ve moved the modules to the directory you want just run the drush registry rebuild command.
drush rr
This will go through your module registry and fix all of the broken connections.
Then clear your drupal caches, and if needed re-run the drush rr command.
Do not blindly trust the power of this command on production servers. I would recommend thorough testing before moving modules around and doing these changes on live sites, as it can become a mess. Always remember to take a backup of your database!
Repair module paths manually
If for some annoying reason you cannot use the power of Drush, or it has failed you, you can still do things manually. Manually fixing is time consuming if you’re doing a lot of changes to your directory structure. Remember to backup your database before starting.
- Make sure you’re logged in your Drupal setup before moving the modules. This allows you to access the /admin/modules page. Accessing this page rebuilds the system table which might solve your problem. Move your modules to the directory you want, and access the /admin/modules page.
-
If the problem still continues you need to manually fix the following tables: system, registry and registry_file. The have filenames for each module, these need to be fixed as they are pointing to wrong directions. The following query is an example and was used when moving modules from the profile to a sites/all/modules/contrib setup.
- UPDATE system SET filename = REPLACE(filename, profiles/myprofile/modules', 'sites/all/modules/contrib');
- UPDATE registry SET filename = REPLACE(filename, 'profiles/myprofile/modules', 'sites/all/modules/contrib');
- UPDATE registry_file SET filename = REPLACE(filename, profiles/myprofile/modules', 'sites/all/modules/contrib');
-
After doing the changes, clear your Drupal caches and keep your fingers crossed for success. You might need to clear all the caching from the database as well.
5. Creating your re-usable local.settings.php
This is more of a personal touch. In my local development I’ve tried to create a local.settings.php that is relatively universal to all of my projects.
There are modules that always cause issues with local development such as securepages, or just need config, such as the before described stage_file_proxy. The local.settings.php is a file that is added to your docroot to provide these settings and make it faster for you to develop.
- # Local site configuration settings.
- if (is_readable('/path/to/site/sites/default/local.settings.php')) {
- include_once('/path/to/site/sites/default/local.settings.php');
- }
And on your local when you’re setting up your site, you just add your optimal local settings php.
This is my current version of the local.settings.php. Check out my latest one at GitHub and feel free to contribute to it, or add your comments below. The idea would be to include as much as I can in a file, as extra config doesn't really matter!
This will speed up your development by making sure that the settings you want are there, and most importantly done without clicking around the site!
- global $conf;
- // Turn off Secure Pages. Secure Pages Module.
- $conf['securepages_enable'] = FALSE;
- $conf['https'] = FALSE;
- // Stage File Proxy Configuration
- $conf['stage_file_proxy_origin'] = 'http://mysite.com';
- // Stage file optional with securepages
- // $conf['stage_file_proxy_origin'] = 'http://username:password@mysite.com';
- $conf["stage_file_proxy_use_imagecache_root"] = FALSE;
- $conf['stage_file_proxy_hotlink'] = TRUE;
- // Turn off Caching.
- $conf['cache'] = 0;
- // Block caching - disabled.
- $conf['block_cache'] = 0;
- // Expiration of cached pages - none.
- $conf['page_cache_maximum_age'] = 0;
- // Aggregate and compress CSS files in Drupal - off.
- $conf['preprocess_css'] = 0;
- // Aggregate JavaScript files in Drupal - off.
- $conf['preprocess_js'] = 0;
- // Minimum cache lifetime - always none.
- $conf['cache_lifetime'] = 0;
- // Cached page compression - always off.
- $conf['page_compression'] = 0;
- // Turn off other caching.
- $conf['css_gzip'] = FALSE;
- $conf['javascript_aggregator_gzip'] = FALSE;
- // Turn on all error reporting for local development.
- error_reporting(-1);
- $conf['error_level'] = 2;
- ini_set('display_errors', TRUE);
- ini_set('display_startup_errors', TRUE);
This will speed up your development by making sure that the settings you want are there, and most importantly done without clicking around the site!
These are just some of the problems have come up during the days. If you have one you’re always running into, feel free to leave a comment about it!
drupal planetdrupaldevelopmenttips