1

I have an eform printing out some fields. I want to create a module to change layout of the eform using hook_theme(). So, I have hook_theme() in my mymodule.module file and mymodule-form.html.twig file in template folder. Since I'm testing it, I only want to print out one field. But it prints out all of the field which seems like hook_theme() or twig is not working. Here's my code.

mymodule.module

<?php

/**
  * Implements hook_theme()
  */
function gq_theme($existing, $type, $theme, $path) {
 return array(
    'gq_form' => array(
      'variables' => array(
        'first_name_field' => $form['field_first_name'],
      ),
    ),
  );
}

/**
 * Prepares variables for the gq form template.
 *
 * Default template: gq-form.html.twig.
 */
function template_preprocess_gq_form(&$variables) {
  $variables['first_name_field'] = $form['field_first_name'];
}

mymodule.html.twig

{{ first_name_field }}

Also, I only have two files, mymodule.module, mymodule.info,yml, templates/mymodule-form.html.twig in module directory. Are there some necessary files?

  • Have you done anything with template suggestions or similar? I think you're missing the part that will actually tell your form to use the template you've provided, so your template will never be used if you're just adding this code and nothing more. – Cottser Nov 6 '15 at 2:47
  • Have you found the issue? – Rafee Sep 28 at 7:12
-1

Assuming that your template file is in a subfolder of your module folder (it looks like it's in templates), then your hook_theme needs to define the path. So it should look some thing like:

function gq_theme($existing, $type, $theme, $path) {
  return array(
    'gq_form' => array(
      'path' => $module_path . '/templates',
      'variables' => array(
        'first_name_field' => $form['field_first_name'],
      ),
    ),
  );
}

where $module_path is the path to your module.

  • The templates folder is required in D8, so this is not necessary. – Cottser Nov 6 '15 at 2:46
  • Yeah, in D8, path of module is automatically set to module_path/templates/, so there might be other problem. – PHG Nov 6 '15 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.