Render form in a Twig Template in drupal8 - drupal

Im curently using drupal 8 and I have created form using form api,
The below code under Module directory
// module/src/Form/ContributeForm
class ContributeForm extends FormBase {
public function getFormId() {
return 'amazing_forms_contribute_form';
}
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
);
$form['video'] = array(
'#type' => 'textfield',
'#title' => t('Youtube video'),
);
$form['video'] = array(
'#type' => 'textfield',
'#title' => t('Youtube video'),
);
$form['develop'] = array(
'#type' => 'checkbox',
'#title' => t('I would like to be involved in developing this
material'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
Now I need to render above vairables in twig template like below
//themes/page.html.twig
<body>
{{form.title}}
{{form.video}}
{{form.video}}
</body>
the twig will be under theme folder.Is it possible to get variable in page.html.twig file??

The best way to render and custom your form via Twig, you'd use $form['#theme'] value in your form. Example below:
module_name/scr/Form/your_form.php
public function buildForm(array $form, FormStateInterface $form_state){
.....
.....
$form['#theme'] = 'your_form_theme';
return $form;
}
module_name/form.module
function form_theme() {
$themes['your_form_theme'] = ['render element' => 'form'];
return $themes;
}
What is left is creating your custom twig and insert you form fields.
module_name/templates/your-form-theme.html.twig
<body>
{{form.field_1}}
{{form.field_2}}
{{form.field_3}}
</body>
Hope it helps you!

You can create a page by using the controller. From that, you can get the form elements.
Create a module_nameController.php file inside src/Controller folder. In that file create a class that should be extended to Controllerbase. In that file, by using form builder function you can get the form elements.

Related

Why the view block under the search field was not rendered?

I created a block based on the view with machine name search_entity_product_block:
Then I created a custom form with method someMethod which should render a block of the view below the search field:
class SearchFieldForm extends FormBase {
public function getFormId() {
return 'entity_product_admin_search_field';
}
public function someMethod($args) {
$view = [
'#type' => 'view',
'#name' => 'search_entity_product',
'#display_id' => 'search_entity_product_block',
'#arguments' => $args,
'#embed' => TRUE,
];
return $view;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['search_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Search Product'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$args = $form_state->getValue('search_field');
$this->someMethod($args);
}
}
But the block didn't render.

upload a file in a drupal 8 contributed module

I am trying to create my first drupal 8 module. in this module I have to create a new form and provide user a file uploading capability in this form. here is my form controller:
class Make2d extends FormBase {
/**
* {#inheritdoc}
*/
public function getFormId() {
return 'make2d_form';
}
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (\Drupal::currentUser()->isAnonymous()) {
return new RedirectResponse(\Drupal::url('user.page'));
}
$form['sheet_size'] = array(
'#type' => 'radios',
'#title' => t('Sheet Size'),
'#options' => array(t('10 X 10(2.99$)'), t('17 X 17(4.99$)'), t('28 X 28(5.99$)')),
);
$form['uploaded_file'] = array(
'#type' => 'file',
'#title' => t('Upload your file'),
'#required' => true
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save to Cart'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {#inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
echo '<pre>';
print_r($form_state->getvalues());
echo '</pre>';
}
/**
* {#inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
print_r($form_state['values']);
}
}
this is the result when I go to my form's page:
image of my form
then I choose a file from my computer and submit the form. but when I print_r my $form_state->getvalues() array the result is sth like this:
Array
(
[sheet_size] => 0
[uploaded_file] =>
[submit] => Drupal\Core\StringTranslation\TranslatableMarkup Object
...
you can see that [uploaded_file] is empty. and there is an error on top of the form about uploading a file. what is wrong with the form controller and file uploading.
thanks.
at last! I find it. we should use '#type' = 'managed_file' so that we let drupal to manage uploaded file. in the case we use '#type' = 'file' we must transfer file ourselves by file_save_upload().

drupal_set_title not working in module form

I´m trying to learn Drupal 8. So now, I´m "building" forms.
I created a module.
INSIDE mymodule\src\Form
I have form1.php
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class form1 extends FormBase {
public function getFormId() {
return 'myform1';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 30,
);
$form['Lastname'] = array(
'#type' => 'textfield',
'#title' => t('Your lastname'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
And WORKS...
But when I want to put a title to the page, in my case and linked to my_module.routing.yml, 127.0.0.1/form1 using
...
public function buildForm(array $form, FormStateInterface $form_state) {
drupal_set_title(t('This is a Title'));
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 30,
);
...
I receive the following ERROR:
ERROR:
Fatal error: Call to undefined function Drupal\mymodule\Form\drupal_set_title() in C:\xampp\htdocs\drupalwebsite\modules\custom\mymodule\src\Form\form1.php on line 16
An the line 16 is:
drupal_set_title(t('This is a Title'));
So the problem is with the title. I tried to resolve it but I could not.
Anyone know why?
Thanks a lot
According to https://www.drupal.org/node/2067859, drupal_set_title() was removed in D8. Have you tried the alternate method that's mentioned in that link, like this:
$form['#title'] = $this->t('This is a Title');

Ajax not working in drupal 8 custom form

I'm trying to use ajax in drupal 8 custom form. But it not work.
I've custom form, in which textfield is display with some value once the user click on the checkbox. Here is my code:-
public function buildForm(array $form, FormStateInterface $form_state) {
$form['del_name'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Delete users, by name'),
'#ajax' => array(
'callback' => array($this, 'my_user_callback'),
'wrapper' => 'del-name',
),
);
$form['name_placeholder'] = array(
'#type' => 'hidden',
'#prefix' => '<div id="del-name">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!empty($form_state->getValue('del_name')) && $form_state->getValue('del_name')) {
/*
* $process_data = some stuff
*/
$form['name_placeholder']['user_role'] = array(
'#title' => $this->t('Users role'),
'#type' => 'textfield',
'#default_value' => $process_data
);
}
}
function my_user_callback(array &$form, FormStateInterface $form_state) {
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace('#del-name', render($form['name_placeholder'])),
)
);
}
Problem is that, textfield not appear when checkbox is checked.
try this:
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
function my_user_callback(array &$form, FormStateInterface $form_state {
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand(
'#wrapper-element-id',
$array_of_form_elements
));
return $response;
);

How do I trigger a Drupal-Function with Javascript/jQuery?

I have a Checkbox with different values. When a user change the Checkbox I will trigger the Drupal-Function field_attach_update http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_update/7
I know how I check the checkbox-change with jQuery but how can I trigger the Drupal-Function then?
You'll want to check out the Form API ajax options. Specifically I think you'll want to define an ajax['callback'] function that calls field_attach_update.
<?php
function my_form_func($form, $form_state) {
$my_checkbox_val = isset($form_state['values']['my_checkbox']) ? $form_state['values']['my_checkbox'] : NULL;
$form['my_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Check me'),
'#default_value' => $my_checkbox_val,
'#return_value' => $nid, // Assuming you are working with a node, but could be any entity
'#ajax' => array(
'callback' => 'my_form_field_update_func',
'event' => 'click',
),
);
return $form;
}
function my_form_field_update_func($form, $form_state) {
if (isset($form_state['values']['my_checkbox'])) {
$node = node_load($form_state['values']['my_checkbox']);
field_attach_update('node', $node);
}
return $form['my_checkbox'];
}
?>

Resources