Symfony2 Sonata Admin IvoryCKEditor wrong render for some reason - symfony

I am trying to implement IvoryCKEditor Bundle to my SonataAdmin entities and I am witnessing some very strange errors/bugs/mistakes... I dont even know..
So when I want to render a simple textarea field and add some longtext to it I simply do something like this:
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
// ->add('id', 'hidden')
->add('name')
->add('contentEn', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('contentEs', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('status')
;
}
This works like a charm.. However if I have mapped entities and I want to show its fields I use sonata collection:
->add('translations', 'sonata_type_collection',
array(
'required' => false,
'label' => false,
),
array(
'edit' => 'inline',
'inline' => 'standard',
)
)
And in the mapped entities admin I do this again:
$formMapper
->with('Item Info')
//->add('id')
->add('product_name')
->add('description_for_lbi', 'ckeditor')
->add('short_description', 'ckeditor')
->add('long_description', 'ckeditor')
->add('conditions', 'ckeditor')
->add('language', null, array('required' => true))
->end()
;
Now here is the problem. Its seems the ckeditor form the collection is rendering in a completely different way.
The first example renders an iframe and makes the ckeditor look "clean".
In the collection ckeditor is rendering in a completely different way, no iframes.. And for the editor to show up I have to click on the field first.. And the field has no borders... I really dont know how to explain this.
So I guess my question is, why the ckeditor is rendering completely different when I am using it in a collection. Am I doing something wrong?
If you guys dont understand what I mean I can post some screens...
UPDATE
I thing the problem is here:
'edit' => 'inline',
'inline' => 'standard',
this makes the editor look bad. However if I remove these lines I get error:
Error: Maximum function nesting level of '100' reached, aborting!
This error is when I am trying to edit an object

You are completely right with your edit. inline editing in ckeditor breaks if you have 2 or more instances of the ckeditor config.
My suggestion is to drop inline editing.
For the second part, the error comes from xdebug, which cannot follow your object nesting level because it exceeds this limit.
In order to fix this (which is recommended, as limit = 100 is way too low for symfony2), please see this
article.

Related

Form of CollectionType implementing EntityType in ManyToMany relation: Duplicate record is added to database table when 'multiple' => false

I have 2 classes with many to many relationship: Foo (owning side) and Bar (inverse side). My form is CollectionType (of Foo) implementing EntityType (of Bar) (add Bar into Foo). This is some code for my form
FooType:
->add('Bars', CollectionType::class, array('entry_type' => BarType::class, 'allow_add' => true, 'allow_delete' => true,
'by_reference' => false))
BarType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text', EntityType::class,
array(
'class' => Bar::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('bar')
->orderBy('bar.text', 'ASC');
},
'choice_label' => 'text',
'by_reference' => false,
)
)
;
}
if i set 'multiple' => true, there is no problem
if i set 'multiple' => false (dont ask me why, this is a requirement from work: add a plus and minus button for the form. When plus button is click, the options from Bar.text are shown, and user can select select only one from those options. When minus button is clicked, undo the plus action), relationships are established but a duplicate record of selected option is added to Bar table.
I would like to ask:
Why does it happen? Is that the way symfony supposed to works?
Is there any fix/workaround for it?
According to this github disccussion of Symfony, you are supposed to use 'multiple' => true to make it work
I am using Symfony 4.4, doctrine bundle 2.6.3
Edit:
I would like to clarify this: my goal is to select multible options loaded from Bar->$text. But instead of selecting multiple options in one run (by setting multiple true), i would like to archieve it by adding many times (with a plus button), and each time only one option can be selected.
The reason I am doing this is that i am migrating from many to one to many to many, and I do not want to change the current UI (select one at a time) to multiple select.

Capitalize filter in symfony2 Constraint Messages

I'm new to symfony. I searched a lot especially in the symfony documentation:
http://symfony.com/doc/current/book/translation.html#translating-constraint-messages
But I found nothing about it.
I know how to use capitalize twig filter in a twig template.
http://twig.sensiolabs.org/doc/filters/capitalize.html
{{ 'my first car'|capitalize }}
But is it possible to capitalize a translation in these three scenarios:
In entities annotations Constraints Messages (this example does not work, translation is not translated if I add |capitalize)
class Author
{
/**
* #Assert\NotBlank(message = "author.name.not_blank|capitalize")
*/
public $name;
}
In a form builder (this example does not work, translation is not translated if I add |capitalize)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'user.password_confirm_mismatch|capitalize',
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
'required' => false
)
In a controller with the translator service. In this case it is not very important because I can use the php ucfirst function. But maybe there is another method more in line with symfony best practices.
//Not capitalized, any way to add twig capitalize filter in this code?
$tranlsation_test_1 = $this->get('translator')->trans('info.profile_updated_successfully',array(),null,$new_user_app_language_code);
//Capitalized but with the php ucfirst function
$tranlsation_test_2 = ucfirst($this->get('translator')->trans('info.profile_updated_successfully',array(),null,$new_user_app_language_code));
Many tanks for your advices.

