Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Extension to override ImageType et VideoType of RichEditorPlugin when used #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ Use `MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\FaviconType`

Use `MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\AudioType`

### RichEditorPlugin

If you use our RichEditorPlugin the ImageType and VideoType are using the MediaManager.
In case you had the RichEditorPlugin before the MediaManagerPlugin you need to copy the folder `%kernel.project_dir%/public%/media/rich-editor` to `%kernel.project_dir%/public%/media/media/rich-editor`
to keep the image from being displayed when they were uploaded through the richEditor and not the mediaManager.

## Contributing

You can find a way to run the plugin without effort in the file [DEVELOPMENT.md](./DEVELOPMENT.md).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{#
UI Element template
type: image
element fields :
image
alt
title
link
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
{% endif %}
</p>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{#
UI Element template
type: image_collection
element fields :
images
#}
<div class="ui stackable two column grid">
{% for image in element.images %}
<div class="column">
{% if image.link is not empty %}
<a href="{{ image.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
{% endif %}
</div>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
UI Element template
type: video
element fields :
video
image
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<div style="text-align: {{align}};">
<video width="100%" poster="{{ get_media_manager_file_path(element.image) }}" controls>
<source src="{{ get_media_manager_file_path(element.video) }}" type="video/{{ element.video|split('.')|last }}">
</video>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{#
UI Element template
type: image
element fields :
image
alt
title
link
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
{% endif %}
</p>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{#
UI Element template
type: image_collection
element fields :
images
#}
<div class="ui stackable two column grid">
{% for image in element.images %}
<div class="column">
{% if image.link is not empty %}
<a href="{{ image.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
{% endif %}
</div>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
UI Element template
type: video
element fields :
video
image
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<div style="text-align: {{align}};">
<video width="100%" poster="{{ get_media_manager_file_path(element.image) }}" controls>
<source src="{{ get_media_manager_file_path(element.video) }}" type="video/{{ element.video|split('.')|last }}">
</video>
</div>
8 changes: 8 additions & 0 deletions recipes/1.0/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@
"MONSIEURBIZ_SYLIUS_MEDIA_MANAGER_PUBLIC_FOLDER": "%kernel.project_dir%/public",
"MONSIEURBIZ_SYLIUS_MEDIA_MANAGER_ROOT_FOLDER_FROM_PUBLIC": "media",
"MONSIEURBIZ_SYLIUS_MEDIA_MANAGER_MAX_FILE_SIZE": "5M"
},
"copy-from-package": {
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/image.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/image.html.twig",
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/image_collection.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/image_collection.html.twig",
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/video.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Admin/UiElement/video.html.twig",
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/image.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/image.html.twig",
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/image_collection.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/image_collection.html.twig",
"src/Resources/views/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/video.html.twig": "templates/MonsieurBizSyliusRichEditorPlugin/Shop/UiElement/video.html.twig"
}
}
58 changes: 58 additions & 0 deletions src/Form/Extension/RichEditor/ImageTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of Monsieur Biz' Media Manager plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusMediaManagerPlugin\Form\Extension\RichEditor;

use Closure;
use MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\ImageType as MediaManagerImageType;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\UiElement\ImageType as RichEditorImageType;
use ReflectionFunction;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;

class ImageTypeExtension extends AbstractTypeExtension
{
/**
* @SuppressWarnings(UnusedFormalParameter)
* @SuppressWarnings(CyclomaticComplexity)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('image', MediaManagerImageType::class)
;

foreach ($builder->getEventDispatcher()->getListeners(FormEvents::PRE_SUBMIT) as $listener) {
if ($listener instanceof Closure) {
$reflection = new ReflectionFunction($listener);
$closureScopeClass = $reflection->getClosureScopeClass();

if (null === $closureScopeClass) {
continue;
}
/** @phpstan-ignore-next-line */
if (RichEditorImageType::class === $closureScopeClass->getName()) {
// Remove the event listener that is re-adding the field and emptying the options
$builder->getEventDispatcher()->removeListener(FormEvents::PRE_SUBMIT, $listener);
}
}
}
}

public static function getExtendedTypes(): iterable
{
/** @phpstan-ignore-next-line */
return [RichEditorImageType::class];
}
}
60 changes: 60 additions & 0 deletions src/Form/Extension/RichEditor/VideoTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Monsieur Biz' Media Manager plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusMediaManagerPlugin\Form\Extension\RichEditor;

use Closure;
use MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\ImageType as MediaManagerImageType;
use MonsieurBiz\SyliusMediaManagerPlugin\Form\Type\VideoType as MediaManagerVideoType;
use MonsieurBiz\SyliusRichEditorPlugin\Form\Type\UiElement\VideoType as RichEditorVideoType;
use ReflectionFunction;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;

class VideoTypeExtension extends AbstractTypeExtension
{
/**
* @SuppressWarnings(UnusedFormalParameter)
* @SuppressWarnings(CyclomaticComplexity)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('video', MediaManagerVideoType::class)
->add('image', MediaManagerImageType::class)
;

foreach ($builder->getEventDispatcher()->getListeners(FormEvents::PRE_SUBMIT) as $listener) {
if ($listener instanceof Closure) {
$reflection = new ReflectionFunction($listener);
$closureScopeClass = $reflection->getClosureScopeClass();

if (null === $closureScopeClass) {
continue;
}
/** @phpstan-ignore-next-line */
if (RichEditorVideoType::class === $closureScopeClass->getName()) {
// Remove the event listener that is re-adding the field and emptying the options
$builder->getEventDispatcher()->removeListener(FormEvents::PRE_SUBMIT, $listener);
}
}
}
}

public static function getExtendedTypes(): iterable
{
/** @phpstan-ignore-next-line */
return [RichEditorVideoType::class];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{#
UI Element template
type: image
element fields :
image
alt
title
link
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
{% endif %}
</p>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{#
UI Element template
type: image_collection
element fields :
images
#}
<div class="ui stackable two column grid">
{% for image in element.images %}
<div class="column">
{% if image.link is not empty %}
<a href="{{ image.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
{% endif %}
</div>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
UI Element template
type: video
element fields :
video
image
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<div style="text-align: {{align}};">
<video width="100%" poster="{{ get_media_manager_file_path(element.image) }}" controls>
<source src="{{ get_media_manager_file_path(element.video) }}" type="video/{{ element.video|split('.')|last }}">
</video>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{#
UI Element template
type: image
element fields :
image
alt
title
link
align
#}
{% if element.image is defined %}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<p style="text-align: {{align}};">
{% if element.link is not empty %}
<a href="{{ element.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(element.image) }}" alt="{{ element.alt|default('') }}" title="{{ element.title|default('') }}" />
{% endif %}
</p>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{#
UI Element template
type: image_collection
element fields :
images
#}
<div class="ui stackable two column grid">
{% for image in element.images %}
<div class="column">
{% if image.link is not empty %}
<a href="{{ image.link }}">
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
</a>
{% else %}
<img class="ui fluid image" src="{{ get_media_manager_file_path(image.image) }}" alt="{{ image.alt|default('') }}" title="{{ image.title|default('') }}" />
{% endif %}
</div>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
UI Element template
type: video
element fields :
video
image
align
#}
{% set align = element.align is defined and element.align is not empty ? element.align : 'inherit' %}
<div style="text-align: {{align}};">
<video width="100%" poster="{{ get_media_manager_file_path(element.image) }}" controls>
<source src="{{ get_media_manager_file_path(element.video) }}" type="video/{{ element.video|split('.')|last }}">
</video>
</div>
Loading