Как автоматически создать обработчик электронной почты по умолчанию для веб-формы при ее создании

use Drupal\webform\WebformInterface;

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function MYMODULE_webform_insert(WebformInterface $webform) {
  /** @var \Drupal\webform\WebformHandlerInterface $webform_handler_manager */
  $webform_handler_manager = \Drupal::service('plugin.manager.webform.handler');

  // Create a \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler.
  $webform_handler = $webform_handler_manager->createInstance('email');

  // Configuration copied from exported webform.webform.contact.yml
  $webform_handler->setConfiguration([
    'id' => 'email',
    'label' => 'Email',
    'handler_id' => 'email',
    'status' => TRUE,
    'weight' => 0,
    'settings' => [
      'states' => ['completed'],
      'to_mail' => '[webform_submission:values:email:raw]',
      'to_options' => array(),
      'cc_mail' => '',
      'cc_options' => array(),
      'bcc_mail' => '',
      'bcc_options' => array(),
      'from_mail' => 'default',
      'from_options' => array(),
      'from_name' => 'default',
      'subject' => '[webform_submission:values:subject:value]',
      'body' => '[webform_submission:values:message:value]',
      'excluded_elements' => [],
      'html' => TRUE,
      'attachments' => FALSE,
      'debug' => 0,
      'reply_to' => '',
      'return_path' => '',
    ],
  ]);

  // Must update original id to prevent the below error.
  // Drupal\Core\Config\ConfigNameException: The machine name of the 'Webform'
  // bundle cannot be changed in
  // Drupal\Core\Config\Entity\ConfigEntityBundleBase->preSave()
  $webform->setOriginalId($webform->id());

  // Add handle to the webform, which triggers another save().
  $webform->addWebformHandler($webform_handler);
}