Skip to content

Instantly share code, notes, and snippets.

View rpayanm's full-sized avatar

Rolando rpayanm

View GitHub Profile
<?php
class ExmapleForm extends FormBase {
// ...
/**
* Mantenemos cuantas veces el form es puesto en la página.
*
* @var int
*/
protected static $instanceId;

Libraries can be attached to all pages, a subset of pages, or to elements in a render array. This allows you to have some assets that are global, and others that are only loaded on an as-needed basis.

Note: While the example below covers how to attach a library in a theme, the same steps work for modules. In the case of modules you'll most likely want to use #attached as part of a Render Array.

  • Attach a library globally to all pages
  • Attach a library to a specific page
  • Use #attached to add a library conditionally via a preprocess function
  • Attach a library from a Twig template

Attach a library globally

git reset --hard <the-sha-you-want-to-return-to>

Or This will undo the last commit.

git reset --hard HEAD~1

Or This will undo the last two commits.

Alter theme hook suggestions

Add your own theme hook suggestions

Modules and themes can alter the list of theme hook suggestions using one of these two hooks. They both function the same but vary in specificity:

  • hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook)
  • hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)

Example:

Edit /home/rpayanm/Programs/PhpStorm/bin/phpstorm.sh and add at the top:

XMODIFIERS=phpstorm.sh
export XMODIFIERS
<?php
/**
* Implements hook_update().
*/
function MODULE_NAME_update_800X(&$sandbox) {
// Use the sandbox at your convenience to store the information needed
// to track progression between successive calls to the function.
if (!isset($sandbox['progress'])) {
// The count of nodes visited so far.
<?php
/**
* Implements hook_mail().
*/
function MY_MODULE_mail($key, &$message, $params) {
if ($key == 'MY_KEY') {
$fid = 4; // For example
$file = File::load($fid);
$message['params']['attachments'][] = [
private $service;

public function __construct($service) {
  $this->service = $service;
}

Call to a member function ...() on null in Drupal\...\Form\...->buildForm()
<?php
/**
* Implements hook_locale_translation_projects_alter().
*/
function MODULENAME_locale_translation_projects_alter(&$projects) {
$path_module = drupal_get_path('module', 'MODULENAME');
$path_translation = $path_module . '/translations/%language.po';