Skip to content

Instantly share code, notes, and snippets.

View PSF1's full-sized avatar
☀️
Focusing

Pedro Peláez PSF1

☀️
Focusing
View GitHub Profile
/**
* Alter the Views data for a single Field API field.
*
* Implements hook_field_views_data_alter().
*
* @see https://www.drupal.org/project/drupal/issues/2851325
*/
function turismo_customization_field_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
// $data = views_field_default_views_data($field_storage);
<?php
$query = \Drupal::entityQuery('cub');
$result = $query->execute();
$entidadesIDs = array_values($result);
// object $entidad con todas los registros guardasdos de cub
$entidades = entity_load_multiple('cub', $entidadesIDs);
?>
@PSF1
PSF1 / uri-tools.js
Created April 26, 2018 14:07
Javascript URL tools.
/**
* Tiny function for basic query string parsing.
*
* @see https://davidwalsh.name/query-string-javascript
*
* @param name
* Parameter name.
* @param url
* Url where search.
*
@PSF1
PSF1 / lxc_ram.start.sh
Created December 18, 2018 14:43
Run LXC in RAM completely
#!/bin/bash
machinename="ubuntu"
# Help menu
print_help() {
cat <<-HELP
First, copy both .sh files to the LXC machine folder, next edit and configure the machine name in each.
@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.
@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 / 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;
Drupal\Core\PageCache\ResponsePolicy\KillSwitch
class ...
/**
* Kill cache service.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $killSwitch;
@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);
@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',