Notes
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();
}
To use different ssh key depending on the repository
git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa -F /dev/null"
Git reset last commit without losing changes
git reset HEAD~1
Git autocorrect typos
git config --global help.autocorrect 1