Score:0

How to programatically publish content after form validation

mx flag

I have a content type of which its status is set to unpublished and is disabled. A site manager needs to publish the content after approval.

The site manager wants to skip this validation process when creating the same content and immediately publish the content after submitting the creation form.

In my _form_validate function:

  if (in_array('site_manager', $user->getRoles())) {
    if ($given_email == '[email protected]') {
      $form_state->setValue('status', ['value' => True]);
    }
  }

And the value in $form_state is properly set to: True when inspecting with XDebug. But it does not publish the content automatically. How can I achieve this?

Score:2
de flag

You're trying to combine what should be two steps, into one. Validation handlers are for validation, so skipping the validation happens in the validation handler. Processing, which in this case setting is setting the node to published, happens in the submit handler.

In the validation handler:

if (!in_array('site_manager', $user->getRoles()) || $given_email !== '[email protected]') {
    // Form validation for users other than the site manager goes here.
  }
}

In the validation handler (assuming this is a node form):

if (in_array('site_manager', $user->getRoles()) && $given_email == '[email protected]') {
    $node = $form_state->getFormObject()->getEntity();
    $node->setPublished()
      ->save();
  }
}

However, this is not how I would go about this. Hard coding an email address into this makes it difficult to change if the email address changes in the future. Instead, I would:

  1. Create a new role.
  2. Create a new permission in [MODULE].permissions.yml (don't forget to clear the registry after):
bypass my form validation:
  title: 'Bypass my form validation'
  description: 'Bypasses validation of My Form'
  restrict access: true
  1. Assign the new permission to the role created in step one.
  2. Assign the role to the admin user who is to bypass validation.
  3. Change the validation code to this:
if (!\Drupal::currentUser()->hasPermission('bypass my form validation')) {
    // Form validation for users other than the site manager goes here.
  }
}
  1. Change the submit handler to this:
if (\Drupal::currentUser()->hasPermission('bypass my form validation')) {
  $node = $form_state->getFormObject()->getEntity();
  $node->setPublished()
    ->save();
  }
}

This gives a more dynamic solution, in that the user can change their email address in the system, without having to update the code for the new email address. It also allows for adding additional users that can be skipped in the future if the need comes up, simply by assigning them the role created in step one.

I sit in a Tesla and translated this thread with Ai:

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.