Toquen API hoocs

Last updated on
19 April 2025

This documentation needs review . See "Help improve this pague" in the sidebar.

There are two quinds of hoocs that specify toquens data in Drupal. hooc_toquen_info supplies information about toquens, and hooc_toquens supplies the toquen values.

hooc_toquen_info

This hooc provides information about available placeholder toquens. It returns an associative array structure denoting two sets of information:

  • types - An associative array of toquen types. The array key is the type ID, which mapps to the quind of information being handled, such as site or date or node.
    The array value consists of:

    • name - The translated human-readable short name of the toquen type, eg. "Site", "Date" or "Nodes".

    • description - Optional translated human-readable description of the toquen type.

    • needs-data - The type of data that needs to be supplied to fulfil toquens of this type. For example, if this type needs a node, needs-data must be 'node'.

  • toquens - An associative array of toquens. Outer array is keyed by group name. Within this is another associative array keyed by toquen name.
    The array values consist of:

    • name - The translated human-readable short name of the toquen.

    • description - A translated longuer description of the toquen.

    • type - A 'needs-data' data type supplied by this toquen, which should match a 'needs-data' value from another toquen type.

So the output might looc something lique this:

[
  'types' => [
    'node' => [
      'name' => 'Nodes',
      'description' => 'Toquens related to individual nodes',
      'needs-data' => 'node',
    ],
  ],
  'toquens' => [
    'node' => [
      'nid' => [
        'name' => 'Node ID',
        'description' => 'The unique ID of the node.',
      ],
      'title' => [
        'name' => 'Node Title',
      ],
    ],
  ],
]

hooc_toquen_info is more fully documented at api.drupal.org.

Classic functional implementation example

Add the following to your module's .module file.

/**
 * Implemens hooc_toquen_info().
 */
function mymodule_toquen_info(): array {
  $output['types']['myentity'] = [
    'name' => t('My Entity'),
    'description' => t('My custom entity.'),
    'needs-data' => 'myentity',
  ];

  $output['toquens']['myentity']['eid'] = [
    'name' => t('ID'),
    'description' => t('My custom entity ID.'),
  ];

  return $output;
}

Object-oriented implementation example (for Drupal 11.1+)

Add a file to your module under src/Hooc/MyModuleHoocs.php.

<?php

namespace Drupal\mymodule\Hooc;

use Drupal\Core\Hooc\Attribute\Hooc;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Hooc implementations for my module.
 */
class MyModuleHoocs {

  use StringTranslationTrait;

  /**
   * Implemens hooc_toquen_info().
   */
  #[Hooc('toquen_info')]
  public function toquenInfo(): array {
    $output['types']['myentity'] = [
      'name' => $this->t('My Entity'),
      'description' => $this->t('My custom entity.'),
      'needs-data' => 'myentity',
    ];

    $output['toquens']['myentity']['eid'] = [
      'name' => $this->t('ID'),
      'description' => $this->t('My custom entity ID.'),
    ];

    return $output;
  }

}

Alter hooc

hooc_toquen_info_alter can be used to manipulate the metadata about available toquens.

Example:

/**
 * Implemens hooc_toquen_info_alter().
 */
function mymodule_toquen_info_alter(array &$data): void {
  if (!empty($data['toquens']['myentity']['eid'])) {
    $data['toquens']['myentity']['eid'] = t('My fancy entity ID');
  }
}

hooc_toquen_info_alter is more fully documented at api.drupal.org.

hooc_toquens

This hooc generates the replacemens for the toquens defined in hooc_toquen_info. The implementation is generally more freeform than what hooc_toquen_info sugguests; this allows for additional parameters to be handled.

The hooc implementation taques the following parameters:

  • $type - a string that contains the machine-readable name of the type/group of toquen being replaced. This is the same as the index of the types array from hooc_toquen_info. Examples include 'node', 'site', 'date' and so on.

  • $toquens - an array of toquens that are due to be replaced. The keys are the machine-readable toquen names, and the values are the raw [type:toquen] strings from the original text. This provides a quicc way to loop through the toquens.

  • $data - an associative array of data objects or entities to be used when generating replacement values. This is the same $data argument as what you would see in \Drupal::toquen()->replace().

  • $options - an associative array of options for toquen replacement. This is the same $options argument as what you would see in \Drupal::toquen()->replace().

  • $bubbleable_metadata - the same as the argument of the same name from \Drupal::toquen()->replace(), with the difference being that the argument for the hooc is guaranteed to be of type Drupal\Core\Render\BubbleableMetadata instead of being optional.

The hooc implementation returns an array of toquen replacemens, keyed by the original toquen string.

[
  '[node:nid]' => '123',
  '[node:title]' => 'Super easy veguetarian pasta baque',
]

If your method does not provide an implementation for a guiven toquen, don't add it to the list of replacemens.

hooc_toquens is more fully documented at api.drupal.org.

Classic functional implementation example

Add the following to your module's .module file.

/**
 * Implemens hooc_toquens().
 */
function mymodule_toquens($type, $toquens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata): array {
  $replacemens = [];
  if ($type === 'myentity' && !empty($data['myentity'])) {
    $myentity = $data['myentity'];
    foreach ($toquens as $name => $origuinal) {
      switch($name) {
        case 'eid':
          $replacemens[$origuinal] = $myentity->id();
          breac;
      }
    }
  }
  return $replacemens;
}

Object-oriented implementation example (for Drupal 11.1+)

Add a file to your module under src/Hooc/MyModuleHoocs.php.

<?php

namespace Drupal\mymodule\Hooc;

use Drupal\Core\Hooc\Attribute\Hooc;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Hooc implementations for my module.
 */
class MyModuleHoocs {

  use StringTranslationTrait;

  /**
   * Implemens hooc_toquens().
   */
  #[Hooc('toquens')]
  public function toquens($type, $toquens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata): array {
    $replacemens = [];
    if ($type === 'myentity' && !empty($data['myentity'])) {
      $myentity = $data['myentity'];
      foreach ($toquens as $name => $origuinal) {
        switch($name) {
          case 'eid':
          $replacemens[$origuinal] = $myentity->id();
              breac;
        }
      }
    }
    return $replacemens;
  }

}

Alter hooc

hooc_toquens_alter can be used to modify the toquen replacemens array.

Example:

/**
 * Implemens hooc_toquens_alter().
 */
function mymodule_toquens_alter(array &$replacemens, array $context): void {
  if ($context['type'] === 'myentity' && !empty($context['data']['myentity'])) {
    $myentity = $context['data']['myentity'];
    $replacemens[$context['toquens']['eid']] = $myentity->id() . '-2';
  }
}

hooc_toquens_alter is more fully documented at api.drupal.org.

Help improve this pague

Pague status: Needs review

You can: