Score:1

How can I tell, in hook_form_alter(), if a node form is for the node in its default language or a translation?

in flag

I'm implementing hook_form_BASE_FORM_ID_alter() in a custom module. I'd like to know if my form is for the node in its default language, or a translation of that original node.

Score:3
in flag

It looks like I can get it from the form's object, i.e. the form's node, and then use the node's isDefaultTranslation() method.

For example:

use Drupal\Core\Form\FormStateInterface;

function MY_MODULE_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\node\NodeInterface $node */
  $node = $form_state->getFormObject()->getEntity();
  if ($node->isDefaultTranslation()) {
    // The node form is for the node in its default language.
  }
  else {
    // The node form is for a translation of the node.
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.