Drupal 8 inline_template in a form example

in your module file

<?php
/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'application_dialog_box' => [
      'variables' => [
        'diolog_id' => 'na',
        'diolog_title' => 'na',
        'diolog_text' => 'na',
       'someother_thing' => [],
      ],
      'template' => 'application-dialog-box',
    ],
  ];
}

?>

 

in your form or alter

<?php
 $form['some_diolog'] = [
      '#type' => 'inline_template',
      '#theme' => 'application_dialog_box',
      '#diolog_title' => 'this is a title',
      '#diolog_id' => 'the_id',
      '#diolog_text' => 'some text',
      '#someother_thing' => ['key' => 'val'],
    ];

in your module /templates twig file mine is (application-dialog-box.html.twig)

{#
/**
 * @file application-dialog-box.twig
*/
#}

{{ diolog_id }}
<br/>

{{ diolog_title }}
<br/>

{{ diolog_text }}


 

 

Category: