Advertisement
idflorin

code for hiding of the 'Title' field and for copying the 'Alternative text' to the 'title' attribute

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