Questions tagged as ['hooks']

Use it for the mechanism that allows to extends module's functionality without editing its code.
Score: 0
simp you avatar
Webform custom element layout twig
ph flag

I am creating a custom webform element, im using the /modules/contrib/webform/webform_example_example, i installed the module and added the element to a form but now i want to customize the webform in a twig file.

Is this possible?

Score: 1
Giuseppe avatar
How to hook on paragraph deletion?
br flag

I'm on D9 and I've a paragraph inside a content type node.

I need to hook when the paragraph is deleted to do some custom logic and set up a queue.

I've tried hook_entity_delete, hook_entity_predelete or hook_paragraph_delete, hook_paragraph_predelete: those hooks run during cron, if the node containing the paragraph is deleted.

However, it seems to me those aren't triggered if an existing paragraph ...

Score: 0
Jason avatar
Entity reference field hidden widget or edit disabled on custom form
in flag

I have a custom node creation form I am rendering within a views preprocess hook. In that form I have two entity reference fields. I am setting the [#value] and [#default_value] of those reference fields programmatically.

On form submit the values I've set are created, but only if the fields are "editable" by the user. I want to make the fields "disabled" and/or "hidden" from the user. Not just h ...

Score: 2
Add a custom variable to an existing block
mx flag

Is there a way to alter the render array of an existing block? I want to do it for a commerce block (cart block) but the question remains for any core or contrib module block.

When a block uses a custom #theme for it's block, is there a way to alter the definition?

Let's say the module defines the following theme :

 * Implements hook_theme().
function commerce_cart_theme($existing, $type, $theme, ...
Score: 1
SomebodySysop avatar
How to add view filter programmatically using hook_views_pre_view()
gb flag

I want to add a simple filter to an existing view in Drupal 9. It appears that I can achieve this with hook_views_pre_view(). In every example I can find, including all the "Similar questions" links that come up when entering the title of this post, I see that $view->add_item() is used. See: Add views exposed filter programmatically

However, when I try to run this code:

function sbn_views_pre_view($vi ...
Score: 0
Arif avatar
How to get remote post handler returned response at module file?
us flag

I want to get remote post handler returned response inside hook_webform_handler_invoke_post_save_alter.

Please help me.

function hook_webform_handler_invoke_post_save_alter(\Drupal\webform\Plugin\WebformHandlerInterface $handler, array &$args) {
  $webform = $handler->getWebform();
  $webform_submission = $handler->getWebformSubmission();
  $webform_id = $webform->id();
  $handler_id = $h ...
Score: 1
Vince Saseniuk avatar
Adding a class to an image using preprocess function
ck flag

I am having trouble adding a class to an image using a preprocessor function in Drupal 9. Specifically, I only want certain images to have that class added. The image is being added using paragraphs.

I've tried a few variations of the following and cannot seem to get class added. I've also tried using ['#item'] but it causes an error.

function confluence_preprocess_field__paragraph__field_card_imag ...
Score: 2
Gabriel Fernandez avatar
hook_node_presave get original of translated node
cn flag

I'm using a hook_node_presave and I want to detect which field is updated when the translated node is updated. Here is my code :

function hook_import_node_presave(\Drupal\node\NodeInterface $node) {
  if (!$node->isNew()){
    $entityFieldManager = \Drupal::service('entity_field.manager');
    $fields = $entityFieldManager->getFieldDefinitions('node', 'article');
    foreach($fields as $field ...
Score: 1
Bohus Ulrych avatar
Place field in "Url Alias" advanced group section (path-0)
jp flag

I'm trying to place my node field to the Advanced group - URL alias section. I'm doing it in custom hook_form_BASE_FORM_ID_alter(), where I'm trying to set custom #group like this:

$form['my_filed']['#group'] = 'path-0';

But "path-0" neither "path_0", "path" works. But if I use something else e.g. "meta", "menu" ... then my field is displayed in right place. What's the trick to place it in "Url Alias" ...

Score: -3
How to pass an argument to a hook without creating a custom module?
cn flag

I need to attach a theme-agnostic ("global") CSS file to the preprocess_hook() function so that any theme I use, would include that CSS.

I want to do that without creating a custom module because while I am a programmer, I am just not a PHP programmer and I find the documentation about creating custom modules overkill for this specific goal and I won't learn this entire subject just for a CSS file.

Score: 0
Patrick Star avatar
Hook for field not triggered when using delta in template
ie flag

I tried to hook my Link type field with machine name "field_link". This field is rendered in template using delta index to extract some values, e.g.:

{{ content.field_link[0]['#title'] }}

I have noticed that when I use content.field_link[0] or content.field_link.0 my hook HOOK_preprocess_field__field_link(&$vars, $hook) is not triggered. But as soon as I use content.field_link the hooks works.

Score: 2
add field to user in hook_update_n
pl flag

The latest release of our existing module needs to define MANY new fields that are attached to the drupal user. For each field, in {module}/config/optional there is a field config and a field storage config. I then implement hook_update_n in {module}/{module}.install to detect new config entries and add them if they don't exist. Running the code installs the config (I can see it with drush cget), but ...

Score: 0
Save textarea word count programmatically
cn flag

I'm trying to keep a word count tally on a set of long text fields. As far as I can see, there are several options:

  1. Create a view and add php to a custom template that produces the word count. Most solutions I've found seem too refer back to this 2005 issue, which is considerably out of date.

This would be a viable solution, except none of the code options listed work, and I'm not savvy enough with my p ...

Score: 0
How to edit file 'description' label and help text
in flag

I want to edit the file upload description label and the help sentence underneath it:

enter image description here

I tried the below code:

function mymodule_field_widget_form_alter(&$element, FormStateInterface &$form_state, $context) {
  if (isset($element['#field_name'] == 'field_file_description') {
      // Add process callback to change field description.
      $element['#process'][] = 'chg_desc';

func ...
Score: 0
Tommy Styrbæk avatar
Ajax message after node save
cn flag

I am trying to add a Ajax Alert or popup message inside a hook_node_presave or hook_node_update, but it is not working.

My current code look like this, but I have tried a lot of other ajax

$response = new ajaxResponse();
$response->addCommand(new AlertCommand('Ajax test'));
return $response;

The node is saved but no alert and no errors in the console. Drupal 9

Score: 0
Berry Dingle avatar
Throw error for required field in hook_form_alter?
cn flag

I have a node type that can be created by authenticated users. In the node create form, I added a terms-of-service checkbox via a form hook like so:

function themename_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_nodetype_form') {
    $form['field_tos_accept'] = array(
      '#type' => 'checkbox',
      '#title' => t('I agree to the terms of service.'),
      ' ...
Score: -1
Dragan Petrovic FSD avatar
How to remove big pipe <script> tags from page response
cn flag

I need to remove <script> tags from some pages. I have managed to remove almost all of them, but these remain:

<script type="application/vnd.drupal-ajax" data-big-pipe-event="start"></script>

<script type="application/vnd.drupal-ajax" data-big-pipe-event="stop"></script>

How do I remove those scripts?

Score: 1
Dragan Petrovic FSD avatar
Hook webform options WEBFORM OPTIONS ID alter()
cn flag

[{"value_1":"Dragon fly"},{"value_1":"Agapostemon angelicus"},{"value_1":"Albuna oberthuri"},{"value_1":"Callohesma flavopicta"}]

This is a JSON data i need to use to import for radios in drupal 8 webforms. Iam using hook_webform_options_WEBFORM_OPTIONS_ID_alter()

I cant use value_1 as a key  ...

Score: 0
Unable to inherit profile
ye flag

I am trying to create a custom installation profile which will use standard as a base profile. So I created the as below:

name: 'Custom profile'
core_version_requirement: '^9'
type: profile
base profile: standard
description: 'The custom Drupal profile for all CMS websites based upon standard.'
  - menu_ui

  - bartik

But I am not getting any modules which ...

Score: 0
SomebodySysop avatar
How to use hook_block_access() with custom block created in UI
gb flag

I have a custom block created using Drupal 9 Custom block library. I want to use hook_block_access() to control access to it, but, as it is a custom block, it does not have a block machine name. The only ID I can find (using Devel variables) is:

stdClass Object ( [CLASS] => Drupal\block_content\Entity\BlockContent [theme:protected] => [values:protected] => Array
( [id] => Array ( [x-d ...

Score: 0
hotwebmatter avatar
Display machine name in entity_form_display_edit_form
nr flag

The Entity Form Display edit form has a hidden field:

      'hidden_name' => [
        '#type' => 'hidden',
        '#default_value' => $field_name,
        '#attributes' => [
          'class' => [

Can I make this field visible with a hook_form_alter() implementation?

Here's what I have so far:

 * Implements hook_form_a ...
Score: 0
Jean da Silva avatar
Hook_install or hook_modules_installed save entity does not work
cn flag

I am trying to insert a new vocabulary to taxonomy because a custom entity from my custom module needs that vocabulary as an entity reference. I think it would be better to always insert the reference vocabulary to make it certain that it will always be there instead of doing it manually by the site.

The problem is the $entity->save() doesn't work neither in hook_install or in hook_modules_installed. ...

Score: 2
Bisonbleu avatar
How can I capture a field’s configs and storage so I can delete it and then re-create it?
pl flag

I’m trying to implement this in a hook_update_N() but I’m not sure it’s proper (see excerpt below). Note I’m changing max_length in the process.

What I'm trying to do: update my_custom_entity.field_my_custom_field which has saved data into it. So I need to:

  1. Store that data
  2. Store the field configs - if that’s possible?
  3. Store the field storage - if that’s possible?
  4. Delete the field
  5. Create the f ...
Score: 1
Custom display in saved Entity reference in Widget field
us flag

We have an entity reference field widget. Based on a view where you can enter the code of a product to get to the product reference.

The widget shows the 2 fields (code) - (Title) when we type in the widget.

But after saving, only the title and the Node ID is shown. Title (nodeID)

How can I override what is displayed in the entity reference field to include my product CODE in prefix? (code) - (Title) ( ...

Score: 0
Using memcached and ip_ban simultaneously?
us flag

Memcached is by no doubt an essential tool to speed up Drupal websites. Is there a way to use the ip_ban module in order to block certain countries from accessing my website before the cached content is delivered to each visitor?

I know it's somewhat counter-intuitive, and I know there's Cloudflare WAF that may resolve this issue on the DNS level, but I wonder if there's any Drupal way of resolvin ...

Score: 5
How to make a checkbox read-only/disabled from list of user entity references
cn flag

In my Content type, I add a field Reference: User with unlimited values, Reference method from a view of user managers, and widget checkboxes.

Basically when a user manager edit the node, he can only check the box with his own name. The other checkboxes are set to disabled. The node will be published if all managers check the box.

I've tried creating form_alter function like below

function mymodul ...
Score: -1
vyshnavi avatar
drupal custom callback in node edit form is not firing
eg flag

i am fairly new to drupal , i have custom button added to one the content type edit page , when i click on that button i want to call a function but it seems like it is not calling function when i see the console i see drupa ajax error coming whenever i click the button . please let me know what i am doing wrong here .

function mymodule_form_node_program_edit_form_alter(&$form, FormStateInterf ...
Score: 0
matts213 avatar
Add class to field parent with a preprocess hook in a custom module?
cn flag


I would like to alter the classes that are added to a rendered field in Drupal 9 in a custom module. I'm trying to use hook_preprocess_field and I'm not sure if this will work or if there is a better alternative.

based on:

I have the following:

function my_module_preproc ...
Score: 0
Павел Герасюта avatar
hook_forms() not working
br flag

I'm trying to add a button to all node forms via callback in hook_forms. I get a similar error. Can someone help.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'my_dossier_form' not found in drupal_retrieve_form() (line 844 of /var/www/includes/
Notice: Array to string conversion in drupal_prepare_form() (line 1094 of /var/www/includes/
Notice ...