Hi Александр, great question again! We are supposed to use $this->t() in order to use the services that are injected for us just by extending the FormBase class (api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormBase.php/class/FormBase/8.2.x). This way the whole quality of the code is better and testable. In a simple example - there is no difference, but in the general case, it is a good practice to either extend a base class in order to have some services and use them or inject the services that you need and use them. Just using t() is possible because of Drupal 7 legacy, but is a bad practice for Drupal 8 projects. Passing by reference is kind of a requirement for almost all Drupal form functions, except buildForm. That's I think because we have the hook_form_alter hook in Drupal and if you want to alter the form at some state by another module of yours - it should be passed by reference because in hook_form_alter we are of course not returning anything. here is the documentation of hook_form_alter: api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.5.x here is your question, answered by someone on drupal.org: www.drupal.org/forum/support/module-development-and-code-questions/2012-12-25/why-we-pass-arguments-with-reference Cheers and have a nice day! Nikolay
Drupal Up - Drupal 8 Video Tutorials thanks for the fact that my questions are interesting for you and you give answers for it. Can you please discover the in you video: services, dependency injections and event subscribers ??
Great tutorials. Keep it up!!! I have two questions regarding this 1)The message is shown in result_message or my_top_message. Also I want to show the message beside the second text box and not on top 2)How can I show the result for mouseout/mouseleave option and not by clicking a button?
Hi Soumya Banerjee, If you check the code here github.com/drupal-up/drupalup_simple_form/blob/master/src/Form/SimpleAjaxForm.php You will see that setMessage() method with the help of HtmlCommand is setting the result in an element with class "result_message" - it could be basically any element on the page it is not restricted to the elements that we create. For your second question, you can use something like this and then implement the callback function as shown in this video $form['textfieldname'] = array( '#type' => 'textfield', '#title' => t('Your field'), '#ajax' => [ 'callback' => '::fetchApiData', 'event' => 'change', 'prevent' => 'click', ], ); have a look at the docu here www.drupal.org/docs/8/api/javascript-api/ajax-forms In the future I am planning to create a video also about other similar problems. Cheers!
Hi Алексей, yes, sorry but I don't speak Russian ... the end code of this video you still may find here though -> github.com/drupal-up/drupalup_simple_form/blob/master/src/Form/SimpleAjaxForm.php Also if you ask any question in Russain - I will try to answer it. Cheers and have a nice day!
Thank you so much. Your videos are very helpful :-)
Hi Nikolay, what is the reason to use $this->t(Calculate) instead t(Calculate)?
And what is reason to use &$form instead $form?
Hi Александр,
great question again!
We are supposed to use $this->t() in order to use the services that are injected for us just by extending the FormBase class (api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormBase.php/class/FormBase/8.2.x). This way the whole quality of the code is better and testable. In a simple example - there is no difference, but in the general case, it is a good practice to either extend a base class in order to have some services and use them or inject the services that you need and use them. Just using t() is possible because of Drupal 7 legacy, but is a bad practice for Drupal 8 projects.
Passing by reference is kind of a requirement for almost all Drupal form functions, except buildForm. That's I think because we have the hook_form_alter hook in Drupal and if you want to alter the form at some state by another module of yours - it should be passed by reference because in hook_form_alter we are of course not returning anything.
here is the documentation of hook_form_alter:
api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.5.x
here is your question, answered by someone on drupal.org:
www.drupal.org/forum/support/module-development-and-code-questions/2012-12-25/why-we-pass-arguments-with-reference
Cheers and have a nice day!
Nikolay
Drupal Up - Drupal 8 Video Tutorials thanks for the fact that my questions are interesting for you and you give answers for it.
Can you please discover the in you video: services, dependency injections and event subscribers ??
Great tutorials. Keep it up!!!
I have two questions regarding this
1)The message is shown in result_message or my_top_message. Also I want to show the message beside the second text box and not on top
2)How can I show the result for mouseout/mouseleave option and not by clicking a button?
Hi Soumya Banerjee,
If you check the code here github.com/drupal-up/drupalup_simple_form/blob/master/src/Form/SimpleAjaxForm.php
You will see that setMessage() method with the help of HtmlCommand is setting the result in an element with class "result_message" - it could be basically any element on the page it is not restricted to the elements that we create.
For your second question, you can use something like this and then implement the callback function as shown in this video
$form['textfieldname'] = array(
'#type' => 'textfield',
'#title' => t('Your field'),
'#ajax' => [
'callback' => '::fetchApiData',
'event' => 'change',
'prevent' => 'click',
],
);
have a look at the docu here www.drupal.org/docs/8/api/javascript-api/ajax-forms
In the future I am planning to create a video also about other similar problems.
Cheers!
Superb!!! Thanks a ton
Thank you! this video is very useful
make a video to insert images into database with custom form
i need ajax dropdown pls share
I want to insert and edit values of form. Saving values in db
It shows me page not found error while redirecting through the path name
supporting AJAX in D8 seems to have gotten more complicated.
А по русски нельзя? Американе, блин.
Hi Алексей,
yes, sorry but I don't speak Russian ... the end code of this video you still may find here though -> github.com/drupal-up/drupalup_simple_form/blob/master/src/Form/SimpleAjaxForm.php
Also if you ask any question in Russain - I will try to answer it.
Cheers and have a nice day!
@@Drupal-up можливо українською мовою буде ?