0

I'm trying to modify a custom block form with hook_form_alter .In its validate function I have a case where one of the field value needs to be unset. For modifying its value I tried with

$form_state->setValue('field_name', '') /

$form['field_name']['#default_value'] = '' /

$form['field_name']['#value'] = ''

But none of these approach seems to work. Below is my code -

function coolmodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    //create or edit form
    if('block_content_coolblock_form' === $form_id || 'block_content_coolblock_edit_form' === $form_id) {
      $form['#validate'][] = 'coolblockvalidate';
    }
}

function coolblockvalidate(array &$form, FormStateInterface &$form_state) {
    //trying to unset the field value here, which is not working.
    $form_state->setValue('field_mycoolfield', '');

    //tried these approach too with no luck.
    //$form['field_mycoolfield']['#default_value'] = '';
    //$form['field_mycoolfield']['#default_value'] = '';

}

I'm very interested in knowing why the field value can't be changed. I tried the same with a custom form alter and it works.

Any ideas/ suggestions would save me. Thanks.

2

3 Answers 3

1

Got this working by adding a submit handler.

Below is the code -

function coolmodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  //create or edit form
  if('block_content_coolblock_form' === $form_id || 'block_content_coolblock_edit_form' === $form_id) {
    //extra submit handler that will execute after default submit
    $form['actions']['submit']['#submit'][] = 'coolmodule_coolblocksubmit';
  }
}


function coolmodule_coolblocksubmit(array &$form, FormStateInterface &$form_state) {

  $entity = $form_state->getFormObject()->getEntity();
  // unset field.
  $entity->set('myfield', '');
  // Saving the entity.
  $entity->save();
}
0

You can unset the field using the Hook_preprocess_hook something like the following.

/**
 * Implements hook_preprocess_HOOK().
 */
function coolmodule_preprocess_block(&$variables) {
  if (
    isset($variables['elements']['content']['#block_content'])
   && $variables['elements']['content']['#block_content']->bundle() == 'your_custom_block_type'
  ) {
    if (isset($variables['content']['field_custom_field_test'])){
      //- set field to empty value
      $variables['content']['field_name'] = [];
      //- Or Unset the field totaly
      unset($variables['content']['field_name']);
    }
  }
}
1
  • Thanks for the suggestion. But I would like to change the value on submit rather than preprocessing the values.
    – wilNev
    Commented Oct 12, 2018 at 10:28
0

You does not need to saved entity multiple time.

function coolmodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  //create or edit form
  if('block_content_coolblock_form' === $form_id || 'block_content_coolblock_edit_form' === $form_id) {
    //extra submit handler that will execute after default submit
    array_unshift($form['actions']['submit']['#submit'] , 'coolmodule_coolblocksubmit');
  }
}


function coolmodule_coolblocksubmit(array &$form, FormStateInterface &$form_state) {

  $entity = $form_state->getFormObject()->getEntity();
  // unset field.
  $entity->set('myfield', '');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.