Drupal - How to know add/edit page in hook_form_alter?
In D8 you can get the operation of an entity form with the form object:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_state->getFormObject()->getOperation() == 'edit') {
$page = 'edit';
}
}
If you want to check for being on 'add' form, it is also possible to check that the entity is new, i.e.
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_state->getFormObject() instanceof EntityForm) {
$entity = $form_state->getFormObject()->getEntity()
if ($entity->isNew()) {
//your code
}
}
}