Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Building the Contentful Page

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We now have full control over how the Contentful pages render. That's thanks to the "Individual Skill layout" that we mapped to all Contentful "Skill" pages.

But... all we have is this manual h1 title. How can we render the real data for whichever Contentful Skill we're viewing?

First, on Contentful's site, if I navigate to "Content model" and click on "Skill", you can see that every Skill has 5 fields... and each field has an internal name. It's... almost easier to see this via the JSON preview. Here we go. So there's a "Title" field, it's internal name is title, "Short Description", "Technique", and a few other like "Image" and "Advertisement". Advertisement is actually a link to that other type of content.

Using the "Contentful Entry Field" Block Type

Anyways, what we really want to do up here is print the skill's title in the h1. Fortunately, that is possible, thanks to a new block type that the Contentful bundle added. It's here at the bottom: "Contentful entry field".

This allows us to render a single field from whatever Contentful entry is currently being rendered. Let's try it! Then delete the old h1.

The new block has one super important option: field identifier. Set that to the internal name of the field: title. And make this an h1. As usual, the block label is optional, but I'll include it.

Cool! Hit publish and continue editing, move over and... yes! It's dynamic. If we go to the URL for some other skill, like /basic-chop, that works too!

Adding the Fancy Hero Area

So let's get fancier. Add a column... and move this title inside. Can you guess what I'm about to do? Give the column that same hero-wrapper class that we used earlier. And you know what else? Each skill has a "Short description". Lets add another entry field block right below.

Notice that one option for this block is "view type". We're going to talk more about that soon, but this should match the "type" of the content that you're pulling from Contentful. So far, both title and this shortDescription are "string" types. Leave this as div.

Testing timer! Hit "Publish and continue editing". And... let's see how it looks. I love that! Let's add more!

Adding a Contentful Image

Every skill has an image. Inside of that same hero column, add another Contentful entry block at the bottom. This will be called image... and the type is "referenced assets". You do need to set a width and height. Let's do 200 by 200. Publish that... refresh and... we're on a roll!

One last thing: rendering the skill content below everything. By the way, we could render this in the same zone... or use the zone below. Zones don't matter much in most case.

Using a 2-Column Block

But let's make this spot more interesting. I want to render the skill content on the left and an advertisement on the right. To do that, for the first time, use a 2-column block. Set this to 66, 33 so that the left side takes up most of the space. Add a title to the left side and make it an h3 with the text "The Technique:". Below, drag over a contentful entry field.

This one... if I go check my fields... is called technique and it holds rich text. If you modified it in Contentful, you'd see a rich text editor... and the final value is HTML. So, type technique, keep it as a div and select Richtext.

Rendering a Related Contentful Entry

Finally, on the right side, add one more Contentful entry field. Look back at the content model for Skills... and scroll down a bit. The one we want to use is called advertisement, and this is a "Referenced entry" type. Yup, if you edited a skill, you would choose the Advertisement from the list of Advertisements we have in Contentful. It's like a database relation.

Anyways, enter advertisement, hit "Publish and continue editing"... refresh and... ok! Sort of awesome. We need a container to bring those in. We already have a column, so click "Wrap in container".

And... yea... though this could also use some top margin. On that same column, add a class: my-3. Publish this... and reload. So much better! Though, the Advertisement is just printing a URL... not rendering an ad. That's because Contentful doesn't know how to render the "Advertisement" content type. We'll help it soon.

But first, let's fix our Skill pages by prefixing all URLs with /skills.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1.0",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^3.7", // v3.7.0
        "doctrine/doctrine-bundle": "^2.7", // 2.7.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.13", // 2.13.3
        "easycorp/easyadmin-bundle": "^4.4", // v4.4.1
        "netgen/layouts-contentful": "^1.3", // 1.3.2
        "netgen/layouts-standard": "^1.3", // 1.3.1
        "pagerfanta/doctrine-orm-adapter": "^3.6",
        "sensio/framework-extra-bundle": "^6.2", // v6.2.8
        "stof/doctrine-extensions-bundle": "^1.7", // v1.7.0
        "symfony/console": "5.4.*", // v5.4.14
        "symfony/dotenv": "5.4.*", // v5.4.5
        "symfony/flex": "^1.17|^2", // v2.2.3
        "symfony/framework-bundle": "5.4.*", // v5.4.14
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/proxy-manager-bridge": "5.4.*", // v5.4.6
        "symfony/runtime": "5.4.*", // v5.4.11
        "symfony/security-bundle": "5.4.*", // v5.4.11
        "symfony/twig-bundle": "5.4.*", // v5.4.8
        "symfony/ux-live-component": "^2.x-dev", // 2.x-dev
        "symfony/ux-twig-component": "^2.x-dev", // 2.x-dev
        "symfony/validator": "5.4.*", // v5.4.14
        "symfony/webpack-encore-bundle": "^1.15", // v1.16.0
        "symfony/yaml": "5.4.*", // v5.4.14
        "twig/extra-bundle": "^2.12|^3.0", // v3.4.0
        "twig/twig": "^2.12|^3.0" // v3.4.3
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
        "symfony/debug-bundle": "5.4.*", // v5.4.11
        "symfony/maker-bundle": "^1.47", // v1.47.0
        "symfony/stopwatch": "5.4.*", // v5.4.13
        "symfony/web-profiler-bundle": "5.4.*", // v5.4.14
        "zenstruck/foundry": "^1.22" // v1.22.1
    }
}
userVoice