You are here

class YamlFormTokenManager in YAML Form 8

Defines a class to manage token replacement.

Hierarchy

Expanded class hierarchy of YamlFormTokenManager

1 string reference to 'YamlFormTokenManager'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormTokenManager
yamlform.token_manager in ./yamlform.services.yml
Drupal\yamlform\YamlFormTokenManager

File

src/YamlFormTokenManager.php, line 12

Namespace

Drupal\yamlform
View source
class YamlFormTokenManager implements YamlFormTokenManagerInterface {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * Constructs a YamlFormTokenManager object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   */
  public function __construct(ModuleHandlerInterface $module_handler, Token $token) {
    $this->moduleHandler = $module_handler;
    $this->token = $token;
  }

  /**
   * {@inheritdoc}
   */
  public function replace($text, EntityInterface $entity = NULL, array $data = [], array $options = []) {

    // Replace tokens within an array.
    if (is_array($text)) {
      foreach ($text as $key => $value) {
        $text[$key] = $this
          ->replace($value, $entity);
      }
      return $text;
    }

    // Most strings won't contain tokens so let's check and return ASAP.
    if (!is_string($text) || strpos($text, '[') === FALSE) {
      return $text;
    }

    // Replace @deprecated [yamlform-submission] with [yamlform_submission].
    $text = str_replace('[yamlform-submission:', '[yamlform_submission:', $text);

    // Set token data based on entity type.
    $this
      ->setTokenData($data, $entity);

    // Set token options.
    $options += [
      'clear' => TRUE,
    ];
    return $this->token
      ->replace($text, $data, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function buildTreeLink() {
    if ($this->moduleHandler
      ->moduleExists('token')) {

      // @todo Issue #2235581: Make Token Dialog support inserting in WYSIWYGs.
      return [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          'yamlform',
          'yamlform_submission',
        ],
        '#click_insert' => FALSE,
        '#dialog' => TRUE,
      ];
    }
    else {
      return [];
    }
  }

  /**
   * Get token data based on an entity's type.
   *
   * @param array $token_data
   *   An array of token data.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   A YAML Form or YAML Form submission entity.
   */
  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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlFormTokenManager::$moduleHandler protected property The module handler.
YamlFormTokenManager::$token protected property The token service.
YamlFormTokenManager::buildTreeLink public function Build token tree link if token.module is installed. Overrides YamlFormTokenManagerInterface::buildTreeLink
YamlFormTokenManager::replace public function Replace tokens in text. Overrides YamlFormTokenManagerInterface::replace
YamlFormTokenManager::setTokenData protected function Get token data based on an entity's type.
YamlFormTokenManager::__construct public function Constructs a YamlFormTokenManager object.