FriendsOfAkeneo / CustomEntityBundle

Eases the creation of custom entity and related views in the PIM
Other
53 stars 60 forks source link

How to create custom entity with wysiwyg text field? #83

Closed msieracki closed 6 years ago

msieracki commented 7 years ago

Hello

I'm trying to create entity with WYSIWYG field. In Akeneo 1.5 I used pim_wysiwyg field type during form creation, but in Akeneo 1.7 it don't work.

$builder ->add( 'fieldName', 'pim_wysiwyg', array( 'label'=> 'Field Label', 'required'=> false ) );

Does anyone knows how to create form with WYSIWYG field for custom entity?

igormukhingmailcom commented 7 years ago

Hi, @jmleroux Any update here? Thanks

jjdiaz commented 6 years ago

Same problem for me in 1.7 version. I can create a TextArea fields using the 'widget' modificator. But I can not use the wysiwyg editor.

Thanks and regards

igormukhingmailcom commented 6 years ago

Editor actually already at codebase and all you need to make it work is to add wysiwyg css class to some textareas. This is how to do this until pim_wysiwyg will be returned back officially:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

class WysiwygType extends TextareaType
{
    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {

    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return 'textarea';
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'wysiwyg';
    }
}
# app/config/config.yml
twig:
    form:
        resources:
            - 'AppBundle:Form:pim-fields.html.twig'
# src/AppBundle/Resources/services.yml

services:

    form.type.wysiwyg:
        class: AppBundle\Form\Type\WysiwygType
        tags:
            - { name: form.type, alias: wysiwyg }
{# src/AppBundle/Resources/views/Form/pim-fields.html.twig #}
{% extends 'PimUIBundle:Form:pim-fields.html.twig' %}

{%- block wysiwyg_widget -%}
    {% set attr = attr|merge({'class': attr.class is defined ? attr.class ~ ' wysiwyg' : 'wysiwyg'}) %}
    <textarea {{ block('widget_attributes') }}>{{ value }}</textarea>
{%- endblock wysiwyg_widget -%}

Usage

Translatable custom WYSIWYG field for Category:

<?php

namespace AppBundle\Form\Type;

use AppBundle\Entity\Brand;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;

use Pim\Bundle\EnrichBundle\Form\Type\CategoryType as BaseCategoryType;

/**
 * Type for category properties
 */
class CategoryType extends BaseCategoryType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'longDescriptionTop',
            'pim_translatable_field',
            [
                'field'             => 'longDescriptionTop',
                'translation_class' => $this->translationDataClass,
                'entity_class'      => $this->dataClass,
                'property_path'     => 'translations',
                'widget'            => 'wysiwyg',
                'required'          => false,
            ]
        );
    }
}
jmleroux commented 6 years ago

Thank you very much @igormukhingmailcom ,

I love when people share their knowledge with each other. That's the way open source should work!

jmleroux commented 6 years ago

I added this explaination in the newly created WIKI: https://github.com/akeneo-labs/CustomEntityBundle/wiki/Frequently-Asked-Questions

If you think it should be part of the main doc, PR are welcome ;)

jjdiaz commented 6 years ago

Thank you very much Igor and Jean Marie. Very good job guys. I will try asap.