Drupal submit contact form from api - drupal

Help, please, there is a task from the front on api to fill out the drupal contact form.
I have a contact form and a method, I need help with the code that will submit the data, I found this solution:
$form_state = new FormState();
$values = [
'message' => 'test',
'name' => 'test'
];
$form_state->setValues($values);
\Drupal::formBuilder()->submitForm('data_portal_contact_us', $form_state);
In the log it gives the following error:
InvalidArgumentException: The form argument data_portal_contact_us is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 197 of /www/core/lib/Drupal/Core/Form/FormBuilder.php).

Try to use The Form class Name in the $form_arg parameter of submitForm($form_arg, FormStateInterface &$form_state)
$values = [
'message' => 'test',
'name' => 'test'
];
$form_state = (new FormState())->setValues($values);
\Drupal::formBuilder()->submitForm('Drupal\module_name\Form\customModuleNameForm', $form_state);

Related

Wordpress Grabity form gform_after_submission

I have a form and i want to send the data to a third party location via endpoint and gform_after_submission.
my form was working fine, but now its not after adding a new field in the third party location (pardot).
this is my old form handlder that i used to use to send the data to Pardot:
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
$post_url = $_POST['input_9'];
$body = array(
'First Name' => $_POST['input_1'],
'Last Name' =>$_POST['input_2'],
'email' => $_POST['input_3'],
'Zip' =>$_POST['input_4'],
'Company' =>$_POST['input_5'],
'Phone' => $_POST['input_6']
);
now on my thirdparty form we added a Radio button; i added that radion on my gravity form and i need to add it to the code above, i tried that but appearently its not working since i guess gravity form radio button is somehow an array...
so can you please help me with that?
Thank you
Never mind, i found the solution:
$body = array(
'First Name' => $_POST['input_1'],
'Last Name' =>$_POST['input_2'],
'email' => $_POST['input_3'],
'Zip' =>$_POST['input_4'],
'Company' =>$_POST['input_5'],
'Phone' => $_POST['input_6']
'distant-field-name' => $_POST['field-input-name']
);

How do you modify entity references on a form in a custom ajax action in Drupal 8?

I have added a custom button to a form:
$form['actions']['autotag_content'] = [
'#type' => 'button',
'#value' => 'Autotag Content',
'#ajax' => [
'callback' => ['\Drupal\taxonomy_migrate\taggerService', 'tagContent'],
'wrapper' => ['block-adminimal-theme-content'],
'progress' => [
'type' => 'throbber',
'message' => 'Tagging content',
],
],
];
Then in the callback I want to add or remove entities from an entity reference field on the form. This would then get sent back to the browser and rerendered. I don't want the changes to be save, I just want them populated in the form and then the user can accept the changes.
For the sake of this example, I have simplified this to just demonstrate the point. I would like to add two entity references to field_tax_subjects and have the frontend form rerender. Currently, the frontend form rerenders, but doesn't reflect the changes
public static function tagContent(array &$form, FormStateInterface &$form_state) {
$node = $form_state->getFormObject()->getEntity();
$node->field_tax_subjects[] = 12345;
$node->field_tax_subjects[] = 23456;
$form = \Drupal::service('entity.form_builder')->getForm($node);
$form_state->setRebuild();
return $form;
}
my answer is just for in case your ajax is working
because in your question you have'nt full code of form
also its not clear its node form or something else
any way
If your ajax is working you only have to correct how to set value for entity reference field and term reference filed
for entity reference and term reference
public static function tagContent(array &$form, FormStateInterface &$form_state) {
$node = $form_state->getFormObject()->getEntity();
// for entity refrence
$node->field_tax_subjects[]['target_id'] = 12345;
$node->field_tax_subjects[]['target_id'] = 23456;
// for term reference
//$node->field_tax_subjects[]['tid'] = 12345;
//$node->field_tax_subjects[]['tid'] = 23456;
$form = \Drupal::service('entity.form_builder')->getForm($node);
$form_state->setRebuild();
return $form;
}
HOPE THIS HELP YOU
THANKS

How to prefill field of type EntityType from PHP

In my form, I have a field of type EntityClass:
$builder
->add(
'user',
EntityType::class,
[
'required' => false,
'label' => 'User',
'placeholder' => 'Please choose',
'choice_label' => 'email',
'choice_value' => 'id',
'class' => 'AppBundle:User',
]
)
;
This field works fine - until I try to pre-fill it from my PHP code. Then it stays empty, and only shows "Please choose".
Pre-filling looks like this:
$user = $this->userRepository->find(...);
$form->get('user')->setData($user);
But it also does not work if I call ->setData($user->getId()), or even ->setData($user->getEmail()).
So how do I prefill a field of type EntityType?
You should not prefill Form, you should prefill Model, if you need it.
$user = $this->userRepository->find(...);
$entity = new YourEntity();
$entity->setUser($user);
$form = $this->createForm(YourEntity::class, $entity);
And it's not about EntityType. It's about any Type in Symfony - there is no way to bind a default value for them. Data is binded on Model.
UPD from comment: It's not true, that Form could be used without Model. It could be used without Doctrine Entity or any other ORM (or not ORM) Entity. But they still operate with data, i.o. with model.
\Symfony\Component\Form\FormFactoryInterface has definition
public function create($type = 'form', $data = null, array $options = array());
So some kind of $data is always present when you're using Form Component.

Symfony Client & DomCrawler: Post with query string

I'm having issues attempting to POST to a script that requires a query string. I'm using the code below, but I get the same error response I would get if I requested the page without the query string or set the query variable to "".
For example http://example.com/login?validate works in the browser, but http://example.com/login?validate="" does not (page displays error)
My code:
$client = new Client();
$crawler = $client->request("POST","https://www.example.com/cgi-bin/login?validate", array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
));
Normally you would use the route like this:
$client->request('POST', '/login', array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
)
);
In your particular case, I don't think you need to add ?validate as it should be triggered when you submit the form:
$client->request('POST', 'https://www.example.com/cgi-bin/login', array(
'USERNAME' => 'TEST',
'PASSWORD' => 'TEST'
)
);
Also, make sure your field is USERNAME and not username, it is case sensitive.

Passing Form to Template using RenderWith method

I am using the Silverstripe comments module by Willr along with an implementation of DataObjects as Pages.
The comments module allows you to attach comments to Data Objects - which I have done. The problem I am having is that when I attempt to pass through customfields from the Datobject to a template using renderwith, the CommentsForm that gets passed renders the form, but does not associate any comments made through the passed form with the DataObject.
Here is my action and renderWith method on my PostsPageHolder:
public function view($request) {
$segment = $request->param('ID');
if ($obj = Post::get()->filter('URLSegment', $segment)->First()) :
switch ($obj->Type) {
case 'News-Post' :
return $this->renderWith(
array('PostsPage_view_news', 'Page'),
array(
'Object' => $obj,
'Type' => $obj->Type,
'Title' => $obj->Title,
'Entry' => $obj->Entry,
'CommentsForm' => $obj->CommentsForm
)
);
break;
...
}
Does anyone know how I can pass through the form using the RenderWith() array?
try customise(array) as shown here https://docs.silverstripe.org/en/3/tutorials/site_search/#showing-the-results
return $this->customise(array(
'Object' => $obj,
'Type' => $obj->Type,
'Title' => $obj->Title,
'Entry' => $obj->Entry,
'CommentsForm' => $obj->CommentsForm
))->renderWith(
array('PostsPage_view_news', 'Page')
);

Resources