Bounty Ended with 50 reputation awarded by user2137454
5 deleted 433 characters in body
source | link

TheYou need "base hook" in there, I believe.

Here's an example:

/**
 * Implements hook_theme().
 */
function formcustomtemplate_theme() {
  return array(
    'form__my_form_test' => array(
      'render element' => 'form',
      'template' => 'form--myformtest-form',
      'base hook' => 'form',
    ),
  );
}

If you are using a custom theme, and you don't need the template in your module, the approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather
Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

Note: you'll need to copy the template into your theme folder.

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

Note: you'll need to copy the template into your theme folder.

You need "base hook" in there, I believe.

Here's an example:

/**
 * Implements hook_theme().
 */
function formcustomtemplate_theme() {
  return array(
    'form__my_form_test' => array(
      'render element' => 'form',
      'template' => 'form--myformtest-form',
      'base hook' => 'form',
    ),
  );
}

If you are using a custom theme, and you don't need the template in your module, the approach of creating a new theme hook for your custom form is unnecessary.
Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

4 added 66 characters in body
source | link

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then create the appopriate template in your theme.

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

Note: you'll need to copy the template into your theme folder.

To check where Drupal is looking for templates using the "form" hook, you can print out the theme registry from hook_theme_registry_alter:

 "form" => array:6 [▼
    "template" => "form"
    "path" => "themes/custom/mycustomtheme/templates/form"
    "type" => "theme_engine"
    "theme path" => "themes/custom/mycustomtheme”
    "render element" => "element"
    "preprocess functions" => array:4 [ …4]

This lets you know that you need to put the template in that directory.

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then create the appopriate template in your theme.

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

Note: you'll need to copy the template into your theme folder.

To check where Drupal is looking for templates using the "form" hook, you can print out the theme registry from hook_theme_registry_alter:

 "form" => array:6 [▼
    "template" => "form"
    "path" => "themes/custom/mycustomtheme/templates/form"
    "type" => "theme_engine"
    "theme path" => "themes/custom/mycustomtheme”
    "render element" => "element"
    "preprocess functions" => array:4 [ …4]

This lets you know that you need to put the template in that directory.

3 added 169 characters in body
source | link

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

The approach of creating a new theme hook for your custom form is unnecessary and I think that may be the issue. Anything such as preprocess_form would not work correctly if you use a different theme hook.

Rather than create a new theme hook, use hook_theme_suggestions_alter in your theme:

/**
 * Implements hook_theme_suggestions_alter().
 */
function mytheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  if ($hook == 'form' & !empty($variables['element']['#theme'])) {
    $suggestions[] = 'form__' . $variables['element']['#theme'][0];
  }

}

then to see the theme suggestions output to the page, turn on twig debugging:

https://drupalize.me/blog/201405/lets-debug-twig-drupal-8

You'll see template suggestions in the markup:

<!-- FILE NAME SUGGESTIONS:
   * form--myformtest-form.html.twig
   x form.html.twig
-->

You can see that it's not yet picking up your template, so create a template file by copying the form.html.twig and renaming it.

And I think the form attributes and children are important so you'd want to keep those in there, like this:

<form{{ attributes }}>
  {{ children }}
</form>

If you want to use the {{ element }} object to theme around the part of the form in twig, make sure that you have the required children:

 <form{{ attributes }}>
   {{ element.textfieldtest }}  
   {{ element.form_build_id }} {# required #}
   {{ element.form_id }} {# required #}
   {{ element.submit }}
 </form>

Also, from my custom controller, I'm calling the form this way:

$build['myform'] = \Drupal::formBuilder()->getForm('Drupal\mycustommodule\Form\MyformtestForm');
2 deleted 1 character in body
source | link
1
source | link