Skip to content

Instantly share code, notes, and snippets.

View mohammadanwar's full-sized avatar
☁️

Mohammad Anwar Siddiqui mohammadanwar

☁️
View GitHub Profile
/* Setting up codebase */
mkdir d7d8mod
cd d7d8mod
ddev config
ddev start
ddev composer create drupal/recommended-project
cp web/sites/default/default.settings.php web/sites/default/settings.php
ddev config
ddev restart
ddev . drush site-install --account-pass=admin
@mohammadanwar
mohammadanwar / README-Template.md
Created August 28, 2024 07:01 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@mohammadanwar
mohammadanwar / date.php
Created August 28, 2024 07:00 — forked from rang501/date.php
Get correct time from datetime field (D8)
<?php
$date = DrupalDateTime::createFromFormat(DATETIME_DATETIME_STORAGE_FORMAT, 'datetime string', DATETIME_STORAGE_TIMEZONE);
$date->setTimeZone(new DateTimeZone(drupal_get_user_timezone()));
$date->format('d.m.Y H:i');
@mohammadanwar
mohammadanwar / file.php
Created August 28, 2024 06:59 — forked from rang501/file.php
Drupal 8 get image dimensions by applying image style effects. This doesn't cause performance issue because the dimensions are calculated without creating the actual image.
$image = \Drupal::service('image.factory')->get($file->getFileUri());
$image_style = \Drupal\image\Entity\ImageStyle::load('gallery_large');
// Set source image dimensions.
$dimensions = [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
// After calling this, the $dimensions array will contain new dimensions.
$image_style->transformDimensions($dimensions, $file->getFileUri());
@mohammadanwar
mohammadanwar / AwsCacheAdapter.php
Created August 28, 2024 06:54 — forked from mirie/AwsCacheAdapter.php
flysystem classes
<?php
namespace Drupal\flysystem_s3;
use Aws\CacheInterface;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* A Drupal cache adapter for use with the AWS PHP SDK.
*/
/**
* Implements hook_file_insert().
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Save file metadata.
if (!empty($this->metadata)) {
if ($update) {
db_delete('file_metadata')->condition('fid', $this->id())->execute();
}
@mohammadanwar
mohammadanwar / p2_drupal_pr_template.md
Created August 28, 2024 06:51 — forked from mirie/p2_drupal_pr_template.md
P2 PR Template Draft (Drupal Project)

Definition of Done:

This section is for your verification before submitting a PR. Remove this before submitting:

  • Has any required automated testing been written/updated?
  • Has the documentation been added/updated?
  • Is there appropriate logging included?
  • Does this add new modules/themes? If so:
    • Has the make file been updated?
    • Has the install profile's info file been updated?
    • Have you run a test build locally to confirm your new modules/themes get downloaded and enabled properly?
  • Does this add new configuration? If so:
@mohammadanwar
mohammadanwar / git_hook.md
Created August 28, 2024 06:49 — forked from crittermike/git_hook.md
Git commit hooks not running? Check the file permissions on them.

Git hooks need to have executable file permissions. If one isn't running for you, try:

chmod -R 755 .git/hooks/YOURHOOKNAME
@mohammadanwar
mohammadanwar / nodes_in_term.php
Created August 28, 2024 06:49 — forked from crittermike/nodes_in_term.php
Loading nodes tagged with a taxonomy term in Drupal 8
<?php
// By term tids
$query = \Drupal::entityQuery('node')
->condition('type', 'YOUR_NODE_TYPE')
->condition('status', 1)
->condition('field_TERMFIELDNAME.entity.tid', $array_of_tids, 'IN');
$nids = $query->execute();
// By term names
@mohammadanwar
mohammadanwar / is_admin.php
Created August 28, 2024 06:48 — forked from crittermike/is_admin.php
How to tell if you're on an admin path in Drupal 7 and 8
// Drupal 7
if (path_is_admin(current_path())) {
// Do stuff on admin pages.
}
// Drupal 8
$is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
if (!$is_admin) {
// Do stuff on admin pages.
}