1

I see from the api and various posts that I can identify a specific form to change using hook_form_alter, like this: hook_form_FORM_ID_alter()

From experience, I know this can be done in the .theme file along these lines...

function MYTHEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if($form_id == 'node_pm_form' || $form_id == 'FORMIDHERE') {
    $form['actions']['submit']['#value'] = t('Send');
  }

But I have a form that occurs on different node types and I want to change the submit button label based on the node type. I tried renaming the function like this... function MYTHEME_form_alter__NODETYPE and MYTHEME_form_alter__NODETYPE__full

That didn't work. I'm thinking form_alter just doesn't accept node type in the function name, so I need some logic in the code to check for node type and condition results on that. But I don't yet know how to write that.

I forgot to mention: the node edit form ID's in my theme include the content type. But I want to alter the comment edit form based on what node type replying to. And all my comment forms have the same id.

1 Answer 1

3

To get the content type on content type forms, you can get the form object, then the node from that, and finally the bundle (content type):

$form_object = $form_state->getFormObject();
$node = $form_object->getEntity()
$content_type = $node->bundle();

When altering a comment form, to get the entity type of the entity the comment is attached to, you have to fetch the commented entity before getting the bundle.

$form_object = $form_state->getFormObject();
$comment = $form_object->getEntity();
$node = $comment->getCommentedEntity();
$content_type = $node->bundle();

So your function would look something like

function HOOK_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $comment_form_id = '[ENTER COMMENT FORM ID HERE]';
  if ($form_id == $comment_form_id) {
    $form_object = $form_state->getFormObject();
    $comment = $form_object->getEntity();
    $node = $comment->getCommentedEntity();
    $content_type = $node->bundle();
    if ($content_type == $some_content_type) {
      // Do stuff.
    }
  }
}
4
  • Thanks! Any tips on where to put that in the "function MYTHEME_form_alter" in the question? I'm afraid I only understand a tiny bit of the logic/syntax.
    – aharown07
    Commented Feb 22, 2023 at 23:21
  • 1
    I added some extra context.
    – Jaypan
    Commented Feb 22, 2023 at 23:36
  • That's great, thanks. May be a few days before I can circle back to it, but this looks exactly like what I needed.
    – aharown07
    Commented Feb 23, 2023 at 23:30
  • Yes, that's exactly the way. thanks! For the benefit of other novices: replace $some_content_type with the machine name of your content type, in single quotes. Like: 'article'
    – aharown07
    Commented Mar 2, 2023 at 3:48

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.