1

I have developed a custom content type with some custom fields which works nice, I have also assigned a template to it.The Problem is I cannot access the fields against the post in my template. let me show you my code

[module].module

 <?php
    function [module]_theme($existing, $type, $theme, $path)
    {
        return [
            'node__[module] => [
                'variables' => [],
            ]
        ];
    }

templates/node--[module].html

<h1>Uneeb</h1>

below is the yml file of field i created and want to access in twig template

[module]/config/install/field.field.node.[module].location.yml

# field.field.node.cspace.location.yml
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.location
    - node.type.cspace
  module:
    - text
id: node.cspace.location
field_name: location
entity_type: node
bundle: cspace
label: 'Script Location'
description: 'More specific information about the car brand'
required: true
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  display_summary: false
field_type: text_with_summary

the node template works perfectly fine I can the text uneeb on the page and only for this specific content type which i originally wanted now I want to access data against this content type.I have tried a bunch of solutions but none of them seems to be working and I cannot access my custom content fields inside twig template can anyone help me out?

1 Answer 1

1

First you need to add base hook to your theme declaration:

    function [module]_theme($existing, $type, $theme, $path)
    {
        return [
            'node__[module]' => [
                'variables' => [],
            ],
            'base hook' => 'node'
        ];
    }

Then in your node--[module].html.twig, you can print content field like this:

{{ content.field_xyz[0] }}

with field_xyz is machine name of the field.

Sign up to request clarification or add additional context in comments.

8 Comments

Kien Thanks for your comment, how can i find out the machine name of the field? i'm a newbie to drupal my apologies for any foolish questions,furthermore the i tried printing out {{ content }} it returns null
I have updated the question and added the yml file of content field in case i did something wrong over there
Your field's machine name is location. And you should delete the code in file [module].module so as not to overwrite the default template in core. Remember to flush the cache
if i comment out the code in [module].module file it ignores the template file and renders the default view,so i had to add that code back , but still {{content.location[0]}} doesnot work i have seen this solution in many places but doesnot work in my case any wild guess what might the problem?
can it be that maybe twig requries a specific pattern and i am not following that i havent used twig before
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.