Movie Card Collection
If we look at the project's prototype, we are using a collection of movie cards to display movies in different categories (i.e. Action, Horror, Comedy, etc.).
There are two parts to creating the list. First we need to create a collection of movie cards where we simply grab a given number of cards and style them the same way shown in the prototype. Second, we create a list which would filter cards per movie type and include the type title on each collection. Although we could accomplish this with a single component, we are planning on building this list with Drupal Views and as you will see, it is best if we split this process in two parts.
Let's start with our usual process of creating some files and adding the respective code to each file.
Inside
nitflex_dev_theme/src/_patterns/01-patterns/
create a new directory called card-collectionInside the card-collection directory create these files:
card-collection.yml
,card-collection.scss
, andcard-collection.twig
Inside
card-collection.yml
copy the following code:
We created an array called items. The array contains multiple items each of which has the fields found in a movie card (heading, synopsis, cover image, etc.).
Inside
card-collection.twig
add the following code:
Important
Create the card-collection library.
We are now introducing the concept of **Twig Blocks (not the same as Drupal blocks), so we are able to alter how content is rendered when we integrate this component with Drupal.
Inside the block, we loop through the items array and for each item we loop through we include a movie-card component. This means we will end up with as many cards as items in the items array.
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 as regions in which you can insert any kind of content.
Now add the component's styles below into card-collection.scss:
Compiling the style-guide
Now that we have written all the necessary code to build the Movie Card Collection 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:
Viewing the component
Open your Drupal site and point to the URL below: http://nitflex.lndo.site/themes/custom/nitflex_dev_theme/dist/style-guide/?p=viewall-patterns-card-collection
Under the Components category you should see the new Movie Card Collection component.
Last updated