1

I have a content type with a mix of paragraphs and custom blocks. Most custom blocks (content) are the same on all pages except for one word that needs to be replaced by an other one. I would like to replace that word with an other one related to a field on the content type page.

I thought it could be done with Token Filter (https://www.drupal.org/project/token_filter) but that is not an option.

EDIT 2: explain with example I have a content type 'CMS page'. It has a field 'field_brand_name'. This field is used to replace a string in paragraphs or custom blocks that exists on the content type page.

If I create a page for Magento and a page for Drupal and a page for WordPress. The only that needs to be replaced is the brand name in the text (Magento, Drupal or WordPress) on all custom blocks or paragraphs that I have used on that specific CMS page ... That could be resolved by a Token if there was a context for it, but there is no ... so the token is not replaced. Is that clear enough?

9
  • Please explain why this question is voted down. I will help if more info is needed.
    – Lance
    Commented Nov 2, 2020 at 13:46
  • I just saw another perfectly reasonable question voted down. Such a shame that StackExchange is a hostile place for newcomers. Commented Nov 2, 2020 at 13:57
  • token_filter is a good choice for where a token already exists. See /admin/help/token - it sounds like you need a method for creating a token Commented Nov 2, 2020 at 13:59
  • I understand, but the issue is that the token used in a custom block does not know from which node the token must be loaded from. Even if the custom block exists on the node it should load the value from. I found this article for creating a custom token: kevinquillen.com/creating-custom-token-drupal-8 but that is not enough.
    – Lance
    Commented Nov 2, 2020 at 14:14
  • Maybe it's me, but I have a hard time understanding the question. Maybe that's the reason why it's voted down (although some explaining would be nice). Can you add an example of what you're trying to achieve? Also, I think the readability would improve if you would use less bold text. Commented Nov 2, 2020 at 14:39

1 Answer 1

0

First create your own module. In mymodule_tokens.module insert below:

<?php

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */

function mymodule_tokens_token_info(): array
{
    $type = array(
        'name' => t('My Module Tokens'),
        'description' => t('Custom tokens to solve use-case problems for the website.'),
    );
    $node['brand'] = array(
        'name' => t("Brand name from node"),
        'description' => t("Brand name to replace in text, custom blocks, paragraphs, ..."),
    );
    return array(
        'types' => array(
            'mymodule_tokens' => $type,
        ),
        'tokens' => array(
            'mymodule_tokens' => $node,
        ),
    );
}

/**
 * Implements hook_tokens().
 */

function mymodule_tokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata): array
{
    $replacements = [];

    if ($type == 'mymodule_tokens') {
        foreach ($tokens as $name => $original) {
            switch ($name) {
                case 'brand':
                    $node = \Drupal::routeMatch()->getParameter('node');
                    if ($node instanceof \Drupal\node\NodeInterface) {
                        if ($node->hasField('field_brand') && !$node->get('field_brand')->isEmpty()) {
                            $brand_name = $node->field_brand->value;
                            $replacements[$original] = $brand_name;
                        } else {
                            $replacements[$original] = null;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    return $replacements;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.