0

We have a twig template, node--page.html.twig where we want to display the custom variable ns-custom-template. The variable is created from a custom module, mymodule with the following code below.

How can I create a variable, populate it using the ns-custom-template variable, and display it in the node--page.html.twig template?

Here is what I tried, but when using kint(test_var) I get NULL returned.

mymodule.module

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'ns_custom_template' => [
      'variables' => [
        'test_var' => NULL,
      ],
    ],
  ];
}

Inside mymodule's template folder is ns-custom-template.html.twig

<p> Testing my variable {{ test_var }} </p>

MyModuleController class

class MyModuleController extends ControllerBase {
  public function content() {
    $config = $this->config('mymodule.settings');
    $config_value = $config->get('my_config_value');
    return [
      '#theme' => 'ns_custom_template',
      '#test_var' => $config_value,
    ]
  }
}

mymodule's routing file:

mymodule.module_controller_content:
  path: '/mymodule/content'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MyModuleController:content'
    _title: 'content'
  requirements:
    _permission: 'access content'

MyModuleForm

class MyModuleForm extends ConfigFormBase {
  protected function getEditableConfigName() {
    return [
      'mymodule.setting',
    ],
  }

  public function getFormId() {
    return 'mymodule_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('mymodule.setting');
    $form['my_config_value'] = [
      '#type' => 'textarea',
      '#title' => $this->t('My config value'),
      '#description' => $this->t('Enter some value'),
      '#default_value' => $config->get('my_config_value'),
    ];
    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    $this->config('mymodule.setting')
      ->set('my_config_value', $form_state->getValue('my_config_value'))
      ->save();
  }
}

node--page.html.twig

<article>
  {{ ns_custom_template }} {# returns null #}
  {{ kint(ns_custom_template }} {# returns null #}
</article>

For reference, I am trying to emulate the steps on Define a custom template for module output page.

3
  • you code almost there, i test it without the form, it works, Just rename your template from template to templates i think. and try to dump the value of $config_value inside the content method.
    – berramou
    Dec 28, 2018 at 22:44
  • @berramou are you sure it is working? i removed the form and '#test_var' => $this->t('some value'), and did {{ kint(test_var) }} but it still returned null. Any other steps I missed? Dec 28, 2018 at 23:34
  • yeah it works for me, don't forget to flush all caches
    – berramou
    Dec 29, 2018 at 22:26

2 Answers 2

2

The variable you are looking for is not ns_custom_template but test_var

To get all variables in your twig file, you can try {{ kint() }}

To get the variable you have previously declared in mymodule_theme, you can try {{ kint(test_var) }}

BUT there is another mistake:

The twig file in which you will find test_var is not node--page.html.twig but only ns-custom-template.html.twig

if you want to have test_var inside node--page.html.twig, you can use a preprocess function : function template_preprocess_node with a code like:

$var['test_var']='my value';

EDIT:It seems that you would like to use the template ns_custom_template in a node template: to achieve this, you need to populate your variable test_var with a render array having the name defined in mymodule_theme as a value of the #theme key.

function mymodule_preprocess_node(&$var) {
  $var['my_free_var_name'] = ['#theme' => 'ns_custom_template', '#test_var' => 'your value'];
}

After doing this, you can display your template inside node--page.html.twig bu using the following code:

{{ my_free_var_name }}
5
  • are you suggesting that in the mymodule_preprocess_node hook, I just get the config_object, use $var['myvariable'] = $config_value, and in the node--page.html.twig file I print it using {{ myvariable' }}? That sounds different then what I am trying to accomplish. I would like to create a variable that i can theme using ns-custom-template.html.twig, and then pass that onto the node--page.html.twig using mymodule. Is your suggestion the only way to do this? Dec 28, 2018 at 23:21
  • @usernameabc, in node preprocess: $var['test_var'] = ['#theme' => 'ns_custom_template', '#test_var' => $config_value];
    – 4uk4
    Dec 29, 2018 at 9:52
  • I have updated my answer
    – Baud
    Dec 29, 2018 at 10:15
  • @Baud in doing this, I have found that the MyModuleController class is not used. Is that correct to expect the controller not to be needed anymore given that I would be using hook_preprocess_node()? Dec 31, 2018 at 17:13
  • If you want to display a variable inside a node template, you do not need to create a route or a controller as everything is already setup by the node module. But if you would like to create a config form that will save some settings (that could be used in the node template), you will need to create a new route to this form (no need to create a controller if you use the _form key... but this is another topic)
    – Baud
    Jan 1, 2019 at 1:27
0

if you want to display twig template inside another, for instance ns-custom-template.html.twig inside node--page.html.twig you should use include instead of {{ some-twig-name }}:
In your case node--page.html.twig will be something like:

<article>
  {% include  '/modules/custom/mymodule/templates/ns-custom-template.html.twig' %}
</article>

I assume that your module reside in /modules/custom folder.

4
  • for some reason this does not return the field I was expecting when i do {{ ksm() }} Dec 31, 2018 at 17:14
  • Where you put {{ ksm() }} ??
    – berramou
    Dec 31, 2018 at 18:15
  • I tried in both ns-custom-template.html.twig and node--page.html.twig Dec 31, 2018 at 18:35
  • if you do something like {{ kint(test_var) }} inside ns-custom-template.html.twig you should get the value of the var, did you flush the cache?
    – berramou
    Dec 31, 2018 at 18:39

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.