Skip to content

Instantly share code, notes, and snippets.

View syammohanmp's full-sized avatar

Syam Mohan M P syammohanmp

View GitHub Profile
@syammohanmp
syammohanmp / drupal-twig-cheat.md
Created March 12, 2024 15:13
Drupal Twig Cheat Sheet

Drupal Cheat Sheet

How to get image URL of a node

{% if content.field_image['#object'] is not empty %}
  {% set image_src = file_url(content.field_image['#object'].field_image.entity.fileuri) %}
{% else %}
  {% set image_src = 'https://via.placeholder.com/600x400' %}
{% endif %}
@syammohanmp
syammohanmp / custom-entity-field.md
Created November 8, 2023 08:23
How to add custom field to a custom entity in Drupal.

How to add custom field to a custom entity in Drupal.

Make install file.

Step 1: Add hook_install to custom_module.install file.

 /**
 * Implements hook_install().
 */
@syammohanmp
syammohanmp / lando-drupal-redis-setup.md
Last active October 12, 2023 08:12
Drupal Redis setup using Lando

Drupal Redis setup using Lando

Run below composer commands.

  • lando composer require predis/predis
  • lando composer require drupal/redis

Modify .lando.yml and add redis service.

services:
@syammohanmp
syammohanmp / pre-commit.sh
Created September 1, 2023 13:52
PHP Codesniffer to Git Pre commit
#!/bin/sh
PROJECT=$(php -r "echo dirname(dirname(dirname(realpath('$0'))));")
STAGED_FILES_CMD=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\.php)
UNSTAGED_FILES_CMD=$(git diff --name-only --diff-filter=ACMR | grep \\.php)
# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
oIFS=$IFS
@syammohanmp
syammohanmp / composer-downgrade.md
Created March 23, 2023 12:45
How to downgrade composer to its lower version.

How to Downgrade Composer to Version 1

With the release of Composer version 2, ServerPilot now provides this version to all servers by default. However, some applications may not be fully compatible with version 2, and so you may need to use version 1 until they address those incompatibilities.

If for any reason you need to downgrade Composer to the latest version 1 release, you can do so by running the following command as root:

sudo composer self-update --1

To prevent ServerPilot from automatically updating its package to version 2 in the future, you can tell the advanced package tool (apt) software to hold our package by running the following command as root:

sudo apt-mark hold sp-composer

@syammohanmp
syammohanmp / media-detection.js
Created November 16, 2022 15:36
Media detection JavaScript functions
const breakpoints = {
xs: 480,
sm: 768,
md: 1024,
lg: 1280,
xl: 1440
}
function minWidth(screensize){
return window.matchMedia(`(min-width: ${screensize}px)`);
@syammohanmp
syammohanmp / .lando.yml
Created October 28, 2022 13:51
Lando configuration for Drupal9 recipe
name: drupal9
recipe: drupal9
config:
webroot: docroot
services:
phpmyadmin:
type: phpmyadmin
hosts:
- database
proxy:
@syammohanmp
syammohanmp / SampleScheduledAds.php
Last active December 16, 2022 13:34
Scheduled Ads Drupal settings form.
<?php
namespace Drupal\sample_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\sample_module\Form\SampleAdsSettingsForm;
@syammohanmp
syammohanmp / drupal-commands.md
Last active December 11, 2023 09:00
Useful commands for Drupal

Useful commands for Drupal

Generate SSH Key

$ ssh-keygen -t ed25519 -C "abc@bcd.com"

Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

$ ssh-keygen -t rsa -b 4096 -C "abc@bcd.com"

Password change

@syammohanmp
syammohanmp / get_time_ago_format.php
Last active October 28, 2022 12:03
Get Time ago format.
<?php
/**
* Get Time ago format.
*/
function timeDiff($datetime) {
$time = time() - strtotime($datetime);
$units = [
31536000 => 'year',