Drupal allows you to create custom content types and assign custom templates to render pages based on that content type. In this tutorial, we’ll show you how to create a custom templated page for a content type, specifically for the nodes with names “About,” “Services,” and “How-We-Work.”
We’ll break down the process into clear steps, from defining the content type to customizing the Twig templates for each specific node. This will give you full control over the appearance of these pages and ensure they are styled and structured as you require.
Step 1: Create a Custom Content Type
Before we create the custom templates, you need to create a custom content type, like “About,” “Services,” and “How-We-Work.” Here’s how you do it:
- Navigate to the Content Types Configuration:
- Go to Structure > Content types in the Drupal admin interface.
- Create a New Content Type:
- Click on Add content type and create a content type, for example,
Custom Page
. - Name it as needed, such as “About”, “Services”, or “How-We-Work”. Define the fields (e.g., body text, image, etc.) as per your requirements.
- Click on Add content type and create a content type, for example,
- Manage Content:
- After creating the content type, go to Content > Add content to create the nodes for “About,” “Services,” and “How-We-Work.”
Step 2: Create Custom Templates for Content Types
In Drupal, you can define templates for specific content types by naming the templates accordingly. We’ll create a template for the Custom Page
content type that will apply to specific nodes based on their title.
- Create the
page--[content-type].html.twig
Template:To create a custom template for the content type, create a new file calledpage--custom-page.html.twig
. This template will apply to any node of typeCustom Page
(which you can rename to fit your needs, such asabout
,services
, orhow-we-work
).Example: - Add the Basic Structure:Here’s an example of a template that will structure the content and display the page with a custom header and footer:
Step 3: Create Specific Templates for Each Node
While you can use a general content type template like page--custom-page.html.twig
, if you want to create specific templates for nodes like “About,” “Services,” and “How-We-Work,” you can create a template for each node individually based on the node’s title or path alias.
- Create Template for “About” Page:Create a template for the “About” page:
- Add the Custom Structure for the “About” Page:Inside the
page--about.html.twig
, you can structure the page as you like. For example: - Create Template for “Services” Page:Similarly, create a template for the “Services” page:
- Add the Custom Structure for the “Services” Page:Example template for the Services page:
- Create Template for “How-We-Work” Page:Finally, create a template for the “How-We-Work” page:
- Add the Custom Structure for the “How-We-Work” Page:Example template for the How-We-Work page:
Step 4: Clear Cache to Apply Changes
After creating your custom templates, you will need to clear the cache to ensure the templates are applied to your content. You can do this via the Drupal admin interface or using Drush (if you have it installed).
- Drupal Admin Interface:
- Go to Configuration > Development > Performance.
- Click Clear all caches.
- Drush:
Step 5: Assign Blocks and Add Content
Now that the templates are in place:
- Add Blocks:
- If you want to display additional content in your header or footer, go to Structure > Block Layout and add blocks to the
header
orfooter
regions.
- If you want to display additional content in your header or footer, go to Structure > Block Layout and add blocks to the
- Create Content:
- Go to Content > Add content, select the content type (e.g.,
About
,Services
, orHow-We-Work
), and create your pages.
- Go to Content > Add content, select the content type (e.g.,
And Finally in your acesoftech.theme you need to ad this code
function acesoftech_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::routeMatch()->getParameter(‘node’)) {
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->getType() == ‘page’) {
if ($node->getTitle() == ‘About’) {
$suggestions[] = ‘page__about’;
}
}
}
}
}
Conclusion
By following these steps, you’ve learned how to create custom-templated content type pages for nodes like “About,” “Services,” and “How-We-Work” in Drupal 10. You’ve also seen how to leverage Drupal’s Twig templating system to customize each page individually, based on its node title or alias. This allows for maximum flexibility in designing your site, while also keeping the content modular and easy to manage.
Now you can apply different templates and layouts to each page, ensuring your site has the custom look and feel it needs. If you need to make further adjustments, simply edit the relevant Twig templates or adjust the content in the Drupal admin interface.