Advertisement
idflorin

hook_form_BASE_FORM_ID_alter() for node forms.

Jan 28th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. /**
  2.  * Implements hook_form_BASE_FORM_ID_alter() for node forms.
  3.  */
  4. function custom_module_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  5.   $node = $form_state->getFormObject()->getEntity();
  6.   if ($node->getType() != 'article') {
  7.     return;
  8.   }
  9.  
  10.   $form['field_image']['widget'][0]['#title_field'] = FALSE;
  11.   array_unshift($form['actions']['submit']['#submit'], 'custom_module_form_submit');
  12.    #multiple field field_image_gallery
  13.  $i=0;
  14.   foreach ($form['field_image']['widget'] as $item) {
  15.     $item[$i]['#title_field'] = False;
  16.     $i++;
  17.   }
  18. }
  19. /**
  20.  * Custom submit handler.
  21.  */
  22. function custom_module_form_submit(&$form, FormStateInterface $form_state) {
  23.   $image_value = $form_state->getValue('field_image');
  24.   $image_value[0]['title'] = $image_value[0]['alt'];
  25.   $form_state->setValue('field_image', $image_value);
  26.   #multiple field field_image_gallery
  27.  $image_value = $form_state->getValue('field_image_gallery');
  28.   foreach ($image_value as $item) {
  29.    
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement