Questions tagged as ['hooks']
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?
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 ...
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 ...

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, ...
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 ...
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 ...
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 ...
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 ...
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" ...

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.
...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.

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 ...

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:
- 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 ...

I want to edit the file upload description label and the help sentence underneath it:
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 ...
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
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.'),
' ...
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>
</body>
How do I remove those scripts?
[{"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() https://www.drupal.org/docs/8/modules/webform/webform-cookbook/how-to-use-options-from-an-external-webservice
I cant use value_1 as a key ...

I am trying to create a custom installation profile which will use standard as a base profile. So I created the customprofile.info.yml 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.'
install:
- menu_ui
themes:
- bartik
But I am not getting any modules which ...
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 ...
The Entity Form Display edit form has a hidden field:
'hidden_name' => [
'#type' => 'hidden',
'#default_value' => $field_name,
'#attributes' => [
'class' => [
'field-name',
],
],
],
Can I make this field visible with a hook_form_alter()
implementation?
Here's what I have so far:
/**
* Implements hook_form_a ...
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. ...
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:
- Store that data
- Store the field configs - if that’s possible?
- Store the field storage - if that’s possible?
- Delete the field
- Create the f ...

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) ( ...

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 ...

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 ...
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 ...
Background:
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.
I have the following:
function my_module_preproc ...
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/form.inc).
Notice: Array to string conversion in drupal_prepare_form() (line 1094 of /var/www/includes/form.inc).
Notice ...