Skip to content

Instantly share code, notes, and snippets.

View mohammadanwar's full-sized avatar
☁️

Mohammad Anwar Siddiqui mohammadanwar

☁️
View GitHub Profile
@mohammadanwar
mohammadanwar / d8_time_ago.php
Created August 28, 2024 06:47 — forked from crittermike/d8_time_ago.php
Convert a timestamp into a time duration ("time ago") in Drupal 8
<?php
print t('@time ago', array('@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($timestamp)));
@mohammadanwar
mohammadanwar / get_node_by_path.php
Created August 28, 2024 06:46 — forked from thagler/get_node_by_path.php
Drupal 8 Get node ID by path alias
<?php
$path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias');
if(preg_match('/node\/(\d+)/', $path, $matches)) {
$node = \Drupal\node\Entity\Node::load($matches[1]);
}
@mohammadanwar
mohammadanwar / router_rebuild.sh
Created August 28, 2024 06:46 — forked from crittermike/router_rebuild.sh
Rebuild Drupal 8 routing table with Drush
drush ev '\Drupal::service("router.builder")->rebuild();'
@mohammadanwar
mohammadanwar / terms.php
Created August 28, 2024 06:46 — forked from crittermike/terms.php
Load terms in a taxonomy in Drupal 8
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('categories');
if (!empty($terms)) {
foreach($terms as $term) {
// do whatever with $term here.
}
}
@mohammadanwar
mohammadanwar / settings.json
Created August 28, 2024 06:44 — forked from mike-potter/settings.json
VSCode settings
{
"editor.fontSize": 14,
"editor.fontFamily": "Liberation Mono for Powerline, Menlo, Monaco, 'Courier New', monospace",
"editor.lineHeight": 17,
"php.suggest.basic": false,
"editor.tabCompletion": "on",
"php-docblocker.qualifyClassNames": true,
"phpcs.standard": "project/tests/.phpcs.xml",
"phpcs.autoConfigSearch": false,
"[php]": {
@mohammadanwar
mohammadanwar / import.php
Created August 28, 2024 06:43 — forked from crittermike/import.php
Importing Drupal 8 config programmatically
<?php
// Import arbitrary config from a variable.
// Assumes $data has the data you want to import for this config.
$config = \Drupal::service('config.factory')->getEditable('filter.format.basic_html');
$config->setData($data)->save();
// Or, re-import the default config for a module or profile, etc.
\Drupal::service('config.installer')->installDefaultConfig('module', 'my_custom_module');
@mohammadanwar
mohammadanwar / IntegerDropdownWidget.php
Created August 28, 2024 06:35 — forked from crittermike/IntegerDropdownWidget.php
Drupal 8 form widget example: creates a select dropdown for integer fields. Place in src/Plugin/Field/FieldWidget
<?php
/**
* @file
* Defines a dropdown widget for integer fields.
*/
namespace Drupal\nba_content_core\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
@mohammadanwar
mohammadanwar / delete_content_entities.d8.php
Created August 28, 2024 06:32 — forked from Jaesin/delete_content_entities.d8.php
Delete all content entities in Drupal 8.
<?php
// Delete all nodes.
entity_delete_multiple('node', \Drupal::entityQuery('node')->execute());
// Delete all files.
entity_delete_multiple('file', \Drupal::entityQuery('file')->execute());
// Delete all taxonomy terms.
entity_delete_multiple('taxonomy_term', \Drupal::entityQuery('taxonomy_term')->execute());
@mohammadanwar
mohammadanwar / drupal_8_building_dynamic_queries_with_injected_services.txt
Created August 28, 2024 06:30 — forked from davidjguru/drupal_8_building_dynamic_queries_with_injected_services.txt
Drupal 8: Building dynamic queries with injected services from a Controller
<?php
/**
* @file
* Contains \Drupal\your_custom_module\Controller\YourCustomModuleController.
*/
namespace Drupal\your_custom_module\Controller;
use Drupal\Core\Controller\ControllerBase;
@mohammadanwar
mohammadanwar / drupal_8_checking_if_a_node_exists_and_create_it.txt
Created August 28, 2024 06:29 — forked from davidjguru/drupal_8_checking_if_a_node_exists_and_create_it.txt
Let's go to check if a node already exists taking by its title...if not, then we'll create one new node and we'll launch it to the render system, ready to show it in screen.
// Context: We are in a Controller, within a method.
// We're catching a request from a route defined in your_custom_module.routing.yml
// Our mission is to return a node ready to be rendered on screen.
// If the node doesn't exist, we will create it.
// Set initial basic properties.
$message = '';
$output = [];
$entity_type = 'node';
$node_title = 'Title of the Node - Chiquito Ipsum';