Drupal 8 hook example: hook_form_alter

This hook is used most often in drupal web development.

  1. <?php
  2. **
  3. * Implements hook_form_alter().
  4. */
  5. function custom_module_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  6.  
  7. if ($form_id == 'node_some_type_form') {
  8.  
  9. // Publish button text based on node state
  10. case 'isnew':
  11. $form['actions']['publish']['#value'] = t('Submit Application');
  12. break;
  13.  
  14. case 'saved':
  15. $form['actions']['publish']['#value'] = t('Submit Application');
  16. break;
  17.  
  18. case 'submitted':
  19. $form['actions']['publish']['#value'] = t('Update Application');
  20. $form['actions']['submit']['#attributes'] = array('class' => array('submitted'));
  21. break;
  22.  
  23. // Add link to Terms and Conditions
  24. $form['field_i_agree_to_the_terms_and_c']['#suffix'] = '<a class="field--name-field-i-agree-to-the-terms-and-c-descr " href="https://www.w3schools.com">Terms and Conditions</a>';
  25.  
  26. // Hide/disable unpublish button
  27. $form['actions']['unpublish']['#access'] = FALSE;
  28. $form['actions']['submit']['#value'] = t('Save Application');
  29. $form['actions']['submit']['#access'] = TRUE;
  30.  
  31. $form['actions']['publish']['#access'] = TRUE;
  32. $form['actions']['publish']['#type'] = "submit";
  33. $form['actions']['publish']['#submit'] = $form['actions']['submit']['#submit'];
  34.  
  35. // Custom submit handler
  36. array_unshift($form['actions']['publish']['#submit'], '_custom_module_change_TF');
  37.  
  38. // Hide the field
  39. $form['field_submitted_application_t_f']['#access'] = FALSE;
  40.  
  41. // Custom redirect
  42. $form['actions']['submit']['#submit'][] = '_custom_module_submit_redirect';
  43. $form['actions']['publish']['#submit'][] = '_custom_module_submit_redirect';
  44.  
  45.  
  46. $current_user = \Drupal::currentUser();
  47. $roles = $current_user->getRoles();
  48.  
  49. if (!in_array('administrator', $roles)) {
  50.  
  51. $form['#validate'][] = 'all_form_app_form_validate';
  52. $form['field_opportunity_areas']['#access'] = FALSE;
  53. $form['field_client_journey']['#access'] = FALSE;
  54. $form['field_new_technology_trends']['#access'] = FALSE;
  55. $form['field_business_modelb']['#access'] = FALSE;
  56.  
  57. }
  58.  
  59.  
  60. /* Ajax submit */
  61.  
  62. $node_status = fin_get_application_state();
  63.  
  64. if ($node_status != 'isnew') {
  65. $form['actions']['submit']['#ajax'] = [
  66. 'callback' => 'Drupal\custom_module\AjaxAppSubmit::ajaxSubmitCallback',
  67. 'event' => 'click',
  68. 'progress' => [
  69. 'type' => 'throbber',
  70. ],
  71. ];
  72.  
  73. $form['actions']['#weight'] = 101;
  74.  
  75. $form['system_messages'] = [
  76. '#markup' => '<div id="' . Html::getClass($form_id) . '-messages"></div>',
  77. '#weight' => 100,
  78. ];
  79.  
  80. $form['changed']['#prefix'] = '<div id="changed_cont">';
  81. $form['changed']['#suffix'] = '</div>';
  82. $form['form_build_id']['#prefix'] = '<div id="form_build_id_cont">';
  83. $form['form_build_id']['#suffix'] = '</div>';
  84. $form['form_token']['#prefix'] = '<div id="form_token_cont">';
  85. $form['form_token']['#suffix'] = '</div>';
  86. }
  87.  
  88. $form['#group_children']['#weight'] = -100;
  89. }
  90.  
  91. if ($form_id == 'node_profile_form') {
  92. $form['actions']['unpublish']['#access'] = FALSE;
  93. $form['actions']['publish']['#submit'][] = '_custom_module_submit_redirect';
  94. $form['actions']['publish']['#value'] = t('Save');
  95. }
  96. }
  97.  
  98. function _custom_module_submit_redirect(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  99. $url = Url::fromRoute('view.my_view.page_1');
  100. $form_state->setRedirectUrl($url);
  101. }