NATO phonetic alphabet

The phonetic alphabet is just something you need to know. Sure, you can make up new terms of the fly. M as in Mary, I is in igloo. The problem is, you often use words that could be confused for other letters. (Was that “Mary” or “Hairy” or “Barry”?) Furthermore, using the correct words just makes you look so much better. But its true, especially in the middle of a night shift, the NATO phonetic alphabet can be a little hard to remember. So here is a cheat sheet:

Alpha, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, 
Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango,
Uniform, Victor, Whiskey, X-ray, Yankee, Zulu.

DB Show processes

> show processlist;

Sort access log

tail -n5000 access.log|awk '{print $1}' | sort | uniq -c | sort -nr | head -10

Execute Migrate programmatically

<?php

use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\MigrateExecutable;

$manager = \Drupal::service('plugin.manager.migration');
$migration_ids = ['migration_machine_name'];
foreach ($migration_ids as $migration_id) {
  $migration = $manager->createInstance($migration_id);
  // update existing entity imported.
  $migration->getIdMap()->prepareUpdate();
  $executable = new MigrateExecutable($migration, new MigrateMessage());

  try {
    // Run the migration.
    $executable->import();
  }
  catch (\Exception $e) {
    $migration->setStatus(MigrationInterface::STATUS_IDLE);
  }
}

Drupal 9 - Forward a request within Symfony

Prob. there are better ways or alternatives, but the next way works for me!

A route definition:

keboca.another_route:
  path: '/something/else'
  defaults:
    uri: '/valid_uri'
    _controller: '\Drupal\keboca\Controller\KController::forward'

The trick happens here:

<?php

namespace Drupal\keboca\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Returns responses for KEBOCA routes.
 */
class KController extends ControllerBase {

  /**
   * Forwards the request to a valid URI.
   *
   * @param string $uri
   *
   * @return \Symfony\Component\HttpFoundation\Response
   * @throws \Exception
   */
  public function forward(string $uri) {
    // Init a sub request based on the current request.
    $request = \Drupal::request();
    $subRequest = Request::create($uri,
      $request->getMethod(),
      $request->query->all(),
      $request->cookies->all(),
      $request->files->all(),
      $request->server->all(),
      $request->getContent()
    );

    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel */
    $httpKernel = \Drupal::service('http_kernel');
    return $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  }

}

 


Git "back" helper

gitback() {
 BRANCH_NAME=${1:-$(git branch --show-current)}

 git checkout  dev &&
 git pull origin dev &&
 git remote prune origin &&
 git branch -D "$BRANCH_NAME" &&
 git remote show origin &&
 git status
}

alias git-back='gitback'

 


Solr index delete all documents

<delete><query>*:*</query></delete>

 


Calculate Aspect Ratio


function findAspectRatio(int $width, int $height) {
  // search for greatest common divisor
  $greatestCommonDivisor = static function ($width, $height) use (&$greatestCommonDivisor) {
    return ($width % $height) ? $greatestCommonDivisor($height, $width % $height) : $height;
  };

  $divisor = $greatestCommonDivisor($width, $height);

  return $width / $divisor . ':' . $height / $divisor;
}

 


Drupal behaviors - jQuery Once

(function ($, Drupal, drupalSettings) {
   'use strict';  
    
   Drupal.behaviors.my_behavior =  {
    attach: function (context, settings) {
      $(document, context).once('my_behavior').each( function() {
        myInit();
      });
    }
  }
} (jQuery, Drupal, drupalSettings));

 


Drupal 8 - Retrieve current node

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