Drupal hook is not applied to the website

Issue

I am writing a custom hook in my request related webform which grants access of a radios field (approve, reject, forward to) to the email address that is entered in an email field (by requestor).

the whole idea is:

requetor submits a form -> director will receive an email with the link and choose whether he wants to approve, reject, or pass it to the executives by editing form

I chose webform_alter hook to achieve this

function ach_request_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

if($form_id == 'ach_request') { //form's id

if($form['submitted_to']['#value'] === \Drupal::currentUser()->getEmail) { //if the value of the email element is equal to the email address of the current user (director);

$form['approval_state_']['#access'] = TRUE; //the radios element would be visible to the director with the same email address

}

}

}

Then I used drush cr in terminal but the select element is still not visible. Is there anything wrong with my code? Or do I need to change anything on UI as well?

Thank you in advance.

Solution

when you implement hooks, the "hook" part of the function name should be replaced with the module or theme name where it resides.
For example,
if you are trying to implement hook_form_alter and you have placed your function in your custom module named "mycoolmodule", your function name would be…

/**
* Implements hook_form_alter in a module called mycoolmodule.
*/
function mycoolmodule_form_alter($form, $form_state, $form_id) {
  // do something with the form here
}

Understanding hooks page on drupal.org

Answered By – 2pha

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published