Drupal 8 CiviCRM Create user invalid session key - session-cookies

I have a custom module where i create custom registration forms for drupal users with different roles. It was working good till i installed CiviCRM
When a form is submitted, it says:
Sorry, due to an error, we are unable to fulfill your request at the moment. You may want to contact your administrator or service provider with more details about what action you were performing when this occurred.
We can't load the requested web page. This page requires cookies to be enabled in your browser settings. Please check this setting and enable cookies (if they are not enabled). Then try again. If this error persists, contact the site administrator for assistance.Site Administrators: This error may indicate that users are accessing this page using a domain or URL other than the configured Base URL. EXAMPLE: Base URL is http://example.org, but some users are accessing the page via http://www.example.org or a domain alias like http://myotherexample.org.Error type: Could not find a valid session key.
My code:
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();
}
Checked for my Resource urls for Civi but they are correct.
Drupal: 8.4.0
CiviCRM: 4.7.28

Found it!
If you build a custom form, Civi need some fields for when a new Drupal user is created.
1: Go to Civi profiles (/civicrm/admin/uf/group?reset=1) and select the desired profile you want to include in the form. I selected "Your registration form".
Go to settings of the profile and select "used for => Drupal User Registration"
In Advanced Settings check Account creation required
2: In your custom form, implement the function: '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: In your template, add the fields with the field name from the Civi function:
{{custom_registration_form.civicrm_profile_register}}
You find the name in /modules/civicrm-drupal/civicrm.module
$form['civicrm_profile_register'] = array(
'#markup' => \Drupal\Core\Render\Markup::create($html),
'#cache' => [
'max-age' => 0,
],
);
The fields from the profile will be included in your custom form and no problems with sessions key anymore.

Related

wordpress register post type rewrite rule doesn't work

First post type :
add_action( 'init', 'create_pressrelease' );
function create_pressrelease() {
register_post_type( 'pressreleases',
array(
'labels' => array(
'name' => __( 'Press Releases' ),
'singular_name' => __( 'Press Release' )
),
'has_archive' => true,
'show_in_menu' => 'edit.php?post_type=storefronts',
'public' => true,
'map_meta_cap' => true,
'menu_position' => 4,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions' ),
'taxonomies' => array( 'post_tag' ),
'menu_icon' => 'dashicons-edit',
'has_archive' => 'pressreleases',
'with_front' => false,
'rewrite' => array(
'slug' => 'suppliers/%supplier_name%/pressreleases',
)
)
);
}
Second post type :
add_action( 'init', 'create_whitepaper' );
function create_whitepaper() {
register_post_type( 'whitepapers',
array(
'labels' => array(
'name' => __( 'White Papers' ),
'singular_name' => __( 'White Paper' )
),
'has_archive' => true,
'show_in_menu' => 'edit.php?post_type=storefronts',
'public' => true,
'map_meta_cap' => true,
'menu_position' => 4,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions' ),
'taxonomies' => array( 'post_tag' ),
'menu_icon' => 'dashicons-edit',
'has_archive' => 'whitepapers',
'rewrite' => array(
'slug' => 'suppliers/%supplier_name%/whitepapers',
)
)
);
}
I have created above post types two , I am able to get first post type correct pages and coming to 2nd post type, I am getting URL but the loads homepage content, why it is happening is there anything that i m missing to include in this both codes.suggest me i am trying since two days I have tired various methods like add_rewrite, perma_structure, need some solution on this
add flush_rewrite_rules( false ); after this register_post_type( 'straws', $args );
As i can understand you're getting wrong post link it's causing of
'rewrite' => array(
'slug' => 'suppliers/%supplier_name%/pressreleases',
)
You just need to remove both 'rewrite' parameter and SET your permalink.

Adding Form field dynamically in helper form Prestashop

