New answers tagged

0 votes

cant get file uri using field preprocess

In a template don't use a content variable to get references to objects. This is what the template is rendering. The structure depends on the field formatter and its configuration. Use it only if you ...
user avatar
  • 88k
1 vote

How to set value on drush updb running and get that value on hook_update_N, to call specific function?

It's not possible because drush updb doesn't accept a parameter that is passed to hook_update_N() implementations, which makes sense, since the only parameter accepted from hook_update_N() is an array ...
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
0 votes

How to use helper/private methods with a .module file?

I would either put the code in a service, as leymannx said in his answer, or do like two Drupal core modules do: Put the code in a class that is retrieved and instantiated with \Drupal::service('...
user avatar
  • 94.1k
0 votes

Load css and js assets as absolute paths during twig render

The folwing hook implemention will change the URL's to absolute URL's /** * Implements hook_js_alter(). * * Pass the JavaScript array to the optimizer service. */ function HOOK_js_alter(&$js, $...
user avatar
1 vote

Access preprocess variable globally

$variables['elements']['content']['#block_content'] is not available in other templates, which you can target directly or via the general HOOK_preprocess((&$variables, $hook) and $hook != 'block'. ...
user avatar
  • 88k
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
0 votes

How add conditional required field

In Drupal 9, there's no need to add so much custom code like the other answer. Here's an easy example, with the condition being a checkbox checked/unchecked: public function buildForm(array $form, ...
user avatar
  • 367
0 votes

remove cache in hook_preprocess_node

In THEME_preprocess_node hook you can add the below code to disable the variable cache for particular content type. if($variables['node']->getType() == 'content_type_machine_name') { $...
user avatar
  • 105
1 vote

How to add class to paragraph fields through preprocess hook?

Come from the other way. Come from the field. Check the parent entity (paragraph) type and then add the classes: /** * Implements hook_preprocess_HOOK(). */ function ...
user avatar
  • 15.9k
0 votes

Generate Link based on custom content field types

Sorry for the late response but, haven't able to find myself some time to elaborate a good sample that may serve you as copy/paste answer. Said that, first of all you need to register your extra field....
user avatar
  • 856

Top 50 recent answers are included