0

我有一个自定义模块,我在其中为具有不同角色的 drupal 用户创建自定义注册表单。在我安装CiviCRM之前它运行良好

提交表单时,它会说:

抱歉,由于出现错误,我们目前无法满足您的要求。您可能需要联系您的管理员或服务提供商,详细了解发生这种情况时您正在执行的操作。我们无法加载请求的网页。此页面需要在您的浏览器设置中启用 cookie。请检查此设置并启用 cookie(如果未启用)。然后再试一次。如果此错误仍然存​​在,请联系站点管理员寻求帮助。

站点管理员:​​此错误可能表明用户正在使用配置的基本 URL 以外的域或 URL 访问此页面。示例:基本 URL 是http://example.org,但有些用户通过http://www.example.org或类似的域别名访问该页面http://myotherexample.org

错误类型:找不到有效的会话密钥。

我的代码:

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $account = entity_create('user');
    $account->setUsername($form_state->getValue('uname'))->setEmail($form_state->getValue('email'));
    $account->addRole('private');
    $account->activate();
    $account->save();
}

检查了我的 Civi 资源网址,但它们是正确的。

Drupal:8.4.0

CiviCRM:4.7.28

4

1 回答 1

0

找到了!

如果您构建自定义表单,Civi 需要一些字段用于创建新的 Drupal 用户。

1:转到 Civi 配置文件 (/civicrm/admin/uf/group?reset=1) 并选择要包含在表单中的所需配置文件。我选择了“您的注册表单”。转到配置文件的设置并选择“用于 => Drupal 用户注册”在高级设置中检查需要创建帐户

2:在您的自定义表单中,实现函数:'civicrm_form_user_register_form_alter'。

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $validators = array(
        'file_validate_extensions' => array('jpg jpeg png'),
    );
    $form['uname'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Username*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['organisation'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Organisation name*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['password'] = array(
        '#type' => 'password_confirm',
        '#placeholder' => t('Password*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['name'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Full Name*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['email'] = array(
        '#type' => 'email',
        '#placeholder' => t('Email Address*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );

    $form['street'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Street*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['nr'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Nr*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['zipcode'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('Zipcode*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );
    $form['city'] = array(
        '#type' => 'textfield',
        '#placeholder' => t('City*'),
        '#required' => TRUE,
        '#attributes' => array('class' => array('form-control')),
    );

    //This did the trick!
    if( function_exists('civicrm_form_user_register_form_alter') ) {
        civicrm_form_user_register_form_alter($form,$form_state,'customRegistration');
    }

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Create'),
        '#attributes' => array('class' => array('btn', 'btn-cs', 'btn-outline')),
    );
    $form['#validate'][] = array($this, 'regValidate');
    return $form;
} 

2:在您的模板中,添加具有来自 Civi 函数的字段名称的字段:

{{custom_registration_form.civicrm_profile_register}}

您可以在 /modules/civicrm-drupal/civicrm.module 中找到名称

$form['civicrm_profile_register'] = array(
'#markup' => \Drupal\Core\Render\Markup::create($html),
'#cache' => [
  'max-age' => 0,
],

);

配置文件中的字段将包含在您的自定义表单中,并且会话密钥不再有问题。

于 2018-07-11T07:55:18.573 回答