{"id":58100,"date":"2023-08-31T12:52:30","date_gmt":"2023-08-31T07:22:30","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58100"},"modified":"2023-08-31T12:52:30","modified_gmt":"2023-08-31T07:22:30","slug":"how-to-use-dependent-fields-in-drupal-9-node-entity","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-use-dependent-fields-in-drupal-9-node-entity\/","title":{"rendered":"How to use Dependent Fields in Drupal 9 Node Entity"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>When working with complex forms, you often have fields whose state depends on the input values of other fields in the form. For example, you might have a select list of options, the options for which depend upon the value selected for another select list.<\/p>\n<p>In this blog, I am representing one case study of my Drupal project where I have to achieve multi-dependent fields with the help of the Dependent Fields module. So, the fields must use the widget &#8220;Select list.&#8221; The child field type must be &#8220;Entity reference.&#8221; A view with the display mode &#8220;Entity Reference&#8221; must exist and is configured to receive as the first argument the value of the parent field.<\/p>\n<h2><strong>Initial Requirements<\/strong><\/h2>\n<p>We have to install <a href=\"https:\/\/www.drupal.org\/project\/dependent_fields\" target=\"_blank\" rel=\"noopener\">Dependent Fields<\/a> module, which is a contributed module and helps us to provide options while creating a field on any entity. For add field configuration such as &#8220;Make field dependent using views&#8221; as the &#8220;Reference method&#8221; and provide parent field. You can use any entity as the parent-child relationship.<\/p>\n<p>For this example, I have created and populated two Content Types entities: Country and City &amp; one Taxonomy vocabulary: State. For the State vocabulary, I&#8217;ve created an entity reference field to link it to the correct country. For the City entity, I&#8217;ve created an entity reference field to link it to the correct state.<\/p>\n<p>Steps to follow<\/p>\n<p>1. Create the parent field by clicking on Manage \/ Content types \/ Article \/ Manage fields \/ Add field.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58086\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-1024x433.png\" alt=\"\" width=\"625\" height=\"264\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-1024x433.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-768x325.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-1536x650.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1-624x264.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF1-1.png 1739w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>2. Configure the parent field to reference the Country Entity.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58087\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-1024x780.png\" alt=\"\" width=\"625\" height=\"476\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-1024x780.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-300x229.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-768x585.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-1536x1170.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1-624x475.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/second-1.png 1801w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>3. Save the field<\/p>\n<p>4. Create a view returning the child content filtered by the parent value (Manage \/ Structure \/ Views \/ Add view).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58088\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-1024x611.png\" alt=\"\" width=\"625\" height=\"373\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-1024x611.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-300x179.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-768x458.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-1536x917.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1-624x372.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF3-1.png 1771w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>5. Creating an &#8220;Entity Reference&#8221; view for State Listing and adding a contextual filter to point to receive the parent value.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58089\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-1024x680.png\" alt=\"\" width=\"625\" height=\"415\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-1024x680.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-300x199.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-768x510.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-1536x1020.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4-624x415.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF4.png 1811w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>6. Click on the view format settings to configure the search option (required for entity reference display), check the appropriate search field, and also change &#8220;Pager&#8221; settings to set unlimited items; otherwise, your select list would have only 10 items as default settings.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58090\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-1024x520.png\" alt=\"\" width=\"625\" height=\"317\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-1024x520.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-300x152.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-768x390.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-1536x779.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5-624x317.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF5.png 1805w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>7. Save the view.<\/p>\n<p>8. Go back to the Article fields configuration to create one field for the State. Click on the &#8220;Add field&#8221; and create a new entity reference field.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58092\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6-1024x497.png\" alt=\"\" width=\"625\" height=\"303\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6-1024x497.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6-300x146.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6-768x372.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6-624x303.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF6.png 1534w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>9. At the field configuration, on the &#8220;Reference Type&#8221; box, select &#8220;Make field dependent using views&#8221; as the &#8220;Reference method.&#8221; On the field &#8220;View used to select the entities,&#8221; select the &#8220;State List&#8221; view we&#8217;ve just created. On the &#8220;Parent field,&#8221; select the country field.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58093\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-1022x1024.png\" alt=\"\" width=\"625\" height=\"626\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-1022x1024.png 1022w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-300x300.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-150x150.png 150w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-768x769.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-1533x1536.png 1533w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-624x625.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-120x120.png 120w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-48x48.png 48w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7-96x96.png 96w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF7.png 1771w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>10. Save the field and settings.<\/p>\n<p>11. Similarly, we will create an &#8220;Entity Reference View&#8221; for populating Cities with a State &#8220;Contextual Filter&#8221; and save the view.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58094\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-1024x683.png\" alt=\"\" width=\"625\" height=\"417\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-1024x683.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-300x200.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-768x512.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-1536x1025.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8-624x416.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF8.png 1809w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58095\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-1024x385.png\" alt=\"\" width=\"625\" height=\"235\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-1024x385.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-300x113.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-768x289.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-1536x578.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9-624x235.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF9.png 1788w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58096\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-1024x545.png\" alt=\"\" width=\"625\" height=\"333\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-1024x545.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-300x160.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-768x409.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-1536x818.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10-624x332.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF10.png 1674w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>12. Go back to the Article fields configuration to create one field for the City. Click on the Add field and create a new entity reference field. At the field configuration, on the &#8220;Reference Type&#8221; box, select &#8220;Make field dependent using views&#8221; as the &#8220;Reference method.&#8221; On the field &#8220;View used to select the entities,&#8221; select the &#8220;City List&#8221; view we&#8217;ve just created. On the &#8220;Parent field,&#8221; select the state field and save the field.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58097\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-1024x958.png\" alt=\"\" width=\"625\" height=\"585\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-1024x958.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-300x281.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-768x718.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-1536x1436.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11-624x584.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF11.png 1789w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>13. Click on the &#8220;Manage form display tab&#8221; and select the &#8220;Select list&#8221; widget for the Country, State, and City fields. Save this setting.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-58098\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12.png\" alt=\"\" width=\"1814\" height=\"271\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12.png 1814w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12-300x45.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12-1024x153.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12-768x115.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12-1536x229.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF12-624x93.png 624w\" sizes=\"(max-width: 1814px) 100vw, 1814px\" \/><\/p>\n<p>Now, when you create a new article, the State field is driven by the Country field, and the City field is driven by the State field.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-large wp-image-58099\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13-1024x691.png\" alt=\"\" width=\"625\" height=\"422\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13-1024x691.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13-300x202.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13-768x518.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13-624x421.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/DF13.png 1327w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Drupal provides the best way by installing a single contributed module, and with the help of a few configurations, we can achieve complex forms and field dependency states with other field values easily.<\/p>\n<p>Let us know in case of any queries, and please feel free to reach out via comments.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction When working with complex forms, you often have fields whose state depends on the input values of other fields in the form. For example, you might have a select list of options, the options for which depend upon the value selected for another select list. In this blog, I am representing one case study [&hellip;]<\/p>\n","protected":false},"author":1511,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":339},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58100"}],"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\/1511"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58100"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58100\/revisions"}],"predecessor-version":[{"id":58219,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58100\/revisions\/58219"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}