Calculate Aspect Ratio

kenneth

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

kenneth
(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

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

 

Subscribe to