0

So I have a custom content entity that has the typical attributes. Name, Author, Creation Date, ect. In the 'view' tab of this content entity, it displays said name, author, ect.

When I hover over the name or title, it will display that this particular area uses field.html.twig as its default base template. I created my own custom template that extends the base template, and wanted to edit it so it would look different.

However, doing so renders the field invisible. I tried copying the base template and pasting it into my custom twig template, however the field is still invisible. Only when I delete my custom template does it revert back to the original base template and display the field.

Here is where I am confused. My custom twig template is taking precedence, it shows that in the debug info, but I am copying the field.html.twig code into my custom code before I start editing. Why then is the fields disappearing off the page or not rendering considering that I am using the exact same coding?

Just for reference, this is a custom module template and not a themeing template I am working with.

Here is a picture of what the field looks like when its displaying properly, otherwise it vanishs.

enter image description here

My hook theme code for this particular field:

  $theme['field__parent_task'] = [
    'render element' => 'content',
    'variables' => ['content' => NULL],
    'file' => 'task.page.inc',
  ];

I don't know what to put for the key names in the theme array, so I just copy and pasted from another one that was already there.

2

If you want to only override the field template.

Try with with the following.

  1. Implement hook_theme like the following no needs for variables and file keys.

  /**
     * Implements hook_theme().
     */
    function myModule_theme($existing, $type, $theme, $path) {
      return [
        'field__parent_task' => [
          'template' => 'field--parent-task',
          'base hook' => 'field',
        ],
      ];
    }

  1. Create templates folder in your module root.
  2. Create field--parent-task.html.twig inside your templates folder.
  3. Copy and the paste the base template code into your template file.
  4. Clear the cache.
  • Thank you so much, I been trying to figure this out for hours. It looks like the issue came down to not having a 'template' key and 'base hook' key. – Shawn May 17 at 20:45
  • You are welcome, i'm glad that helped :) – berramou May 17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.