YamlFormTokenManager.php in YAML Form 8
File
src/YamlFormTokenManager.php
View source
<?php
namespace Drupal\yamlform;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Utility\Token;
class YamlFormTokenManager implements YamlFormTokenManagerInterface {
protected $moduleHandler;
protected $token;
public function __construct(ModuleHandlerInterface $module_handler, Token $token) {
$this->moduleHandler = $module_handler;
$this->token = $token;
}
public function replace($text, EntityInterface $entity = NULL, array $data = [], array $options = []) {
if (is_array($text)) {
foreach ($text as $key => $value) {
$text[$key] = $this
->replace($value, $entity);
}
return $text;
}
if (!is_string($text) || strpos($text, '[') === FALSE) {
return $text;
}
$text = str_replace('[yamlform-submission:', '[yamlform_submission:', $text);
$this
->setTokenData($data, $entity);
$options += [
'clear' => TRUE,
];
return $this->token
->replace($text, $data, $options);
}
public function buildTreeLink() {
if ($this->moduleHandler
->moduleExists('token')) {
return [
'#theme' => 'token_tree_link',
'#token_types' => [
'yamlform',
'yamlform_submission',
],
'#click_insert' => FALSE,
'#dialog' => TRUE,
];
}
else {
return [];
}
}
protected function setTokenData(array &$token_data, EntityInterface $entity) {
if ($entity instanceof YamlFormSubmissionInterface) {
$token_data['yamlform_submission'] = $entity;
$token_data['yamlform'] = $entity
->getYamlForm();
}
elseif ($entity instanceof YamlFormInterface) {
$token_data['yamlform'] = $entity;
}
}
}