0

I've created a module so users can insert a link.

When users will insert their link, i wish to add a suffix to the link.

For exemple : they will add a link > "www.test.com"

I want to be able to add to the link once they hit save "http://"

So the link will be http://www.test.com

When i do :

$form['attributes']['href']['#prefix'] = 'prefix';

I get : prefix

What's the best way to do it without using $form['attributes']['href']['#default_value']

Many thanks,

1 Answer 1

3

As you want the suffix to be added when the save button is added you need to add a custom submit handler. In the hook_form_alter do something like $form['actions']['submit']['#submit'][] = 'mymodule_form_submit'; and in the submit function mymodule_form_submit save the value you want.

function mymodule_form_submit(&$form, FormStateInterface $form_state) {
  $entity = $form_state->getFormObject()->getEntity();
  // Get the value of your field
  $my_val = $entity->get('field_name')->getValue();
  // Set new value
  $entity->set('field_name', 'http://' . $my_val);
  //Save the entity
  $entity->save();
}

Your Answer

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

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