Step 7 — Embed content¶
Creating lists and detailed views of content types and their respective items often involves loading related resources. In this step, you add a related object, a Landmark, which will be displayed on Ride pages.
You can add as many or as little related resources as you like.
Add the Landmark content type¶
Now you need to add the second content type needed in the site, Landmark.
Go to Content types, and in the Content group, add the Landmark content type.
A Landmark is an interesting place that Rides go through. Each Ride may be related to multiple Landmarks.
- Name: Landmark
- Identifier: landmark
Then add all Fields with the following information:
Field Type | Name | Identifier | Required | Searchable | Translatable |
---|---|---|---|---|---|
Text line | Name | name |
yes | yes | yes |
Rich text | Description | description |
no | yes | yes |
Image Asset | Photo | photo |
yes | no | no |
Map location | Location | location |
yes | yes | no |
Confirm the creation of the content type by selecting Create.
Create a Landmarks Folder and add some Landmarks to it. Note that you will need pictures (for the Photo Field) to represent them.
Add Landmarks to Ride content type definition¶
Now edit the Ride content type in order to add a Multiple Content Relation between the two content types.
Create a new Content relations (multiple) Field called "Landmarks" with identifier landmarks
and allow content type "Landmark" to be added to it:
Confirm by clicking Save and close.
Go back to one of your existing Rides, edit it and link some Landmarks to it. Click Publish.
Display a list of Landmarks in Ride view¶
Create Landmark line view¶
Now you need to create the line view for Landmarks.
Declare a new override rule in config/packages/views.yaml
:
1 2 3 4 5 6 7 8 9 10 |
|
Add the template for the line view of a Landmark by creating templates/line/landmark.html.twig
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Like before, you use an image variation here (line 4) and you need to configure it.
Add the following section to config/packages/image_variations.yaml
, at the same level as ride_list
:
1 2 3 4 |
|
Create the RideController¶
You must provide additional information when the Ride object is displayed.
This requires creating a custom controller.
The controller uses ContentService
to load related resources (Landmarks) for a particular Ride.
Create a src/Controller/RideController.php
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Update config/packages/views.yaml
to mention the RideController.php
by adding a line with the controller
key to the view config:
1 2 3 4 5 6 7 8 9 10 |
|
Add the Landmark in the Ride full view¶
Now modify the Ride full view template to include a list of Landmarks, and the controller that you just created.
Add the following lines at the end of templates/full/ride.html.twig
, before the last </div>
and the closing tag {% endblock %}
:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
You can now check the Ride page again to see all the connected Landmarks.
Tip
You can use dump()
in Twig templates to display all available variables.