Just when you thought "I got this!", we are now going to build a new component that uses slightly different approach. For the most part, the majority of the components you've built follow the same principles you've been following all along, however, there are other type of components that may deviate from those principles a little. When content needs to be displayed in some kind of list or collection of components, there are a few things we need to do differently.
Let's start with our usual process of creating some files and adding the respective code to each file. We will explain everything once these steps are done.
Inside nitflex_dev_theme/src/_patterns/01-patterns/ create a new directory called movie-list
Inside the movie-list directory create these files: movie-list.yml, movie-list.scss, and movie-list.twig
Inside movie-list.yml add the following code:
list_title:title:"Action and Adventure"heading_level:3items: -average_rating:5cover_image:"<img src=\"/sites/default/files/action-3.jpg\" alt=\"Alt text\" />"heading:title:"DrupalCon Seattle 2019"url:"#"heading_level:4modifier:"movie-card__heading"mpaa_rating:"PG-13,"promo_text:"Be part of the 12th season this fall,"synopsis:"Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit." -average_rating:5cover_image:"<img src=\"/sites/default/files/action-3.jpg\" alt=\"Alt text\" />"heading:title:"DrupalCon Seattle 2019"url:"#"heading_level:4modifier:"movie-card__heading"mpaa_rating:"PG-13,"promo_text:"Be part of the 12th season this fall,"synopsis:"Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit." -average_rating:5cover_image:"<img src=\"/sites/default/files/action-3.jpg\" alt=\"Alt text\" />"heading:title:"DrupalCon Seattle 2019"url:"#"heading_level:4modifier:"movie-card__heading"mpaa_rating:"PG-13,"promo_text:"Be part of the 12th season this fall,"synopsis:"Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit." -average_rating:5cover_image:"<img src=\"/sites/default/files/action-3.jpg\" alt=\"Alt text\" />"heading:title:"DrupalCon Seattle 2019"url:"#"heading_level:4modifier:"movie-card__heading"mpaa_rating:"PG-13,"promo_text:"Be part of the 12th season this fall,"synopsis:"Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit." -average_rating:5cover_image:"<img src=\"/sites/default/files/action-3.jpg\" alt=\"Alt text\" />"heading:title:"DrupalCon Seattle 2019"url:"#"heading_level:4modifier:"movie-card__heading"mpaa_rating:"PG-13,"promo_text:"Be part of the 12th season this fall,"synopsis:"Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit."
Same as in the Card Collection component, we created an items[ ] array. The array contains multiple instances of a movie card which we will use to render a collection of movie cards.
Add the following code snippet into movie-list.twig:
First, notice we are using the very helpful include statement to nest the heading component in order to print a title for each collection.
Then we have declared {% block list %} so we are able to alter how content is rendered when we integrate this component with Drupal.
Inside the twig block, we are simply including the card-collection component as is, without having to map variables. This is because we provided a list of movie cards for it to use in our movie-list.yml file.
Twig blocks
Twig blocks, not the same as Drupal blocks, are a great way to extend twig templates. It can be confusing at first given that all of our lives as Drupal developers we have worked with blocks. However, think of Twig blocks are regions in which you can insert any kind of content.
Now add the component's styles below into movie-list.scss:
Now that we have written all the necessary code to build the Movie List component, it's time to see the component in the style-guide. Let's compile our project first.
In your terminal or command line, navigate to /themes/custom/nitflex_dev_theme and run the following command: