Render a field multiple times in Twig and Symfony - symfony

I need two inputs so that the user can choose from
Controller
$etud = new Etudiant();
$form=$this->createFormBuilder($etud)
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))->getForm();
if ($form->isValid()) {
// ... maybe do some form processing, like saving the Task and Tag objects
}
return $this->render('inscriptionBundle:Default:authentification.html.twig', array(
'modif' => $form->createView(),
));
How can I do it?

I'm almost sure you want a ChoiceType/EntityType field with multiple and expanded options as true.
It should be something like this:
$form->add('filierechoisit', EntityType::class, array(
# query choices from this entity
'class' => 'inscriptionBundle\Entity\filieres',
# use the filieres.libelle_filiere property as the visible option string
'choice_label' => 'libelle_filiere',
# used to render a select box, check boxes or radios
'multiple' => true,
'expanded' => true,
));

You are mixing the form processing and form rendering. If you want user to choose which way he enters data - you do not want to process this data in two different ways until these are two independent fields.
You should just have one
->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))
call to add field processing and left all rendering for the front-end side. You could use some JS, or API there, or in simple case, just override Twig template for that field
http://symfony.com/doc/current/cookbook/form/form_customization.html
You could render your own widget for your form here, allowing user to do some html stuff to change input.
Currently, making two add with identical names call just makes the second one override the first.

Related

Changing Type from Content To Global or VDC Drupal

I'm using Drupal 7 with the view_database_connector module. I'm currently working with a view that consists of a table displaying database information. My goal is to have a field with action buttons corresponding to each row, such as delete.
I am not allowed to use global php as a field.
I've attempted to make a custom module following this. I can currently use this module on content tables, however, when I try to use it on my view_database_connector table, I'm unable to add it as a field, since it's not apart of the same group.
Here's where I set up the information for making the action:
function mymodule_views_data_alter(&$data) {
// debug($data['node']);
$data['node']['actions'] = array(
'title' => t('Actions'),
'help' => t('Clickable links to actions a user may perform on a Node.'),
'field' => array(
'handler' => 'mymodule_views_handler_field_actions',
'group' => 'Content',
'click sortable' => FALSE,
),
);
}
I've tried deleting Content, changing it to global, and changing it to the VDC type, but none of that changes it into Global or VDC.
Alternatively, if there's an easier way just to hook into a field that has a button to run my code which will download a file, that could circumvent this issue.
You don't have to create this feature on your own - it already exists in views. There is a field type with actions as delete.
Other option is to use option called "rewrite value of this field" (or similar). With it you can make your field from views interface, and totally rewrite it's output. You can use token and some of them for sure contains node id, so you can use it to generate link to node deleting/editing/viewing pages.

Symfony2 personnalisation every attr radio buttons

I can not find the opportunity to customize the attributes on radio buttons separately, because I want to insert a title tag and data-toggle="tooltip" to display a tooltip on every radio buttons
$builder
->add('type', 'choice', array(
'choices' => array(
'0' => 'Demande', // Here personnalise attr
'1' => 'Recherche', // Here personnalise attr
),
'expanded' => true,
))
I tried with the attr option, but it puts on all the radio buttons ... Or should that EVERY radio button is a different attr.
Thank you !
The expanded choice list in Symfony is actually a form with children of either type radio or type checkbox (depending on the multiple option being false or true respectively).
In my opinion, the best way to solve this, is create a custom type, and do your own templating of the type. This usually is the most clear to the fellow developer and is consistent (and much easier than you might think).
The type should look like this:
class MyType extends AbstractType()
{
public function getName()
{
return 'tooltip_choice';
}
public function getParent()
{
return 'choice';
}
}
Add this class to your service definitions and tag it as a form type (see docs on this).
Now, the form templating layer will search for a block named tooltip_choice_row (for the row of the element), tooltip_choice_widget for the widget of the element. If you inspect the default form templates, you can easily dissect how the choice list rendering works in the default implementation. You can either copy and paste it to your custom form theming and override.
The downside of this solution is that it costs a little bit more code, but you can much easier extend your types to include the tooltip itself, and pass that as an option to your form type as well.
Another solution would be to override the radio_widget and the checkbox_widget blocks in your form template, and check if the parent is a choice block. You can find out what exact variables you need by dumping the _context variable in your template, it shows exactly what values are available for you to check on.

Sonata admin, editable field with choice

