We received the UX from our design team, but they still haven’t decided on the details yet. However, the content from our travel packages is already defined and we do not want to hold our marketers back from writing their content.
Install the demo project
To jumpstart our project, we will install our demo project.
The easiest way to do this is to clone our git repository.
? What Magnolia would you like to install?
1) magnolia-empty-webapp
2) magnolia-community-webapp
3) magnolia-community-demo-webapp
4) magnolia-dx-core-webapp
5) magnolia-dx-core-demo-webapp
Answer: 2Copy
Once the installation is finished, you will see an output similar to this on your console:
info Magnolia has been successfully setup for light development!
info You can now open a new terminal tab or window and start it up with the CLI command'mgnl start'
info Magnolia will be ready after a few seconds at localhost:8080/magnoliaAuthor. Username and password is superuserCopy
Now, pick up some handy configuration and sample images.
We are going to build a Travel Website SPA promoting travel packages. Marketers can add new navigation items. They can create new pages and arrange the content freely with pre-defined content blocks.
What is a Headless CMS?
When talking about Headless CMS, we are typically talking about something we call "Form-Centric or API first Approach".
You want to create a technology and layout agnostic way for marketers to create content, and for developers to consume the content and use in any way they want, and whatever tools they want.
What will we do next?
Create a Content Type
Create a Magnolia app
Define a REST API endpoint
Defining a Content Type
Scenario
After talking to the design and marketing team, we know that our Travel Packages can have the following information: name - description - featured - image - tour types - location - date - duration - tour operator - body, some rich text to describe the travel package.
What we have just described is our Content Model. It’s the contract between developers and marketers to create their website.
With Magnolia CMS, we describe the model of content with something we call "Content Types". Implementing content types is very developer-friendly: just describe the type in a YAML file.
When creating YAML definition files, make sure the file extension is .yaml, not .yml.
In our hello-magnolia-headless folder, we will go into contentTypes directory:
cd ./basic-headless-demos/magnolia/light-modules/basic-tours-lm/contentTypesCopy
Create a file named tours.yaml with the following content (or just check that the file and the content is already there):
tours.yaml
datasource:workspace:toursautoCreate:truemodel:properties:name:label:Namerequired:truei18n:truedescription:label:Descriptioni18n:trueisFeatured:type:Boolean#Types 'Decimal', 'Long' and 'Double' are also available.label:Featurethisitemimage:type:assetlabel:ImagetourTypes:label:TourTypestype:reference:categorymultiple:truelocation:label:StartCityi18n:truedate:label:Datetype:Dateduration:type:Longlabel:TourDurationoptions:"2":value:2label:2days"7":value:7label:7days"14":value:14label:14days"21":value:21label:21daystourOperator:label:TourOperatori18n:truebody:type:richTextlabel:Bodyi18n:trueCopy
Magnolia will automatically pick up the new content type and will make it available on our running instance.
Create a file named tours.yaml with the following content (or just check that the file and the content is already there):
!content-type:toursname:tourslabel:Tours# Optionally override any of the app configuration supplied by the content type.subApps:detail:label:TourDetailform:properties:isFeatured:buttonLabel:FeaturedCopy
To see your new Tours app, log out and log back in, and press the grid icon.
Installing content
We have prepared Demo Content to start with. To install the Demo Content, follow these steps:
Open the Tours app.
Click the Import action. (You might need to scroll down in the Action bar.)
Select the file ./magnolia/_dev/content-to-import/tours.magnolia-travels.xml.
Browsing the content
These steps were already enough to browse the content and to use the Form Editor.
What did we just do?
We modeled our content and created a Content Type.
We created the Tours App.
We installed some Demo Content.
We browsed through the Tours app and viewed the Demo Content.
Defining a REST API Endpoint
Defining REST API endpoints is as easy as creating the content type and app.
Change to the basic-tours-lm/restEndpoints/delivery directory.
cd ../restEndpoints/deliveryCopy
Create a file named (you guessed it) tours.yaml with the following content:
The frontend needs to be run on a web server. In your terminal, go to /basic-headless-demos/frontend and start a web server, then access your page in the browser.
For example, use the http-server npm package:
npm install http-server -g
http-serverCopy
Just opening the index.html file in a web browser, for example by double-clicking on it, will result in a CORS error.
Permissions for images
What? No images displaying? We need to allow anonymous access to the image URLs since we are running on the author instance.
Open the Security app, open the Roles tab, and edit the anonymous role.
Go to the Web access tab, click Add new and enter the path /.imaging* set to GET.
Now refresh your client, et voila - images!
Congratulations
In the meantime, we have the design. Our marketers want to enhance the travel portfolio page. They want to be able to add promotional teaser on top of the page and a custom sidebar too. They want to be able to define freely what to add there and add new article pages. They want to have full control of the navigation.