mustache_templates-8.x-1.0-beta4/modules/mustache_token/mustache_token.module

modules/mustache_token/mustache_token.module
<?php

/**
 * @file
 * Mustache Logic-less Tokens module file.
 */

use Drupal\mustache\Render\Markup;

/**
 * Implements template_preprocess_table__token_tree().
 *
 * Replaces the Token syntax by Mustache template syntax.
 */
function mustache_token_preprocess_table__token_tree(&$variables) {
  if (!isset($variables['rows']) || !is_array($variables['rows'])) {
    return;
  }

  $variables['caption'] = Markup::create(((string) $variables['caption'] ?? '') . ' ' . (string) t('
      <p>You can write Mustache templates:</p>
      <ul>
        <li>Use tokens within Mustache brackets, for example {{site.name}}</li>
        <li>Also supports conditionals: {{#node.title}}&lt;h1&gt;{{node.title}}&lt;/h1&gt;{{/node.title}}</li>
        <li>See the <a href="https://mustache.github.io/mustache.5.html" target="_blank" rel="noopener noreferrer">Mustache manual</a> for more details.</li>
      </ul>')
    );

  /** @var \Drupal\mustache\Plugin\MustacheMagicManager $magic_manager */
  $magic_manager = \Drupal::service('plugin.manager.mustache.magic');
  $magic_plugins = [];
  foreach ($magic_manager->getDefinitions() as $definition) {
    if (mb_strpos((string) $definition['description'], '{{#' . $definition['id'])) {
      $magic_plugins['{{' . $definition['id'] . '.?}}'] = $definition['id'];
    }
  }

  foreach ($variables['rows'] as &$row) {
    if (isset($row['cells']['token']['content'])) {
      $token_content = $row['cells']['token']['content'];
      $token_content = str_replace('[', '{{', $token_content);
      $token_content = str_replace(']', '}}', $token_content);
      $token_content = str_replace(':', '.', $token_content);
      $token_content = str_replace('-', '_', $token_content);
      if ($token_content === '{{iterate.loop.?}}') {
        $token_content = '{{#iterate.loop.?}} ? {{/iterate.loop.?}}';
      }
      elseif (isset($magic_plugins[$token_content])) {
        $token_content = '{{#' . $magic_plugins[$token_content] . '.?}} ? {{/' . $magic_plugins[$token_content] . '.?}}';
      }
      $row['cells']['token']['content'] = $token_content;
    }
  }
  unset($row);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc