{"id":55319,"date":"2022-07-20T08:50:45","date_gmt":"2022-07-20T03:20:45","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55319"},"modified":"2022-07-20T08:50:45","modified_gmt":"2022-07-20T03:20:45","slug":"how-to-give-twig-suggestion-for-custom-form-in-drupal-8-and-9","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-give-twig-suggestion-for-custom-form-in-drupal-8-and-9\/","title":{"rendered":"How to give twig suggestion for custom form in Drupal 8 and 9"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In this tutorial we will learn how to give twig suggestions to a custom form in drupal 8\/9<\/span><\/p>\n<p><span style=\"font-weight: 400;\">First, we will render the entire form into twig file then we will render one by one field in the twig file<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here we have a module ttn_blogs module.\u00a0<\/span><span style=\"font-weight: 400;\">Below are the steps to follow:<\/span><\/p>\n<h2><strong>Step 1\u00a0<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Create routing file in module: ttn_blogs.routing.yml\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ttn_blogs.add_article:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0path: &#8216;\/add\/article<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0defaults:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0_title: &#8216;Add Article<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0_form: &#8216;\\Drupal\\ttn_blogs\\Form\\AddArticleForm&#8217;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0requirements:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0_access: &#8216;TRUE&#8217;<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Step 2<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Create a form in module path \\Src\\Form\\AddArticleForm.php.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;?php<\/span><\/p>\n<p><span style=\"font-weight: 400;\">namespace Drupal\\ttn_blogs\\Form;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Drupal\\Core\\Form\\FormBase;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Drupal\\Core\\Form\\FormStateInterface;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Drupal\\Core\\Database\\Database;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Drupal\\Core\\Url;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Drupal\\Core\\Routing;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">* Provides the form for adding article from custom form.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class AddArticleForm extends FormBase {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0* {@inheritdoc}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0public function getFormId() {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return &#8216;ttn_blogs_add_article_form&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0* {@inheritdoc}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0public function buildForm(array $form, FormStateInterface $form_state) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 $form[&#8216;title&#8217;] = [<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#type&#8217; =&gt; &#8216;textfield&#8217;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#title&#8217; =&gt; $this-&gt;t(&#8216;Article Title&#8217;),<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#required&#8217; =&gt; TRUE,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#maxlength&#8217; =&gt; 20,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#default_value&#8217; =&gt; &#8221;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0];<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> $form[&#8216;body&#8217;] = [<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#type&#8217; =&gt; &#8216;textarea&#8217;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#title&#8217; =&gt; $this-&gt;t(&#8216;Body&#8217;),<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#required&#8217; =&gt; TRUE,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0&#8216;#rows&#8217; =&gt; 10,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#cols&#8217; =&gt; 60,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#resizable&#8217; =&gt; TRUE,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$form[&#8216;submit&#8217;] = [<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#type&#8217; =&gt; &#8216;submit&#8217;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;#value&#8217; =&gt; $this-&gt;t(&#8216;Save&#8217;) ,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$form[&#8216;#theme&#8217;] = &#8216;ttn_blogs_add_article_form&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return $form;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0* {@inheritdoc}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0public function validateForm(array &amp; $form, FormStateInterface $form_state) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\/\/ form validate logic here<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0* {@inheritdoc}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0public function submitForm(array &amp; $form, FormStateInterface $form_state) {\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\/\/ Add article logic here<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below is for passing the twig suggestion for our custom form:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$form[\u2018#theme\u2019] = <\/span><span style=\"font-weight: 400;\">&#8216;ttn_blogs_add_article_form&#8217;<\/span><span style=\"font-weight: 400;\">;<\/span><\/p>\n<h2><\/h2>\n<h2><strong>Step 3<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Create hook_theme function and provide your form template details in your\u00a0 module file or .theme file as below.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">* @file<\/span><\/p>\n<p><span style=\"font-weight: 400;\">* Implementing our hooks.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/**<\/span><\/p>\n<p><span style=\"font-weight: 400;\">* Implements hook_theme().<\/span><\/p>\n<p><span style=\"font-weight: 400;\">*\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">function ttn_blogs_theme($existing, $type, $theme, $path) {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0return [<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0&#8216;ttn_blogs_add_article_form&#8217; =&gt; [<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;render element&#8217; =&gt; &#8216;form&#8217;,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0],<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0];<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here <\/span><span style=\"font-weight: 400;\">&#8216;ttn_blogs_add_article_form&#8217;<\/span><span style=\"font-weight: 400;\"> is the form twig template.<\/span><\/p>\n<h2><\/h2>\n<h2><strong>Step 4<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Create a twig template as below in path \/templates\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here template name will be <\/span><span style=\"font-weight: 400;\">ttn-blogs-add-article-form.html.twig<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below code in twig prints entire form in above twig template.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;div class=&#8221;row custom-form&#8221;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div class=&#8221;col-md-6&#8243;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{{ form }}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now see form route, and you can see custom form classes in div while inspecting this page<\/span><\/p>\n<h2><\/h2>\n<h2><strong>Step 5<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Here we are printing each form field in twig template<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So in our <\/span><span style=\"font-weight: 400;\">ttn-blogs-add-article-form.html.twig<\/span><span style=\"font-weight: 400;\"> file,\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So finally code in twig template will be as below.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;div class=&#8221;row custom-form&#8221;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div class=&#8221;col-md-12&#8243;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0{{ form.title }}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 &lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0&lt;div class=&#8221;col-md-12&#8243;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0{{ form.body }}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;div class=&#8221;col-md-12&#8243;&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0{{form|without(\u2018title\u2019,\u2019body\u2019)}}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0&lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;\/div&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Now you can see the above form in your browser, which is printed by the above twig file.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this tutorial we will learn how to give twig suggestions to a custom form in drupal 8\/9 First, we will render the entire form into twig file then we will render one by one field in the twig file Here we have a module ttn_blogs module.\u00a0Below are the steps to follow: Step 1\u00a0 Create [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":46},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55319"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55319"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55319\/revisions"}],"predecessor-version":[{"id":55322,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55319\/revisions\/55322"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}