Notes

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);
  }

}

 

Friday, April 1, 2022 - 15:21
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'

 

Monday, February 28, 2022 - 12:02
Solr index delete all documents
<delete><query>*:*</query></delete>

 

Monday, February 28, 2022 - 11:13
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;
}

 

Friday, December 3, 2021 - 09:14
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));

 

Monday, November 8, 2021 - 11:14
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