I have a back office admin controller in which I am using the helper form in renderform() to generate a form and it is working form but I want a functionality to add a field in that form dynamically.
I have done this by helper list, while rendering the form I am returning a render list also, check renderAdditionalOptionsList() usage
public function renderForm() {
$id_citydelivery = (int) Tools::getValue('id_citydelivery');
$citydeliveryObj = new CityPortModel($id_citydelivery);
if (Validate::isLoadedObject($citydeliveryObj)) {
$_legends = $citydeliveryObj->legend;
$_legend_array = explode(',', $_legends);
foreach ($_legend_array as $_legend) {
$this->fields_value['legend_' . $_legend] = $_legend;
}
}
$temp = $this->fields_value;
$fields_form_1 = array(
'form' => array(
'legend' => array('title' => $this->l('Price and Delivery Destination Manager'), 'icon' => 'icon-cogs'),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Country'),
'name' => 'id_country',
'required' => false,
'lang' => false,
'options' => array(
'query' => $this->getCountries(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'text',
'label' => $this->l('Portname'),
'name' => 'portname',
'size' => 255),
array(
'type' => 'hidden',
'name' => 'id_citydelivery'
),
array(
'type' => 'text',
'label' => $this->l('Per m3'),
'name' => 'perm3',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Service charges'),
'name' => 'servicecharges',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Insurance'),
'name' => 'insurance',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Inspection'),
'name' => 'inspection',
'size' => 255),
array(
'type' => 'text',
'label' => $this->l('Certificate'),
'name' => 'certificate',
'size' => 255),
array(
'type' => 'checkbox',
'label' => $this->l('Service legend'),
'name' => 'legend',
'required' => false,
'lang' => false,
'values' => array(
'query' => $this->getLegends(),
'id' => 'value',
'name' => 'name'
)),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Active')), array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Inactive')))),
),
'submit' => array(
'title' => $this->l('Save'),
'type' => 'submit'
),
'buttons' => array(
array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite($this->name),
'title' => $this->l('Cancle'),
'icon' => 'process-icon-cancel'
)
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->toolbar_scroll = true;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitAddcitydelivery';
$helper->currentIndex = AdminController::$currentIndex;
$helper->token = Tools::getAdminTokenLite($this->name);
$helper->tpl_vars = array(
'fields_value' => $this->getFormValues($citydeliveryObj, $temp),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$_1 = $helper->generateForm(array($fields_form_1));
$return = $_1;
if ($this->display != 'add') {
$return .= $this->renderAdditionalOptionsList($id_citydelivery);
}
return $return;
}
public function initPageHeaderToolbar() {
if ($this->display == 'edit' || $this->display == 'add') {
$this->page_header_toolbar_btn['back_to_list'] = array(
'href' => Context::getContext()->link->getAdminLink('AdminGCardeliverycity', true),
'desc' => $this->l('Back to list', null, null, false),
'icon' => 'process-icon-back'
);
}
parent::initPageHeaderToolbar();
}
function getFormValues($citydeliveryObj, $temp) {
$arrTwo = array(
'id_citydelivery' => Tools::getValue('id_citydelivery', $citydeliveryObj->id_citydelivery),
'id_country' => Tools::getValue('id_country', $citydeliveryObj->id_country),
'portname' => Tools::getValue('portname', $citydeliveryObj->portname),
'perm3' => Tools::getValue('perm3', $citydeliveryObj->perm3),
'servicecharges' => Tools::getValue('servicecharges', $citydeliveryObj->servicecharges),
'insurance' => Tools::getValue('insurance', $citydeliveryObj->insurance),
'inspection' => Tools::getValue('inspection', $citydeliveryObj->inspection),
'certificate' => Tools::getValue('certificate', $citydeliveryObj->certificate),
'active' => Tools::getValue('active', $citydeliveryObj->active),
);
return array_merge($temp, $arrTwo);
}
protected function renderAdditionalOptionsList($id_citydelivery) {
$this->fields_list = array(
'service_name' => array(
'width' => 'auto',
'orderby' => false,
'title' => $this->l('Service Name'),
'type' => 'text',
'search' => false,
),
'service_desc' => array(
'type' => 'text',
'title' => $this->l('Service Description'),
'search' => false,
'orderby' => false,
),
'charge' => array(
'title' => $this->l('Service Charge'),
'type' => 'text',
'search' => false,
'orderby' => false,
),
'active' => array(
'title' => $this->l('Status'),
'active' => 'status',
'type' => 'bool',
'search' => false,
'orderby' => false
)
);
$linkToAdditionServiceController = Context::getContext()->link->getAdminLink('AdminAdditionalService', false);
$helperList = new HelperList();
$helperList->table = 'additional_service';
$helperList->shopLinkType = '';
$helperList->simple_header = false; //For showing add and refresh button
$helperList->identifier = 'id_additional_service';
$helperList->actions = array('edit', 'delete');
$helperList->show_toolbar = false;
$helperList->toolbar_btn['new'] = array(
'href' => $linkToAdditionServiceController . '&addadditional_service&id_citydelivery=' . $id_citydelivery . '&token=' . Tools::getAdminTokenLite('AdminAdditionalService'),
'desc' => $this->l('Add new')
);
$helperList->title = "Additional Option Manager";
$helperList->currentIndex = $linkToAdditionServiceController;
$helperList->token = Tools::getAdminTokenLite('AdminAdditionalService');
$content = $this->getListContent($id_citydelivery);
$helperList->listTotal = count($content);
return $helperList->generateList($content, $this->fields_list);
}

