<commit_before><?php

namespace Pum\Bundle\TypeExtraBundle\Pum\Type;

use Doctrine\ORM\Mapping\ClassMetadata as DoctrineMetadata;
use Doctrine\ORM\Mapping\ClassMetadata;
use Pum\Core\AbstractType;
use Pum\Core\Context\FieldBuildContext;
use Pum\Core\Context\FieldContext;
use Pum\Core\Definition\View\FormViewField;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidationMetadata;

class HtmlType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'is_inline'     => false, // block (<p>, <div>....) --- inline (<br />)
            'label'         => null,
            'placeholder'   => null,
            'required'      => false
        ));
    }

    public function buildField(FieldBuildContext $context)
    {
        $cb    = $context->getClassBuilder();
        $camel = $context->getField()->getCamelCaseName();

        $cb->createProperty($camel);
        $cb->addGetMethod($camel);
        $cb->addSetMethod($camel);
    }

    /**
     * {@inheritdoc}
     */
    public function mapDoctrineField(FieldContext $context, ClassMetadata $metadata)
    {
        $metadata->mapField(array(
            'name'      => $context->getField()->getLowercaseName(),
            'fieldName' => $context->getField()->getCamelCaseName(),
            'type'      => 'text',
            'nullable'  => true
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'html';
    }

    public function buildForm(FieldContext $context, FormInterface $form, FormViewField $formViewField)
    {
        $name = $context->getField()->getCamelCaseName();

        if ($context->getOption('is_inline')) {
            $toolbar = array(array('Bold', 'Italic', 'Link'));
        } else {
            $toolbar = array(array('Styles', 'Table'));
        }

        $ckeditorConfig = array(
            'toolbar' => $toolbar,
            'customConfig' => '', # disable dynamic config.js loading
        );

        $options = array(
            'attr' => array(
                'data-ckeditor' => json_encode($ckeditorConfig),
                'placeholder'   => $formViewField->getPlaceholder()
            ),
            'label'    => $formViewField->getLabel(),
            'required' => $context->getOption('required'),
        );

        $form->add($name, 'textarea', $options);
    }

    public function mapValidation(FieldContext $context, ValidationMetadata $metadata)
    {
        // :-)
    }

    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(FormBuilderInterface $builder)
    {
        $builder
            ->add('is_inline', 'checkbox', array('required' => false))
        ;
    }
}<commit_msg>required option for html type<commit_after><?php

namespace Pum\Bundle\TypeExtraBundle\Pum\Type;

use Doctrine\ORM\Mapping\ClassMetadata as DoctrineMetadata;
use Doctrine\ORM\Mapping\ClassMetadata;
use Pum\Core\AbstractType;
use Pum\Core\Context\FieldBuildContext;
use Pum\Core\Context\FieldContext;
use Pum\Core\Definition\View\FormViewField;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidationMetadata;

class HtmlType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'is_inline'     => false, // block (<p>, <div>....) --- inline (<br />)
            'label'         => null,
            'placeholder'   => null,
            'required'      => false
        ));
    }

    public function buildField(FieldBuildContext $context)
    {
        $cb    = $context->getClassBuilder();
        $camel = $context->getField()->getCamelCaseName();

        $cb->createProperty($camel);
        $cb->addGetMethod($camel);
        $cb->addSetMethod($camel);
    }

    /**
     * {@inheritdoc}
     */
    public function mapDoctrineField(FieldContext $context, ClassMetadata $metadata)
    {
        $metadata->mapField(array(
            'name'      => $context->getField()->getLowercaseName(),
            'fieldName' => $context->getField()->getCamelCaseName(),
            'type'      => 'text',
            'nullable'  => true
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'html';
    }

    public function buildForm(FieldContext $context, FormInterface $form, FormViewField $formViewField)
    {
        $name = $context->getField()->getCamelCaseName();

        if ($context->getOption('is_inline')) {
            $toolbar = array(array('Bold', 'Italic', 'Link'));
        } else {
            $toolbar = array(array('Styles', 'Table'));
        }

        $ckeditorConfig = array(
            'toolbar' => $toolbar,
            'customConfig' => '', # disable dynamic config.js loading
        );

        $options = array(
            'attr' => array(
                'data-ckeditor' => json_encode($ckeditorConfig),
                'placeholder'   => $formViewField->getPlaceholder()
            ),
            'label'    => $formViewField->getLabel(),
            'required' => $context->getOption('required'),
        );

        $form->add($name, 'textarea', $options);
    }

    public function mapValidation(FieldContext $context, ValidationMetadata $metadata)
    {
        // :-)
    }

    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(FormBuilderInterface $builder)
    {
        $builder
            ->add('is_inline', 'checkbox', array('required' => false))
            ->add('required', 'checkbox', array('required' => false))
        ;
    }
}
{'fraction_non_alphanumeric': 0.1449419568822554, 'fraction_numerical': 0.0, 'mean_word_length': 2.3375760929717764, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3627225', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1672, 'n_words': 294}