Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Drupal\Core\Form\FormStateInterface;
- /**
- * Implements hook_form_BASE_FORM_ID_alter() for node forms.
- */
- function custom_module_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
- $node = $form_state->getFormObject()->getEntity();
- if ($node->getType() != 'article' && $node->getType() != 'bookmark') {
- return;
- }
- #hide single field field_image title
- $form['field_image']['widget'][0]['#title_field'] = FALSE;
- array_unshift($form['actions']['submit']['#submit'], 'custom_module_form_submit');
- #hide multiple field field_image_gallery
- for ($i = 0; $i <= $form['field_image_gallery']['widget']['#file_upload_delta']; $i++) {
- $form['field_image_gallery']['widget'][$i]['#title_field'] = FALSE;
- }
- }
- /**
- * Custom submit handler.
- */
- function custom_module_form_submit(&$form, FormStateInterface $form_state) {
- #copy image alt to title for a single field_image
- $image_value = $form_state->getValue('field_image');
- $image_value[0]['title'] = $image_value[0]['alt'];
- $form_state->setValue('field_image', $image_value);
- #copy image alt to title for a multiple field field_image_gallery
- $field_value = $form_state->getValue('field_image_gallery');
- foreach ($field_value as &$item) {
- $item['title'] = $item['alt'];
- }
- $form_state->setValue('field_image_gallery', $field_value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement