Silverstripe Gridfield Extensions - TextFields are shown as Textarea - silverstripe

Perhaps someone can help me. I've just installed the Grid Field Extensions Module for Silverstripe (https://github.com/ajshort/silverstripe-gridfieldextensions) because I need inline editing/adding. It works, but simple TextFields are shown as textareas and not as a simple textfield.
Can someone tell me how to change that?

The module attempts to automatically work out what field would be best for your variable. It will create a DropdownField for an Enum variable, TextareaField for a Text varialbe and so on.
If you don't want to manually set the field types for each variable that you want to be editable inline you need to change your variables a little.
TextareaField is the field set for Text variables.
TextField is the field set for Varchar variables.
For any variables that you want to be a TextField instead of a TextareaField change it's type from Text to Varchar(255) (or however large a character limit you need).
Otherwise you can manually set the fields using
setDisplayFields as described in the documentation.
$grid->getConfig()->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array(
'FirstField' => function($record, $column, $grid) {
return new TextField($column);
},
'SecondField' => function($record, $column, $grid) {
return new TextField($column);
},
// ... etc for each field you want to be editable
));

Related

How can I add a paragraph (as default) in the node form by using hook_form_alter in Drupal 8?

I am trying to add a bunch of different empty paragraphs of different types, to a entity reference revisions field, everytime a node of a certain content type is created.
I DON'T want to use the contrib module "default paragraphs" for this, because I need to use a certain form widget here, and default paragraphs is also achieved by a widget.
What I tried so far:
function myModule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
$paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
'type' => 'tab_features'
]);
$paragraph->save();
$form['field_tabs']['widget'][0]['target_id']=$paragraph->id();
$form['field_tabs']['widget'][0]['target_revision_id']=$paragraph->getRevisionId();
return $form;
}
$field_tabs is my entity reference revisions field.
'tab_features' is the paragraphs type I want to add.
I guess there should be a method that can be used in the form or form widget to add a paragraph to the form, like someone already clicked the button to add it. I want to avoid to actually trigger this via Javascript if possible. Anybody knows how to do this in form_alter?
In a project I'm working on, we have done something like this:
//get the body field
$field = $entity->get('field_em_p_body');
$paragraph = Paragraph::create([
'type' => 'em_section', // Paragraph type.
]);
$paragraph->isNew();
$paragraph->set('YOUR_FIELD', 'SOMETHING');
$field->appendItem($paragraph);

TypeRocket - Conditional fields

I've got a Typerocket form (within Wordpress) and I'm trying to make a conditional field.
Like if the Type field is Image, show and Image field afterwards and if the Type field is Quote have a text (editor) field afterwards.
I can't find anything about Conditional fields in Typerocket's doc.
Here's what I got right now.
echo $form->repeater('pj_images_du_projet')->setLabel("Images du projet")->setFields([
$form->select('Type')->setOptions([
"Image"=>'image',
"Vidéo"=>'video',
"Témoignage"=>'temoignage'
]), //Type is what chooses what fields are below. Options are Image, Video or Testimonial
$form->image('image')->setLabel('Image'), //image only
$form->text('video')->setLabel('Vidéo'), //video only
$form->toggle('autoplay')->setLabel('Autoplay'), //video only
$form->textarea('temoignage')->setLabel('Témoignage'), //temoignage only
$form->text('auteur')->setLabel('Auteur'), //temoignage only
]);
Created a JS that does the work.
JS
jQuery('body').ready(function(){
jQuery("body").on('change','.is-conditional-selector',function(){px_redoConditionals();})
px_redoConditionals();
})
function px_redoConditionals(){
jQuery('body').find('.is-conditional-selector').each(function(){
var repeater = jQuery(this).parent().parent().parent();
console.log(repeater.attr('class'));
repeater.find('.is-conditional').removeClass('px-show');
//console.log(jQuery(this).val());
repeater.find('.is-conditional.is-conditional-'+jQuery(this).val()).addClass('px-show');
})
}
CSS
.is-conditional:not(.px-show){
display:none !important;
}
And then I add this to the select that changes which section is shown
->setAttribute('class','is-conditional-selector')
And this on the fields that need to be shown or hidden
->setAttribute('class','is-conditional is-conditional-[value of the select to show this]'),

Set easyadmin_autocomplete Select Null by default. Symfony

I would like to know if there is any way to put null value by default in the easyadmin_autocomplete select.
The first item in the list is selected and filled with data from database but i need a null value by default and setted automaticly. The goal is a first result point to an empty one (Choose one...).
Do you know how to do it?
Can you think of any way combining the options defined below?
#easy_admin.yml
Product:
class: App\Entity\Product
controller: App\Controller\ProductController
new:
fields:
- { property: 'category', label: 'Category', type: 'easyadmin_autocomplete', type_options: { class: 'App\Entity\Category' } }
}
In this example, one select has a placeholder with the text 'Any' (Ninguno). I need to know why is handling them different.
These are the options defined for the attr fields -> type_options of the yml:
action
allow_extra_fields
allow_file_upload
attr
auto_initialize
block_name
by_reference
class
compound
constraints
csrf_field_name
csrf_message
csrf_protection
csrf_token_id
csrf_token_manager
data
data_class
disabled
empty_data
error_bubbling
error_mapping
extra_fields_message
help
help_attr
inherit_data
invalid_message
invalid_message_parameters
label
label_attr
label_format
mapped
method
multiple
post_max_size_message
property_path
required
translation_domain
trim
upload_max_size_message
validation_groups
If setting your default value would be a solution for you (like zero), service listener might be an answer:
// You need to add this listener yourself:
class ProductServiceListener
{
...
// you can manipulate entity in this level as you wish:
public function preUpdate(LifeCycleEventArgs $args)
{
// You will focus to Product entity, so block others:
if (!(get_class($entity) == 'App\Entity\Product')) {
return;
}
// Set whatever default value you want if it's null:
if($entity->getCategory() == null) {
$entity->setCategory(0); // Zero, as an example.
}
There is no way to set a placeholder like value for easyadmin_autocomplete.
The normal way for a choice type is to use the placeholder option as described in the symfony documentation. But easyadmin_autocomplete does not extend that type and it is a standalone type. Can't you use a choice type or entity type with the placeholder option. The only need for the easyadmin_autocomplete type is if there are a lot of entities and it will slow the application if all are loaded on the page.
My best bet is that you can extend the easyadmin_autocomplete. Because the easyadmin_autocomplete uses EntityType you can add the option for placeholder in the configureOptions method in the extended type. That will delegate the option to the EntityType but even then it is not displayed in the html because of the select2 javascript which also needs to be modified.
If it is a must I recommend the way to extend the type, add the option and also add custom javascript to easyadmin that will handle the new type and add the placeholder option. You can see the select2 documentation on how to set the placeholder.
But if you can use the entity or choice type for your select it is the preferred solution.

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.

Symfony not editable form field

I am new to Symfony, and Im wondering whether there is a build in way to show a not editable input field within a form.(not hidden, not editable, so it just appears in a box)
I am using echo_field and doctrine model.
Just set the read_only attribute to true when you make the form.
$builder->add('dueDate', 'text', array('read_only' => true));
http://symfony.com/doc/master/reference/forms/types/text.html#read-only

Resources