Skip to content

Instantly share code, notes, and snippets.

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:

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.

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

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

The friendly.libraries.yml file will look like this:

friendly-greeting:
  version: 1.0
  license: GPL
  js:
    js/friendly-greeting.js: { }
  dependencies:
    - core/drupalSettings
<?php
// Podemos hacerlo en el hook_entity_presave(), ejemplo en un block.
/**
* Implements hook_entity_presave().
*/
function MODULO_NAME_block_content_presave(BlockContent $entity) {
$value_title = $entity->field_container_url->title;
if ($value_title != "") {
<?php
/**
* Implements hook_module_implements_alter().
*/
function YOURMODULENAME_module_implements_alter(&$implementations, $hook) {
// I want that my hook_form_alter will execute last
// Move our hook_form_alter() implementation to the end of the list.
$my_module = 'MYMODULENAME';
if ($hook == 'form_alter') {
<?php
namespace Drupal\modal_form_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;

There are two ways to discover the hook name of a specific component. With the hook name, you can locate its base template file.

First, if you already know the filename of the template, you can infer the hook name from that.

The typical pattern for naming template files is {hook}--{optional context}.html.twig

Hook name dicovery

You can ignore the .html.twig suffix. Which results in {hook}--{optional context}

If you want to remove the commit altogether (and every commit that came after that)

git reset --hard adca297de~ //(assuming the commit's hash is adca297de). 
git push origin -f