Notes

Drupal 8 - Retrieve current node
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  $nid = $node->id();
}

 

Wednesday, September 9, 2020 - 11:21
To use different ssh key depending on the repository
git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa -F /dev/null"

 

Thursday, March 19, 2020 - 14:31
Git reset last commit without losing changes
git reset HEAD~1

 

Thursday, December 5, 2019 - 14:23
Git autocorrect typos
git config --global help.autocorrect 1

 

Monday, November 25, 2019 - 10:28
Prepare git-checkout statements by using `awk` Linux

Having a status like this:

$ git status
modified:   composer.json
modified:   composer.lock
deleted:    config/webform.webform.foo.yml
deleted:    config/webform.webform.bar.yml
deleted:    config/webform.webform.bazyml
modified:   config/core.extension.yml

If I want to revert only `webform` changes by running something like,

$ git checkout config/webform.webform.*

It won't work because those are deleted files, then I can use `awk` to create `checkout` statements,

$ git status | grep webform | awk  '{ print "git checkout " $2; }'
git checkout config/webform.webform.foo.yml
git checkout config/webform.webform.bar.yml
git checkout config/webform.webform.baz.yml

Then copy and paste once and enjoy your wonderful day!

EDIT:

As a heads-up from a friend of mine, if we include double quotes it will work, as below:

git checkout "config/webform.webform.*"
Updated 3 paths from the index

PD:

Ha! An extra trick, if you include `| xargs -0 bash -c ` at the end, it will get execute it right away:

git status | grep webform | awk  '{ print "git checkout " $2; }' | xargs -0 bash -c                              
Updated 1 path from the index
Updated 1 path from the index
Updated 1 path from the index

 

Friday, November 8, 2019 - 11:37
Drupal 8 - raw SQL queries for debugging
/** @var \Drupal\Core\Database\Query\SelectExtender $query */
echo $query->__toString();
var_dump($query->getArguments());

 

Friday, October 18, 2019 - 12:10
CSS - Typical Device Breakpoints
 /* Extra small devices (phones, 600px and down) */
  @media only screen and (max-width: 600px) {
  }

  /* Small devices (portrait tablets and large phones, 600px and up) */
  @media only screen and (min-width: 600px) {
  }

  /* Medium devices (landscape tablets, 768px and up) */
  @media only screen and (min-width: 768px) {
  }

  /* Large devices (laptops/desktops, 992px and up) */
  @media only screen and (min-width: 992px) {
  }

  /* Extra large devices (large laptops and desktops, 1200px and up) */
  @media only screen and (min-width: 1200px) {
  }

 

Friday, October 11, 2019 - 08:27
Valid URL from Link field into a Twig template
{#  it will work either external or internal link #}
{{ node.field_image_link.get(0).getUrl().toString() }}

 

Wednesday, October 9, 2019 - 13:48
Drupal 8 - The following module is missing from the file system... hotfix
drush sql-query "DELETE FROM key_value WHERE collection='system.schema' AND name='module_name';"

source: https://www.drupal.org/node/2487215

Friday, July 26, 2019 - 09:53
Drupal 8 - Country name list by code
/** @var array $countries */
$countries = \Drupal::service('country_manager')
  ->getList();
// Transform TranslatableMarkup object into a string.
array_walk($countries, function (&$translation) {
  /** @var  Drupal\Core\StringTranslation\TranslatableMarkup $translation */
  $translation = $translation->render();
});
var_dump($countries);

Drupal 8 ships with a service to easily retrieve all countries by code. Be sure to use dependency injection.

Tuesday, July 16, 2019 - 16:17