php Drupal 8以编程方式创建用户

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Drupal 8以编程方式创建用户相关的知识,希望对你有一定的参考价值。

<?php

$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

// Required
$user = \Drupal\user\Entity\User::create();
$user->setPassword($form_state->getValue('pass'));
$user->setEmail($form_state->getValue('mail'));
$user->setUsername($form_state->getValue('name'));//This username must be unique and accept only a-Z,0-9, - _ @ .
$user->enforceIsNew();

// Optional
$user->set("init", 'mail');
$user->set("langcode", $language);
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
$user->activate();

//Save user account
$user->save();

// Sending "no approval required" email to user
_user_mail_notify('register_no_approval_required', $user);

// login user immediately
user_login_finalize($user);

// Showing message if needed
drupal_set_message(t('Registration successful. You are now logged in.'));

// Redirect if needed
$form_state->setRedirect('');

以上是关于php Drupal 8以编程方式创建用户的主要内容,如果未能解决你的问题,请参考以下文章

php Drupal 8:以编程方式处理队列

PHP Drupal:以编程方式注销用户

drupal 以编程方式禁用或删除节点创建节点链接

drupal 7以编程方式登录用户

Drupal 以编程方式呈现登录表单

drupal 7以编程方式登录用户