Drupal, Typescript, Angular and Midjourney blog content, code snippets and tutorials.
Blog
Render a block programmatically in twig with drupal 8 or 9
In the early days of Drupal 8 we used the EntityTypeManager to render some plugin blocks or other entities. But this changed a bit. Here's a snippet on how to render a block inside your twig files.
Blog
Set media image url with image style token for meta image in drupal 8 & 9
Image url is a must-have for webpages to be properly shareable on social media. With this token snippet you can set a media entity url with image style token.
Blog
Redirect taxonomy terms to filtered view page
A common case when using facet searches is where you want to list some terms and when people click on them you get redirected to a filtered view. This code helps you with that.
Blog
Loop through entity reference in Twig to get id's in drupal 8
Entity references are widely used in drupal to link to other entity objects. Here's how to loop over them in a twig file.
Blog
Check if entity object is of entity type node
<p>There are some ways to make sure your loaded object is in fact an entity of type node. I prefer the following way. In addition I check if the entity types are correct too.</p>
Blog
Translate dates in Twig with drupal 8
I had to output my dates in dutch format, but struggled a bit to do it automatically. I discovered a nice twig built-in feature.
Blog
Add html to a menu links in drupal 8 (twig)
I found a nice way to build complex menu's with twig in Html. Here are a few snippets
Blog
Add views machine name as a class in twig [drupal 8 or 9]
With this views-view.html.twig file in your theme, you add the machine name of the view to your container class.
Blog
How to update a custom entity in drupal 8 once installed: add a field
Custom entities provide great control and flexibility because your entire entity resides in code. This snippet shows how to add a field to your entity, with an update hook. Because once the entity is installed, you should be able to add/remove/edit your fields.
Blog
Create an extra tab on the user log in page
I had a case where an extra tab was necessary on the user pages. This snippets show you how to do it.
Blog
Redirect form after registration in drupal 8
Sometimes it is needed for a user to be redirected after registration. The user is not logged in yet, and has to go to a landing page with more instructions on verifying his e-mail.
Blog
Trim metatag description to 140 characters in drupal 8
By default drupal will take 600 characters to show your description. But you need to know that this setting is used to show in the metatags as well.
Blog
Get node from url in drupal 8
<pre>
<code class="language-php">foreach (\Drupal::routeMatch()->getParameters() as $entity) {
if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
// here $entity is your object
}
}</code></pre>
<p> </p>
<code class="language-php">foreach (\Drupal::routeMatch()->getParameters() as $entity) {
if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
// here $entity is your object
}
}</code></pre>
<p> </p>
Blog
Create forum topic programmatically in drupal 8
The forum module is a core module that was rewritten for drupal 8. In this snippet I show you how to create forum topics programmatically.
Blog
Clear specific cache in drupal 8
Drupal's default caching mechanism is to clear "all" cache. This is rather inefficiënt. There are options to clear specific caches only, here's how.
Blog
A default .gitignore file for drupal 8
This example might help you setting up default installations.
Blog
Tailwind css responsive footer full example
I was building a website with Tailwind and thought it would be nice to share this generic footer html with you.
Blog
Validate numeric url parameter for controller using regex in drupal 8
The routing system that drupal 8 offers is flexible in terms of validation. It is possible to validate your route against some typical regex lines.
Blog
Disable caching on a drupal 8 page controller
Caching in drupal 8 is quite nifty, here's a snippet on how to completely disable caching in your controller.
Blog
Tailwind css introductie (code + video) [dutch]
Ik ben onlangs beginnen experimenteren met Tailwind CSS en dit nieuwe framework biedt een hoop mogelijkheden.
Blog
How to "composer require" Github package
Sometime you would want to require a module or library from Github instead some packagist or drupal.org. Here's how you would do this.
Blog
Caching in drupal 8 & 9 explained [video & code]
In this video I discuss block caching in drupal 8 & 9. It is quite radically different from drupal 7. I discuss cache tags, cache context and cache max-age.
Blog
Nid to url in twig [Drupal 8]
What if you have to link to a specific url in in twig, but you only have the nid? Moreover, it is good practice to use the id and not a hardcored url. Here's what you should use:
Blog
drupal 8: image field to image style programmatically
Helper snippet to render images with an image style in drupal 8
Blog
Create string translations programmatically in drupal 8
String translations are always a bit a pain in the ass for drupal. Luckily, in drupal 8 the function is rather easy.
Blog
Delete all users of a role in drupal 8
Snippet on how to remove all users with a certain role in drupal.
Blog
Delete all menu links of a menu programmatically in drupal 8
I play a lot with importing and exporting content lately. When I remove content, the created menu items need to be removed automatically. Here's a snippet on how to delete all links in a menu.
Blog
Create a menu link with html markup in drupal 8 (menu plugin tutorial)
Goodbye preprocess functions! Welcome to drupal 8. We use a nice plug-in to get control over our menu item and to show html inside the title. In this case we show a counter of the amount of messages a user has.
Blog
Format created date of custom entity in drupal 8 [Solved]
Some parts of custom entities seem different from nodes, but mostly they aren't. Small snippet for showing a created date of a custom entity in a controller
Blog
Fix "The provided host name is not valid for this server" error of drupal 8
When installing drupal 8, or moving to production one could see this error. The fix is extremely simple.
Blog
Serve a pdf download in controller in drupal 8
Quick tutorial on how to serve a PDF that downloads automatically in your browser in drupal 8.
Blog
Change the page title of the user edit form in drupal 8 with a RouteSubscriber
Sometimes simple tasks like changing a page title of a system page can be hard. Luckily, drupal 8 provides clean ways to do that. In this tutorial some info about RouteSubscribers.
Blog
How to create menulink programmatically in drupal 8 [SOLVED]
When creating seed data, for example in your modules hook_install() script, creating a menulink can be necessary. This snippet shows how.
Blog
Use html markup in form labels in drupal 8
Getting the output like you want it to be an be quite a challenge in drupal. This snippet shows you how you can add markup to your form labels in drupal 8.
Blog
Validate a UUID in drupal 8 (check if uuid is valid)
In drupal 8, each entity will get a UUID assigned automatically. With this snippet you can check if it is a valid one.
Blog
Blog
Create or add a menu item without a link in drupal 8
In drupal 7 we had some contrib modules using a <nolink> tag to make subtitles in our menu. In drupal 8 I try to avoid contrib modules on my menu items. Here's a hint on how to get it to work.</nolink>
Blog
Set placeholder for Chosen widget in drupal 8 form alter
The chosen widget is my default option for select widget. This snippet shows you how to add the placeholder in a form alter.
Blog
Create an instagram widget of an hashtag in drupal 8
For a few months I'm working in my spare time on a module that can import instagram feeds and turn it into nodes. And on top of that, a nice javascript extension to make it look beautiful out-of-the box.
Blog
Make sure javascript keeps working after ajax call in drupal 8 [solved]
A thing developers can forget while testing, is to make sure all functionality of your module is stable when used in the context of ajax. Some plugins might stop working after an ajax call on the page. This is how to prevent such thing.
Blog
Increase the length of a custom entity name field in drupal 8 [SOLVED]
It is a bit strange that the name field (which functions like the node's title field) can be only 50 characters long. The snippet I'd like to share with you, makes it 255 long.
Blog
Styling the 404 error pages in drupal 8
Drupal ships with an ugly 404 page by default. in this tutorial I learn you how to add markup to it.
Blog
Get entity reference objects from node programmatically in drupal 8
There is a great shortcut I use a lot when dealing with entity references (the're great, right). I thought I'd share the snippet here.
Blog
How to check if custom entity is published in drupal 8 [SOLVED]
We all well know the functions to check if a node is published or not, but what about a custom entity?
Blog
How to render a table in drupal 8 (with html in cells)
You create a custom controller and want to show a table that resembles the core back-end tables. This snippet shows you how to do it.
Blog
Two ways of getting the current node id in drupal 8
A very recurring thing you would do is to get the current node id of the visited page. Here are two ways of doing it programmatically.
Blog
Drupal 8: get the current path alias
Where in drupal 7 you would typically use something like drupal_get_path_alias(), this is different in drupal 8.
Blog
How to access a field value from entity (or node) object in drupal 8, 9 & 10
Discover how to access field values from entity or node objects in Drupal 8 and 9, covering regular text, link, entity reference, image, date, boolean, and list fields.
Blog
Get parameter from url in drupal 8 [solved]
Short snippet that shows how to get url parameters in drupal 8. Where in drupal 7 you would have to do some $_GET or work with args(0), you now have a clean function to do this.
Blog
How to use image styles in Twig on drupal 8
I had to loop over some images in Twig and dynamically adding image styles to them. Without using contrib this seemed a bit hard to do. I found a fix and I share it here with you.
Pagination
- Previous page
- Page 7
- Next page