0

I have created a new form mode (in the UI at admin/structure/display-modes) for a custom entity (form mode is entitled apply) and added apply to the form handler classes in the entity class.

The form is built and returned in a Controller, and specifies the form mode.

$form = $this->entityFormBuilder()->getForm($entity, 'apply');

Now I would like to be able to determine from a hook_form_alter whether this form is being displayed using the apply form mode, but I cannot work out how that is available from $form or $form_state.

3

You can try EntityFormInterface::getOperation:

function mymodule_form_FORM_ID_alter(&$form, FormStateInterface $form_state) {
  // only continue if this is for form mode "apply"
  if ($form_state->getFormObject()->getOperation() !== 'apply') {
    return;
  }

  // do stuff

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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