I'm using sonata admin and there is an option 'editable' => true for edit directly inline datas on the list view.
If my field is a text, it's ok, i can click, edit the text and save directly on the table.
But i don't want an input type="text" when i click on the field, but a list, i'm trying something like :
->add('etat', null, array('editable' => true), 'choice', array(
'choices' => array(
'Brut' => 'Brut',
'NRP' => 'NRP',
)
))
But no effetc.. is this possible ?
Since Sonata Admin Bundle 2.2 choice accepts the "editable" parameter in the list view. You use it like that:
$listMapper->add('etat', 'choice', [
'choices'=>['Brut'=>'Brut', 'NRP' => 'NRP',],
'editable'=>true,
]);
Doc: https://sonata-project.org/bundles/admin/2-2/doc/reference/field_types.html
It's possible for scalar values only. Hear some doc
http://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
Well it's not possible at the moment and won't be possible as I can guess in the near future. Your own realization should be written.

Drupal form with custom ID

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.
P.S:drupal version - 6.x
Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.
I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:
function voci_comment_form($form, &$form_state, $cid) {
$form['#attributes']['id'] = 'voci-comment-form-' . $cid;
$form['#attributes']['class'][] = 'voci-comment-form';
$form['body'] = array(
'#title' => 'Post a comment',
'#type' => 'textarea',
'#resizable' => FALSE,
'#rows' => 1,
);
$form['comment'] = array(
'#type' => 'submit',
'#value' => 'Comment',
);
foreach ($form as $k => &$element) {
$k = str_replace('_', '-', $k);
$element['#attributes']['id'] = "edit-$k-$cid";
$element['#attributes']['class'][] = "edit-$k";
}
return $form;
}
This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.
It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).
Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.
This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).
If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.
If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

Drupal 6 passing variables from Forms to Content, how to?

I created a BLOCK (left) with this simple form.
Now I want to PROCESS and DISPLAY results on PAGE (center)
How can I do it ?
inputs:
name = James
surname = Bond
output I want :
<div style="color:red">Welcome, James Bond</div>
here is a BLOCK which i wrote and works.
<?php
echo drupal_get_form('myForm');
function myForm($form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 20,
'#maxlength' => 10
);
$form['surname'] = array(
'#type' => 'textfield',
'#title' => t('Surname'),
'#size' => 20,
'#maxlength' => 10
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
function myForm_submit($form,&$form_state)
{
//??
};
Now I need to display the output :).
Please don't suggest to use VIEWS or any other addon.
I want to learn Drupal from Inside out. Not the other way around ;)
Well, it depends a bit on how you want to do things. Since you are learning how to make a drupal module, you might want to start with an implementation of hook_menu(). This hook is used to define menu items, which basically means that you can register urls with that function. Going that route you can:
Implement hook_menu()
A general way of handling redirects is using drupal_goto(). However, in this case it is much more fitting to use the $form_state['redirect'] as Henrik explained in his comment.
For the url you are redirecting to, you should have a call back function which is where you put your logic, the way you setup the hook_menu and the callback function will determine how you get your variables available. You probably want to look into the arg() function which is what generally is used to get the values from the url.
Run the user input through a filter to make sure that they haven't posted nasty stuff like script tags ect, use check_plain
return a theme function, alternatively make your own, look at theme() and hook_theme()
There are quicker ways to do this, but doing it this way, you will generate urls for every search result that drupal can cache, which is nice. Also not being dependent on the post parameters people can bookmark the search results
Another thing is that you might want to put some basic validation to your form. That would be a good practice to learn. That would look something like this:
/**
* Validation handler for myForm.
*/
function myForm_validate($form, &$form_state) {
$name = $form_state['values']['name'];
// do some checks to $name.
if ($error) {
form_set_error('name', t('error message to be displayed, showing the value of the field: #name', array('#name' => $name);
}
};
You could implement AHAH in your form, and specify an element inside your page's content area as the 'wrapper' (the element in which the results of the callback function will be placed). But, you would need to understand the excellent advice of Mr. Opel before you even attempt it.
How about using drupal_set_html_head() to write a string of script to the head section of the page? I am doing this on specific pages (getting user latitude and longitude and passing them into my gMap function), but I am interested in dong the same thing directly from hook_form_submit(). I have made a few tries at it, and I am obviously outputting the script string but calling the function from submit doesn't seem to work.
if the submit function creates a page full of html output; what is the best way to pass this to a page callback? i doubt that passing as an arg on the url would work.
i stuffed into a session var but maybe a better way?

Resources