ZendFramework 2: No database adapter present

I have this class:
<?php
class RegisterFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
'name' => 'email1',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'email2',
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'user',
'field' => 'email',
'messages' => array(
'recordFound' => "Email already exist ... ! <br>",
),
),
),
),
));
}
}
?>
I get this error: No database adapter present. Any ideas why this happens?
It would be good if you'd read the documentation about Zend\Validator\Db\Record*. The given error message means exactly what it said. You don't provide a DB-Adapter inside the Validator.
From the DOCs:
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'users',
'field' => 'emailaddress',
'adapter' => $dbAdapter
)
);
If you want to find out how to get the DB-Adapter into your Form, i have written a Blog Article about said topic.

ZF2: 'required' => false ignored for DateSelect element

In a Zend Framework 2 application, I have the following form in my controller action method:
public function testAction()
{
$form = new \Zend\Form\Form('test');
$date = new \Zend\Form\Element\DateSelect('date');
$date->setOptions(array(
'label' => 'Date',
'min_year' => date('Y') - 10,
'max_year' => date('Y') + 10,
)
);
$date->getDayElement()->setEmptyOption('day');
$date->getMonthElement()->setEmptyOption('month');
$date->getYearElement()->setEmptyOption('year');
$form->add($date);
$form->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'submit',
'id' => 'submitbutton',
),
));
if ($this->request->isPost()) {
$data = $this->request->getPost();
$form->setData($data);
if ($form->isValid()) {
$data = $form->getData();
// more code
}
}
return array('form' => $form);
}
Now if I submit the form I get the validation message:
'The input does not appear to be a valid date'
That is correct but I would only want to know if the field is required. If I look in the source of the DateSelect element, I see a getInputSpecification() method that sets required to false by default and there also is a getValidator() method requiring a format with which the empty date does not comply.
How can I bypass validation if the input is not required (obviously, in my real form I have more elements)?
I usually use the factory pattern and you can set whether the field is required ie
$inputFilter->add($factory->createInput(array(
'name' => 'availableDate',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 20,
),
),
),
)));
refer the field required - you can set this to false

Pass different class name

I do have the following form:
'region' => new sfWidgetFormChoice(array('expanded' => true,'choices' => $region))
I get the folloing html.
<ul class="radio_list">
<li> first...
<li> second....
I want to change the name of class in the table. It shouldn't be radio_list. I want to name it by my own?
How can I do it in 'region'?
Following is not working and changes the classes of <li>:
'region' => new sfWidgetFormChoice(array('expanded' => true,'choices' => $region), array('class' => 'your_class'))
Thanks!
Gunnar
This is my complete Widget:
$this->setWidgets(array(
'recipename' => new sfWidgetFormInputText(array(), array('size' => '40', 'maxlength' => '150')),
'description' => new sfWidgetFormInputText(array(), array('size' => '40', 'maxlength' => '100')),
'ingredients' => new sfWidgetFormTextarea(array(), array('rows' => '10', 'cols' => '35')),
'preparation' => new sfWidgetFormTextarea(array(), array('rows' => '10', 'cols' => '35')),
'kis' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $kis)),
'category' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $category)),
'region' => new sfWidgetFormChoice(array('expanded' => true, 'choices' => $region)),
));
You need to use renderer_options for that, since sfWidgetFormSelectRadio overrides passed attribute with it's own option.
new sfWidgetFormChoice(array(
'expanded' => true,
'choices' => $region,
'renderer_options' => array(
'class' => 'your_class'
)
);

Resources