Commit a4c02196 authored by Niklas de Vries's avatar Niklas de Vries

Fix for symfony 4

parent 6f2030e6
......@@ -13,11 +13,15 @@ use Cwd\MediaBundle\Form\Transformer\MediaTransformer;
use Cwd\MediaBundle\Model\Entity\Media;
use Cwd\MediaBundle\Service\MediaService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
......@@ -51,7 +55,6 @@ class ImageType extends AbstractType
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
$media = $event->getData();
if ($media instanceof Media) {
return $media;
} elseif (is_numeric($media)) {
......@@ -66,32 +69,59 @@ class ImageType extends AbstractType
}
/**
* @param OptionsResolver $resolver
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function configureOptions(OptionsResolver $resolver)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$resolver->setDefaults(
array(
'attr' => ['class' => 'image-upload'],
'validation_groups' => array('default'),
'data_class' => 'Cwd\MediaBundle\Model\Entity\Media',
'cascade_validation' => true,
)
);
if ($options['multiple']) {
$view->vars['full_name'] .= '[]';
$view->vars['attr']['multiple'] = 'multiple';
}
$view->vars = array_replace($view->vars, array(
'type' => 'file',
'value' => '',
));
}
public function getBlockPrefix()
/**
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
return 'cwd_image_type';
$view->vars['multipart'] = true;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$emptyData = function (Options $options) {
return $options['multiple'] ? array() : null;
};
$resolver->setDefaults(array(
'compound' => false,
'data_class' => 'Cwd\MediaBundle\Model\Entity\Media',
'empty_data' => $emptyData,
'multiple' => false,
'attr' => ['class' => 'image-upload'],
'validation_groups' => array('default'),
'cascade_validation' => true,
));
}
/**
*
* @return string
*/
public function getParent()
public function getBlockPrefix()
{
return FileType::class;
return 'cwd_image_type';
}
/**
......
......@@ -21,12 +21,15 @@
{% endif %}
</div>
<div>
<span class="btn default btn-file">
<span class="btn btn-primary default btn-file" style="margin-right:5px;">
<span class="fileinput-new"> {{ "Select image"|trans }} </span>
<span class="fileinput-exists"> {{ "Change"|trans }} </span>
<input type="file">
</span>
<a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> {{ "Reset"|trans }} </a>
</span><a
href="javascript:;"
class="btn btn-warning red fileinput-exists"
data-dismiss="fileinput"
> {{ "Reset"|trans }} </a>
</div>
</div>
</div>
......@@ -34,4 +37,4 @@
</div>
</div>
{% endblock %}
{% endblock %}
\ No newline at end of file
......@@ -560,7 +560,7 @@ class MediaService
public function find($pid)
{
try {
$obj = $this->findById('Model:Media', intval($pid));
$obj = $this->findById($this->getConfig('entity_class'), intval($pid));
if ($obj === null) {
$this->getLogger()->info('Row with ID {id} not found', array('id' => $pid));
......@@ -583,7 +583,7 @@ class MediaService
{
$filter = ['mediatype' => ['image/jpeg', 'image/png', 'image/gif']];
return $this->findAllByModel('Model:Media', $filter, $order, $amount, $offset);
return $this->findAllByModel($this->getConfig('entity_class'), $filter, $order, $amount, $offset);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment