Drupal - Change value on validate form when I create or edit a block

Please insert the validate method inside the #element_validate function and then change the element value.

/**
 * Implements hook_form_FORM_alter().
 */
function MY_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'block_content_MY_BLOCK_form' ||  $form_id =='block_content_MY_BLOCK_edit_form') {
       $form['field_test']['widget'][0]['#element_validate'][] = '_custom_validate';
      }
    }

function _custom_validate(&$element, FormStateInterface $form_state, &$complete_form) {
  $form_state->setValueForElement($element,['value' => 'See the change']);
}

Tags:

Forms

8

Blocks