In a Drupal 8 website I created a custom token that is working in basic page.
Then I created a custom block with a WYSIWYG field :
class Myblock extends BlockBase {
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public function getMachineName() {
return 'my_custom_blcok';
}
public function label() {
return $this->t('Block label');
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
$form['my_field'] = [
'#type' => 'text_format',
'#format' => 'content_manager',
'#title' => $this->t('Field name'),
'#default_value' => isset($config['my_field']['value']) ? $config['my_field']['value'] : '',
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->setConfigurationValue('my_field', $form_state->getValue('my_field'));
}
public function build() {
$config = $this->getConfiguration();
$content = $config['my_field']['value'];
return [
'#theme' => 'simulator_block',
'#content' => $content,
];
}
}
If I type My content [module:my_token]
in the WYSIWYG.
The following content is display :
<p>My content [module:my_token]</p>
How can I replace the token ?