A2LiX Translation Form labels options

I'm using KnpLabs/DoctrineBehaviors/Translatable and A2LiX Translation Form to translate my entities in a Symfony application. It works very well. However, when the form is rendered there is a "translations" title that I would like to delete and a "EN [Default]" text on a tab that I would like to change.
In the examples of the doc, there's a "medias" example so I imagine that we can change this text. Moreover, the tabs don't have this [Default] text. So I imagine that's possible to change them.
And this is mine:
Does anybody know how to do it? If we take a look on the form type options we don't see anything concerning the "Translations" label. For the "Default", I can't see where I should search for it.
Default template file is located at vendor/a2lix/translation-form-bundle/A2lix/TranslationFormBundle/Resources/views/default.html.twig. If you want you can specify your own template and set it in config.yml file, like this:
a2lix_translation_form:
....
templating: "#SLCore/includes/translation.html.twig"
More information can be found here.
For the "translations" title, I was able to override it adding a label to the form type just like a normal field. However, it is not possible to use a blank value. I had to use ' ' in order to override the text.
->add('translations', 'a2lix_translations', array(
'label' => ' ', --> this overrides the translations title
'fields' => array(
'name' => array(
'field_type' => 'text',
'label' => 'blabla'
),
'description' => array(
'field_type' => 'textarea',
'label' => 'bleble',
)
)
))
For the "Default" label, I still have no solution.

Symfony CMF with multiple images on Product entity

I'm currently trying to make Symfony CMF and Sonata allow the admin user to add multiple images to a product through the same view panel. I have my Product and Image entities setup and working individually with their ORM relationships too.
Product:
class Product extends AbstractEntity
{
/**
* #ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"})
*/
protected $images;
// ...
Image:
class Image extends AbstractEntity
{
/**
* #ManyToOne(targetEntity="Product", inversedBy="image", cascade={"persist"})
* #JoinColumn(name="product_id", referencedColumnName="id")
**/
private $product;
// ...
Rather than having the user add all the images first and then link to them, I want it to be achievable from the same view. So in my ProductAdmin class I have added the following:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product')
->add('name', 'text', array('required' => true))
// everything else ...
->add('images', 'sonata_type_collection', array(
'type_options' => array('delete' => false),
), array(
'edit' => 'inline',
'inline' => 'table',
))
->end();
}
Currently this allows me to upload images from the product management page. However it does not create a link between the product and image and I'm not sure what I need to do to get it to do that.
UPDATE
Having read countless articles across StackOverflow and the wider web the common response I seem to be seeing is that the relation is only stored when the sonata_type_collection is used for the Admin page of the Entity which holds the relationship (in my case that would be the image). I can understand why it is that way, but it would be much better from a user perspective to work the other way round also (as I'd expect to add images to a product, and not add images and then add a product to them).
I'll leave this question open in case anyone has/does figure out a workaround.
WORKING
I've managed to get it working. Firstly I have changed my relationship to a Many-to-Many (so that images can be re-used for other products as my system contains versions), however I do not believe this is the fix itself, but worth noting none the less.
What I think has made it work is the inclusion of the by_reference attribute:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product')
->add('name', 'text', array('required' => true))
// everything else ...
->add('images', 'sonata_type_collection', array(
'by_reference' => false,
'type_options' => array('delete' => false),
), array(
'edit' => 'inline',
'inline' => 'table',
))
->end();
}
So my issue was because I was missing 'by_reference' => false: http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
Similarly, if you're using the collection form type where your underlying collection data is an object (like with Doctrine's ArrayCollection), then by_reference must be set to false if you need the adder and remover (e.g. addAuthor() and removeAuthor()) to be called.
Or in my case addImage() and removeImage().

ManyToMany nullable not allowed

I have a scenario like this:
tracks ManyToMany instruments
I create the form in this way:
$builder
->add('instruments', 'entity_hidden', array(
'attr' => array('class' => 'one_req'),
'label' => 'form.instrument',
'translation_domain' => 'label',
'required' => false,
'class' => 'Acme\DemoBundle\Entity\Instrument'
))
"hidden_entity" is given by a custom transformer that you can find here: gist
although I believe that is not the problem.
The problem is that the user may not even choose any instrument (such asamong other specified with "required" => false).
When I go to save my returns this error:
Neither the property 'instruments' nor one of the methods 'setInstruments()',
'__set()' or '__call()' exist and have public access in
class 'Acme\DemoBundle\Entity\SoundtrackVersion'.
I do not understand why ..
Where am I doing wrong?
It sounds very obvious, but error can not lie. Check that Acme\DemoBundle\Entity\SoundtrackVersion entity has property instruments and methods setInstruments() and getInstruments().

Resources