Skip to content

Instantly share code, notes, and snippets.

View PSF1's full-sized avatar
☀️
Focusing

Pedro Peláez PSF1

☀️
Focusing
View GitHub Profile
@PSF1
PSF1 / gist:f82d31920fa1032e12dce1513948cc89
Created February 19, 2024 10:37
Steps to add a new action operation to Entity
Steps to add a new operation to Entity:
- In the entity class:
-- Add definition in handlers/form
-- Override handlers/route_provider with a new route generator.
-- Add route template in links
- Create a new form to manage the operation.
- Generate a new route in the route generator class.
- Add operation in the entity's ListBuilder.
@PSF1
PSF1 / new_drupal.sh
Created February 5, 2024 10:41
DDEV Create Drupal Project
#!/bin/bash
#
# 2024-02-05 - Creado con gpt-4-1106-preview
# Por Pedro Pelaez
#
# Define una función para imprimir mensajes en azul
print_blue() {
echo -e "\033[34m$1\033[0m"
@PSF1
PSF1 / drupal-8-cheatsheet.md
Created November 14, 2023 15:55 — forked from cesarmiquel/drupal-8-cheatsheet.md
Drupal 8/9/10 Cheatsheet

Drupal 8 Cheatsheet

Files, Images and Media

// Load file object
$file = File::load($fid);

// Get uri (public://foo/bar/baz.png)
$uri = $file->getFileUri();
@PSF1
PSF1 / phpcbf
Last active September 28, 2023 13:52
Drupal code style with DDEV
#!/bin/bash
# Fix code style automatically.
#
# To create the command 'ddev phpcbf', create in '.ddev/commands/web' a new file 'phpcbf'.
# This require, in project, install 'squizlabs/php_codesniffer' and 'drupal/coder' how a dev dependency.
# 2023 By Pedro Pelaez <aaaaa976@gmail.com>
folders=""
@PSF1
PSF1 / local.settings.php
Created August 19, 2020 06:50
Drupal 8+ - Configurar SQL MODE
/**
* Localhost setup.
*/
$databases['default']['default'] = [
'database' => 'db',
'username' => 'db',
'password' => 'db',
'prefix' => '',
'host' => 'mariadb',
'port' => '3306',
@PSF1
PSF1 / gist:370bf1ef17260d7b1efaa8739da2e74a
Last active December 12, 2019 15:15
Drupal 8: Get users by permission
// Get administrators.
$roles = user_role_names(FALSE, 'administer reservable_list_form petitions');
$rids = array_keys($roles);
$uids = Drupal::service('entity_type.manager')->getStorage('user')->getQuery()
->condition('roles', $rids, 'IN')
->execute();
$users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple($uids);
Drupal\Core\PageCache\ResponsePolicy\KillSwitch
class ...
/**
* Kill cache service.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $killSwitch;
@PSF1
PSF1 / field_config.php
Created November 7, 2019 17:56
Get field configuration
public static function getFieldConfig($nodeBundle, $field_name) {
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_manager->useCaches(FALSE);
$fieldsSettings = $entity_field_manager->getFieldDefinitions('node', $nodeBundle);
if (!isset($fieldsSettings[$field_name])) {
return NULL;
}
/** @var \Drupal\field\Entity\FieldConfig $fieldSetting */
$fieldSetting = $fieldsSettings[$field_name];
return $fieldSetting;
@PSF1
PSF1 / gist:34c20aaeede2428cc4d83106946a41f2
Last active November 28, 2019 17:57
Allow use of twig render in Drupal 8.7 inside cron or console
// Load twig engine in cron.
include_once \Drupal::root() . '/core/themes/engines/twig/twig.engine';
// Render a render array.
$renderer = \Drupal::service('renderer');
$result = $renderer->renderPlain($output);
@PSF1
PSF1 / alter_custom_text_views_field.module
Last active June 14, 2019 07:49
How to alter the content of a custom text field in a view in Drupal 8.7.
<?php
/**
* @file
* Primary module hooks for alter_custom_text_views_field module.
*
* To use in a module called "alter_custom_text_views", in Drupal 8.
*
* @DCG
* This file is no longer required in Drupal 8.