Posts

File upload security in Drupal

  finfo_file  https://www.drupal.org/project/file_upload_secure_validator/

Drupal example using migrate module, migrations, drush import, revert import

 https://www.drupal.org/project/path_redirect_import

Nginx config for redirect map

 Using 'Map' for redirects map_hash_bucket_size 128; map $uri $new_uri { /test-to-rewrite /test-rewritten; /test2-to-rewrite /test2-rewritten; } server { server_name example.com ; if ($new_uri) { return 301 $new_uri; } #... }         Source: https://www.dogsbody.com/blog/nginx-optimising-redirects/ Another example for using query strings: http {      map_hash_bucket_size 128;     map $uri$is_args$args $new_uri {         # /hello?123     /hello123?345;         include /my-location/redirect.tsv;         default         $uri$is_args$args;   # Default to the original URI if not matched     }    server {    location / {       if ($new_uri != $uri$is_args$args) {          return 302 $new_uri;       }     } } File...

Drupal caching

 Use case1: You return data as a JSON API via an endpoint. Ex: /api/mydata. This data is fetched from a config form. Now, we have to cache the JSON API response and refresh it when the config form is updated. Code: $config = $this->config(MySettingsForm::my_config_name); $cacheMeta = new CacheableMetadata(); $cacheMeta->addCacheTags($config->getCacheTags()); $data = ['key' => 'value']; $response = new CacheableJsonResponse(json_encode([$data], JSON_UNESCAPED_SLASHES), 200, [], TRUE); $response->addCacheableDependency($cacheMeta); return $response; When this code is added to the controller, Drupal stores following cache id (cid) in cache_config table when the response is created for the first time. For subsequent requests the cached data is returned. When the config form is updated the cached data is refreshed using the cache id (cid) cache id: config:my_module.my_config_name 

Upgrade from D9 to D10

 Ngnix change: Change from: location @rewrite {     rewrite ^/(.*)$ /index.php?q=$1; } To: location @rewrite {     rewrite ^ /index.php; # For Drupal >= 7   }

Setting up SSH keys, Git to contribute to Drupal.org

SSH Keys:  https://www.drupal.org/drupalorg/docs/user-accounts/git-authentication-for-drupalorg-projects cd ~/.ssh ssh-keygen -t ed25519 -b 4096 -C " drupalcode.org " Enter the filename. Ex:  id_ed25519_drupalcode_org ssh-add ~/.ssh/ id_ed25519_drupalcode_org vim  id_ed25519_drupalcode_org.pub copy the contents and create a new key at  https://git.drupalcode.org/-/profile/keys Test:  ssh -T git@git.drupal.org You can also add ssh config file  vim ~/.ssh/config Save the below contents Host git.drupal.org   AddKeysToAgent yes   IdentityFile ~/.ssh/id_ed25519_drupal_org Configuring Git: https://www.drupal.org/docs/develop/git/setting-up-git-for-drupal/configuring-git-for-drupal Note: Configure your name and email address https://www.drupal.org/user/<your-id>/git Committing: Make sure username and email are correct in the folder you are committing > git config --list   credential.helper=osxkeychain init.defaultbranch=main user.email= gop...

PHP Ngnix Mysql in OSX with multiple PHP Versions

 https://kevdees.com/macos-11-big-sur-nginx-setup-multiple-php-versions/ https://github.com/shivammathur/homebrew-php == Upgrade PHP version from 8.2 to 8.3 1. Take the backup of PHP 8.2 ini file from /usr/local/etc/php/8.2/php.ini /usr/local/etc/php/php.ini 2. Remove PHP 8.2 brew uninstall shivammathur/php/php@8.1 3. Install PHP 8.3 # Add the PHP and PHP Extension taps brew tap shivammathur/php brew tap shivammathur/extensions # Install PHP 8.3 brew install shivammathur/php/php@8.3 # Link PHP 8.3 CLI executable as `php` brew link --overwrite --force shivammathur/php/php@8.3 # Test installation php -v 4. Restart sudo killall php-fpm sudo service php-fpm restart 5. Edit config file vim /usr/local/etc/php/8.3/php-fpm.d/www.conf # default user = _www group = _www listen = 127.0.0.1:9000 # change to user = <your_username> group = staff listen = 127.0.0.1:9074 brew services restart php@8.3 Other tools Xdebug in shivammathur/homebrew-php brew install shivammathur/extensions/xdebug@...