add file_widget render
This commit is contained in:
parent
9a61c5e876
commit
95a2d0a894
|
@ -1,3 +1,3 @@
|
|||
twig:
|
||||
default_path: '%kernel.project_dir%/templates'
|
||||
form_themes: ['bootstrap_4_layout.html.twig']
|
||||
form_themes: ['site/form/bootstrap_4_form_theme.html.twig']
|
||||
|
|
|
@ -30,6 +30,7 @@ class FileBlockType extends TextBlockType
|
|||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => FileBlock::class,
|
||||
'block_prefix' => 'file_block',
|
||||
'options' => [],
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ use App\Repository\Blog\PostRepository;
|
|||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
/**
|
||||
* @ORM\Entity(repositoryClass=PostRepository::class)
|
||||
|
@ -150,8 +151,16 @@ class Post implements EntityInterface
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getImage(): ?string
|
||||
public function getImage()
|
||||
{
|
||||
if (is_string($this->image)) {
|
||||
if (file_exists($this->image)) {
|
||||
return new File($this->image);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->image;
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ class PostEventSubscriber extends EntityManagerEventSubscriber
|
|||
$finder->files()->in('uploads/post');
|
||||
|
||||
foreach ($finder as $file) {
|
||||
$image = str_replace('uploads/', '', $file->getPathname());
|
||||
$image = $file->getPathname();
|
||||
|
||||
$post = $this->query->create()
|
||||
->where('.image = :image')
|
||||
|
|
|
@ -22,12 +22,6 @@
|
|||
{% for item in ['image', 'imageCaption', 'status', 'publishedAt', 'author'] %}
|
||||
<div class="col-12">
|
||||
{{ form_row(form[item]) }}
|
||||
|
||||
{% if item == 'image' %}
|
||||
{% if entity.image %}
|
||||
<img src="{{ asset('uploads/' ~ entity.image) }}" class="img-fluid">
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
<tr>
|
||||
<td class="col-6">
|
||||
{% if item.image %}
|
||||
{% set image = asset(item.image) %}
|
||||
{% set image = asset(item.image.pathname) %}
|
||||
{% else %}
|
||||
{% set image = asset('build/images/no-image.png') %}
|
||||
{% endif %}
|
||||
|
|
|
@ -68,7 +68,7 @@
|
|||
|
||||
{% if entity.image %}
|
||||
<figure>
|
||||
<img src="{{ asset(entity.image) }}" alt="{{ entity.imageCaption }}" title="{{ entity.imageCaption }}" class="img-fluid">
|
||||
<img src="{{ asset(entity.image.pathname) }}" alt="{{ entity.imageCaption }}" title="{{ entity.imageCaption }}" class="img-fluid">
|
||||
|
||||
<figcaption>
|
||||
{{ entity.imageCaption }}
|
||||
|
|
36
templates/site/form/bootstrap_4_form_theme.html.twig
Normal file
36
templates/site/form/bootstrap_4_form_theme.html.twig
Normal file
|
@ -0,0 +1,36 @@
|
|||
{% extends 'bootstrap_4_layout.html.twig' %}
|
||||
|
||||
{% block file_widget -%}
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
{% set value = form.vars.data %}
|
||||
|
||||
{% if value %}
|
||||
{% if value and value.extension in ['jpg', 'gif', 'png'] %}
|
||||
<div class="card">
|
||||
<div class="card-img-top bg-dark text-center">
|
||||
<img src="{{ asset(value.pathname) }}" class="img-fluid">
|
||||
</div>
|
||||
<div class="card-body">
|
||||
{{- parent() -}}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
{{- parent() -}}
|
||||
|
||||
<div class="p-2 text-center">
|
||||
<a class="btn btn-primary" href="{{ asset(value.pathname) }}" target="_blank">
|
||||
Télécharger
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
{{- parent() -}}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue