![]() Let’s create a wrapper block around all the markup, a content block around the contents of the wrapper, and a label block around the label.Drupal 8 is an open source content management system and powerful framework that helps deliver great websites to individuals and organizations, including non-profits, commercial, and government around the globe. Now wrap Twig blocks around any markup we want to be able to override in other templates. Below, I’ve started with core/modules/system/templates/ and refactored it to move the wrapping div outside all the conditions. Start by copying the core field template into your custom theme’s templates directory. Let’s find a better way! Through judicious use of Twig " blocks”, we can mitigate the first two issues entirely and reduce the impact of the third. ![]() Future changes to the original template usually go unnoticed rather than being evaluated for applicability and ported into the override.Depending on the changes made to the override, it may lose contextual wrappers (the “gear icon”) and attributes data injected by other modules.It usually results in significant code duplication between the original template and the override.There are three problems with this process: Make your desired changes to the template.Rename the file to match the template suggestion.Copy/paste the original template to your theme’s templates directory.Naming suggestions for the new template.Which template is currently being used.Override templates without duplicating markupĬreating a theme template typically goes like this: Let’s now look at how to implement the new field-details template suggestion efficiently. The |as filter allows front-end developers to stay out of the PHP if they want to. While there’s nothing wrong with this approach, it creates a dependency on PHP skills for any developer wishing to work on the front-end. Normally, you would implement hook_theme_suggestions_alter() in a theme or module file to make unrelated fields use the same template. Keeps front-end logic in the front-end.The addition of |as('details') makes the code more readable and adds semantic meaning to the source. But to an outsider reviewing the Article’s node template, the source of the wrapper around the body field may prove arcane. For experienced Drupal developers, the idea of template suggestions and their naming scheme might seem obvious.If you have another field that needs the same customization, you no longer have to resort to various forms of chicanery, like copy/pasting the original template into an exact but renamed duplicate, Twig extendsing an otherwise-unrelated file, or symlinking one template to another. Naming the template in a generic way leaves it available for use by other fields.“How is this different/better” you might be asking “than just using the standard field-name template suggestion, field-node-body-article?” Three main reasons: , along with a new template,, that adds the markup. ![]() īut QA kicks it back, noting that the body field is not required and an empty element shows on the page when the field is empty. To highlight these key concepts, let us consider a simple example-wrapping the Article node’s body field in a element. More flexibility with |value and |label filters.Override templates without duplicating markup.Learning when and where to apply them will empower you to create more readable and DRY code, and allow themers of all experience levels to contribute in a maintainable way. Drupal 8’s templating language, Twig, provides a powerful suite of tools that often go underutilized.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |