Stef Van Looveren
  • Member for 5 years
  • Last seen this week
Get taxonomy terms
13 votes

This is what I use to create a list of tags: use Drupal\taxonomy\Entity\Term; use Drupal\Core\Link; use Drupal\Core\Url; $vocabulary_name = 'YOUR_VOCABULARY_NAME'; //name of your ...

View answer
How do I apply a patch with Composer workflow?
10 votes

First, run Composer require cweagans/composer-patches Now you have a package installed that enables you to patch core, libraries or modules. Second, add a patch to a folder named 'patches' in the ...

View answer
How can I change a user's password?
10 votes

In Drush 9 (recommended for > Drupal 8.4) this is slightly different: drush upwd USERNAME PASSWORD

View answer
Site UUID in source storage does not match the target storage
8 votes

Traditionally, you would use the following before importing config: drush cset system.site uuid "6ba9bxxx-xxxx-438x-9c8x-e5x7x3d0x347" Or with drupal console: $ drupal config:override Enter ...

View answer
How can I create a patch for a contrib module or core which is not in version control?
Accepted answer
8 votes

So, you would have to download the module seperately, like: git clone --branch 8.1.x http://git.drupal.org/project/colorbox.git Then, you make the change and run the following: git diff 8.1.x > ...

View answer
Programmatically update an entity reference field
7 votes

You should be aware of the difference between a single value field and a multiple value field. Code below shows how. Source: https://stefvanlooveren.me/blog/how-programmatically-update-entity-...

View answer
How to create a custom Webform handler to submit to a SOAP API
Accepted answer
6 votes

In your custom module add a folder src/Plugin/WebformHandler and create a file Subscription.php. Add the following code: <?php namespace Drupal\<mycustommodule>\Plugin\WebformHandler; use ...

View answer
Allow markup inside drupal 8 Link function
Accepted answer
6 votes

Use the Markup class: use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\Core\Render\Markup; $url = Url::fromUri('http://www.example.com'); $linkText = 'My link <br> containing ...

View answer
Does the login-link users receive upon registration actually expire?
4 votes

Nope, new users' one-time-link do not expire. Only "forgot passwords"-links do. Also, see this discussion. It might come in future Drupal 9 releases. https://www.drupal.org/project/drupal/...

View answer
How do I programmatically install and enable a module?
4 votes

For drupal 8 you can use the following: \Drupal::service("module_installer")->install(["my_custom_module"]); In drush: drush php-eval '\Drupal::service("module_installer")->install(["...

View answer
How to patch your htaccess file correctly in composer.json
Accepted answer
4 votes

In the scripts section of your composer.json file, use the following: "post-drupal-scaffold-cmd": [ "patch -p0 < patches/htaccess.patch" ] (this requires "drupal-composer/drupal-scaffold" in ...

View answer
How do I get the field label from a node object?
4 votes

To get the field label in Twig (in this case in a node-NODETYPE.html.twig): {{ content.field_FIELDNAME['#title'] }}

View answer
How to rebuild route cache programmatically
4 votes

If you would like to clear route cache programmatically in drupal 8, use the following: \Drupal::service('router.builder')->rebuild();

View answer
Retrieve translated taxonomy term in current language programatically
4 votes

The snippets above will return untranslated terms also. You must check if a term is translated with the hasTranslation function: $vocabulary = 'MY_VOCABULARY_NAME'; $language = \Drupal::...

View answer
Redirect after user login
3 votes

I use the following snippet quite a lot so I thought I'd share it. You can add different redirects depending on which role the user has. use Symfony\Component\HttpFoundation\RedirectResponse; /** * ...

View answer
How do I structure the batch API array?
3 votes

As this was for me a really time consuming issue, I uploaded an example module on my GitHub page: drupal 8 batch import example with interaction. $batch = [ 'title' => t('Importing animals'), '...

View answer
How can I provide an autocomplete filter for tags in a Search API view?
Accepted answer
3 votes

Add the datasource (entity reference) as a exposed filter. Check the "allow multiple selections". Now, the user can add comma seperated tags (with autocompletion): Used search api 8.x-1.13 on ...

View answer
How can I query a database created using the previous core version from the new core version?
Accepted answer
3 votes

You can add external databases in your settings.php file: $databases['external']['default']= array( 'database'=>'', 'username'=>'', 'password'=>'', 'prefix'=>'', 'host'=>'', ...

View answer
How to properly add inline Javascript?
3 votes

In some cases you would really want to add a snippet somewhere inside your code, inside script tags. This is how you do it: Let's say you want the snippet inside a controller. Create a custom module ...

View answer
How do I add an extra "Add content"-link to the admin toolbar dropdown
Accepted answer
3 votes

This is fairly simple. First, create a your_module.links.menu.yml file in your custom module and add the following: your_module.admin: title: 'Add content' parent: system.admin_content url: ...

View answer
Patching .htaccess file
3 votes

For drupal 8, use the following. In the 'scripts' section of your composer.json, add "post-drupal-scaffold-cmd": [ "patch -p0 < patches/htaccess.patch" ]

View answer
How to do a RedirectResponse with a destination query parameter?
3 votes

This full example might help: use Symfony\Component\HttpFoundation\RedirectResponse; $target = 65; // a parameter you want to add $url = Url::fromUri('internal:/node/add/page'); // choose a path $...

View answer
get taxonomy terms of specified language
3 votes

I had some difficulties with printing tags in the current language in code too. This is what worked to get ONLY the translated terms of a vocabulary: $vocabulary = 'MY_VOCABULARY_NAME'; $language = ...

View answer
How do I programmatically render a custom entity?
Accepted answer
3 votes

Make sure the entity id exists and the error disappears.

View answer
How to get the base URL of a site
2 votes

Code below is how core defines the [site:url] token. This works best in all cases for me in drupal 8 & 9: $url_options = [ 'absolute' => TRUE, 'language' => \Drupal::languageManager()-&...

View answer
add a link to admin menu bar
2 votes

In drupal 8 here is how you would attach a menu-link to the toolbar. Add a file my_module.links.menu.yml to your module: my_module.admin_settings_form: title: 'MyModule Settings' route_name: ...

View answer
How do I prevent node enumeration?
2 votes

This is difficult, because it is core functionality. What you could do to prevent people from checking node/4, node/5 etc is blocking access when a url request has a certain structure. You'd need a ...

View answer
Clearing Twig cache only?
Accepted answer
2 votes

Invalidate the twig cache first, like this: drush php-eval "\Drupal::service('twig')->invalidate();" drush cc render

View answer
Reinstall a patch with composer - update one module only
Accepted answer
2 votes

Yes, use the following: composer update --prefer-source drupal/menu_block

View answer
How do you wrap an image with a link?
2 votes

This way should be bulletproof: use Drupal\Core\Link; use Drupal\Core\Render\Markup; $url = $node->get('field_url')->getValue(); $imageUri = $node->get('field_image')->entity->...

View answer