Hot answers tagged

6 votes
Accepted

How to hook on paragraph deletion?

You need to implement a node update hook and compare the paragraph IDs: use Drupal\node\Entity\NodeInterface; /** * Implements hook_ENTITY_TYPE_update() for node entities. */ function ...
user avatar
  • 88k
4 votes
Accepted

How to make a checkbox read-only/disabled from list of user entity references

As far as I know, Drupal core does not provide this functionality out of the box - there's no way to add attributes to an element's individual options. However, one way I've done this is with the help ...
user avatar
  • 7,123
4 votes
Accepted

Is it bad practice to use the same hook twice?

Of course you would have to benchmark your modules to get an exact answer, but generally speaking, the benefits of breaking up complex code into multiple modules separated by function is the way to go....
user avatar
3 votes

Derive all image styles when user uploading

I believe that the Image Style Warmer module is what you want. The Image Style Warmer module provides options to create image styles during upload or via queue worker. So configured image derivates ...
user avatar
  • 14.7k
3 votes

Hook webform options WEBFORM OPTIONS ID alter()

I just found a solution. I probably didn't explain it well, so there was no answer. I hope that the solution to the problem I had will better explain the situation I was in. function ...
user avatar
3 votes
Accepted

Adding Javascript within a hook_entity_type_presave() hook

hook_entity_presave() and hook_ENTITY_TYPE_presave() should not assume the entity has been edited using a form, or a page has been shown to the users. Those hooks are also invoked when an entity is ...
user avatar
  • 94.1k
3 votes

In a hook, how can I distinguish between web users and JSON:API users?

Using redirects with $response->send() like this used to work: $response = new RedirectResponse($url); $response->send(); But, this no longer works in Drupal 9.2. Also, it breaks JSON:API ...
user avatar
3 votes

Is it bad practice to use the same hook twice?

The number of installed modules may influence performance. That's why, for example, Drupal core keeps in cache the name of the implemented hooks. Given what Drupal core does, increasing the number of ...
user avatar
  • 94.1k
3 votes

How can I retrieve information about the current request's URL?

I need help finding a way to get the current request URL in a controller In a controller get the request with a type-hinted argument Request $request: <?php namespace Drupal\mymodule\Controller; ...
user avatar
  • 88k
3 votes
Accepted

String substitution in Views Contextual filter parameter?

Would hook_views_pre_view() do the trick? function hook_views_pre_view(ViewExecutable $view, $display_id, array &$args) { if ($view->id() == 'my_view' && $display_id == '...
user avatar
3 votes
Accepted

How to not show the "has been created" message?

If you want to add a custom message in a hook, you can clear the messages and then add your custom message. This is quick and easy, and it can be done in most hooks. Unfortunately, this won't work ...
user avatar
3 votes

How can I get a multivalue field correctly inserted?

The field deltas are one level higher than the properties. $entity->field_myfield = [ 0 => [ 'value' => 'one', 'safevalue' => 'one', ], 1 => [ 'value' => 'two', ...
user avatar
  • 88k
2 votes

Set CSS class for a View programmatically in a hook

It works in drupal 9 /** * Implements hook_views_pre_view(). */ function MODULE_NAME_views_pre_view($view, $display_id, $args) { if ($view->id() == 'YOUR_VIEW_ID' && $display_id == '...
user avatar
  • 21
2 votes
Accepted

Example hook_pathauto_pattern_alter

hook_pathauto_pattern_alter() is only invoked when: The entity has a path field Pathauto processing is enabled For entities that support revisions, the entity is using the default revision A default ...
user avatar
  • 94.1k
2 votes
Accepted

Can't get page html after updating the page and flushing all caches

This is not a valid use case for drupal_flush_all_caches(). This function is for changed or new code. For content you have to use cache tags. In the rare case, where it is not possible to add correct ...
user avatar
  • 88k
2 votes
Accepted

How to use hook_block_access() with custom block created in UI

After you've created the custom block in UI you can place it in different regions. Each of these instances gets a machine name and hook_block_access() works as intended. If you want to control access ...
user avatar
  • 88k
2 votes

How to make a checkbox read-only/disabled from list of user entity references

Do this: function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) { if ($form_id === 'node_request_order_edit_form') { $field_approved_by = $form['...
user avatar
  • 1,061
2 votes

How to remove big pipe <script> tags from page response

These scripts are added by the core module BigPipe. If you want to remove them from a specific page then add the option _no_big_pipe to the route: $route->setOption('_no_big_pipe', TRUE); See ...
user avatar
  • 88k
2 votes

add field to user in hook_update_n

Final Solution based on accepted solution from @4k4 (see answer). <?php use Drupal\Core\Database\Database; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Utility\NestedArray; use ...
user avatar
2 votes
Accepted

add field to user in hook_update_n

You need the config entity classes FieldStorageConfig::create() and FieldConfig::create(). You have already the use statements and are not using them. Otherwise the entity events like postcreate and ...
user avatar
  • 88k
2 votes
Accepted

Add a custom variable to an existing block

You can do it with hook_preprocess_HOOK and the all caps HOOK part refers to the array key in hook_theme(). /** * Implements hook_preprocess_HOOK(). */ function ...
user avatar
  • 29.6k
2 votes
Accepted

What is the difference/purpose of 'content' and 'elements' keys, in preprocess?

elements is the variable name defined in theme_hook() to hold the renderable elements (for the node this is the entity build array). Preprocess is then responsible to prepare variables for rendering, ...
user avatar
  • 88k
2 votes

How do I properly define a library so that JS will execute after a certain condition is met?

I wouldn't use Drupal::state() or hook_page_attachments(), those are too static. Better use a session value and a block, see my comment in the previous question. Set the session value in the hook when ...
user avatar
  • 88k
2 votes
Accepted

Programmatically change view mode of media field

In my wysiwyg I'm embedding media entities. When I embed a media entity I want to be able to change the view mode that is used. When you embed a media entity in WYSIWYG you can select a view mode. If ...
user avatar
  • 88k
2 votes
Accepted

How can I detect when a stream wrapper is uninstalled?

Yes. The module providing the stream wrapper should implement an uninstall validator, that checks for usages.
user avatar
  • 2,790
2 votes
Accepted

Clarify difference between hook_form_TYPE_alter and hook_form_TYPE_EDIT_alter

You can either use the full form ID with content type and operation or the base form ID node_form. This is without the operation but also missing the content type. So when you alter the base form you ...
user avatar
  • 88k
2 votes

How to not show the "has been created" message?

I've accepted and upvoted Patrick Kenny's answer, since it provides 99% of the solution. However, I realized that I also needed this worked out: I examine the node in hook_node_insert, and when ...
user avatar
  • 14.8k
2 votes

Run hook_preprocess_node only for main page

If the other nodes are rendered using a different view mode, you can check that $variables['view_mode'] is 'full' or the view mode used for rending nodes on /node/[nid]. Otherwise, you can verify the ...
user avatar
  • 94.1k
2 votes
Accepted

How to render a custom block with variables from another custom block twig template

Variables are passed from PHP/preprocess to Twig, but never vice versa. Instead of preprocessesing the filter of the second block, replace the product type filter in the 2nd views block with a "...
user avatar
  • 4,908
1 vote
Accepted

hook_node_presave get original of translated node

$node = $node->getTranslation($langcode); This is changing the translation of the node object passed by reference. Better use a different variable: $node_translated = $node->getTranslation($...
user avatar
  • 88k

Only top scored, non community-wiki answers of a minimum length are eligible