Using try catch - php exceptions

Not many devs use try catch in custom code, especially in drupal 7. Thing is that it can save you from bigger problems if you wrap your (bad) code into that block, so lets just make simple example how to use it.

Use file_scan_directory and sort files in array

If you are using file_scan_directory to scan files in some directory and then loop over them to use them for some action, you probably also want to sort them, otherwise you will get them in order that is set in filesystem. So to do that, you need to set key in options to "filename" to use filename as key, so you can sort that associative array per key.

Flush image styles in drupal 7

To flush some image style, you can install module for that with UI, but you can also do it in lean way, with just drush php command or if locally with devel/php window. If using drush use this command

 drush php-eval '$style = image_style_load("optimize_original"); image_style_flush($style);'

 

Get translated value of particular taxonomy term

First get current language

$language =  \Drupal::languageManager()->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId();

then load term with providing term id

Orphaned migration config breaks migration UI

If you created some migration config and then you renamed it later, there is a good chance it is still present in config and it can also break your migration UI for that migration group. To clear that out, check your config with drush cedit find id of config and run this in devel/php or drush php

Cannot save files in PHPStorm

Maybe you came to that annoying message when you want to save something in PHP Storm but it refuses to and gives you options "cancel" or "revert changes", usually happens around some config files, like settings.php. Why is this so? Probably because you didn't put write permissions to folder where phpstorm is trying to write its temp files.

Get values from $settings variable

When you set or override some variables in settings.php with $settings var and want to get it in your code, call it like this.

 

use Drupal\Core\Site\Settings;

$mySetting = Settings::get('some_setting', NULL);

 

Configure git to ignore some files locally without .gitignore file

Some times you dont want to add something to .gitignore, while it would be nice that there is some .gitignore-local version by default, there is not, so you need to find a different way to do this, the way to do is this.  Go to .git/info/exclude file and edit it like regular .gitignore file, add files/folders you want to ignore.

Xdebug and docker4drupal on mac

To have xdebug working, you need to have this in your docker config for PHP.

      PHP_XDEBUG: 1
      PHP_XDEBUG_DEFAULT_ENABLE: 1
      PHP_XDEBUG_REMOTE_CONNECT_BACK: 0
      PHP_XDEBUG_REMOTE_HOST: 10.254.254.254 # macOS, Docker < 18.03

Also it is often noted that this should also be run