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

Implement ImagineBlock #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion src/Document/CustomBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lakion\CmsPlugin\Document;

use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ImagineBlock;

class CustomBlock extends ImagineBlock implements ResourceInterface
{
Expand Down
198 changes: 198 additions & 0 deletions src/Document/ImagineBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Lakion\CmsPlugin\Document;

use PHPCR\NodeInterface;
use Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\AbstractBlock;
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
use Symfony\Cmf\Bundle\MediaBundle\ImageInterface;
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Block to hold an image.
*/
class ImagineBlock extends AbstractBlock implements TranslatableInterface
{
/**
* @var Image
*/
protected $image;

/**
* @var string
*/
protected $label;

/**
* Optional link url to use on the image.
*
* @var string
*/
protected $linkUrl;

/**
* @var string
*/
protected $filter;

/**
* @var \PHPCR\NodeInterface
*/
protected $node;

/**
* {@inheritdoc}
*/
public function getType()
{
return 'cmf.block.imagine';
}

/**
* Set label.
*
* @param string $label
*
* @return $this
*/
public function setLabel($label)
{
$this->label = $label;

return $this;
}

/**
* Get label.
*
* @return string
*/
public function getLabel()
{
return $this->label;
}

/**
* Set link url.
*
* @param string $url
*
* @return $this
*/
public function setLinkUrl($url)
{
$this->linkUrl = $url;

return $this;
}

/**
* Get link url.
*
* @return string
*/
public function getLinkUrl()
{
return $this->linkUrl;
}

/**
* Sets the Imagine filter which is going to be used.
*
* @param string $filter
*
* @return $this
*/
public function setFilter($filter)
{
$this->filter = $filter;

return $this;
}

/**
* Get the Imagine filter.
*
* @return string
*/
public function getFilter()
{
return $this->filter;
}

/**
* Set the image for this block.
*
* Setting null will do nothing, as this is what happens when you edit this
* block in a form without uploading a replacement file.
*
* If you need to delete the Image, you can use getImage and delete it with
* the document manager. Note that this block does not make much sense
* without an image, though.
*
* @param ImageInterface|UploadedFile|null $image optional the image to update
*
* @return $this
*
* @throws \InvalidArgumentException if the $image parameter can not be handled
*/
public function setImage($image = null)
{
if (!$image) {
return $this;
}

if (!$image instanceof ImageInterface && !$image instanceof UploadedFile) {
$type = is_object($image) ? get_class($image) : gettype($image);

throw new \InvalidArgumentException(sprintf(
'Image is not a valid type, "%s" given.',
$type
));
}

if ($this->image) {
// existing image, only update content
// TODO: https://github.com/doctrine/phpcr-odm/pull/262
$this->image->copyContentFromFile($image);
} elseif ($image instanceof ImageInterface) {
$image->setName('image'); // ensure document has right name
$this->image = $image;
} else {
$this->image = new Image();
$this->image->copyContentFromFile($image);
}

return $this;
}

/**
* Get image.
*
* @return Image
*/
public function getImage()
{
return $this->image;
}

/**
* Get node.
*
* @return NodeInterface
*/
public function getNode()
{
return $this->node;
}
}
30 changes: 30 additions & 0 deletions src/Resources/config/doctrine/ImagineBlock.phpcr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/phpcr-odm/phpcr-mapping
https://github.com/doctrine/phpcr-odm/raw/master/doctrine-phpcr-odm-mapping.xsd"
>

<document
name="Lakion\CmsPlugin\Document\ImagineBlock"
referenceable="true"
translator="attribute"
>

<node name="node"/>

<locale name="locale"/>

<field name="label" type="string" translated="true" nullable="true"/>
<field name="linkUrl" type="string" translated="true" nullable="true"/>
<field name="filter" type="string" nullable="true"/>

<child name="image" node-name="image">
<cascade>
<cascade-persist/>
</cascade>
</child>

</document>

</doctrine-mapping>
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<imports>
<import resource="services/imagine_block.xml" />
<import resource="services/custom_block.xml" />
<import resource="services/product_block.xml" />
<import resource="services/route.xml" />
Expand Down
17 changes: 17 additions & 0 deletions src/Resources/config/services/imagine_block.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="cmf.block.imagine" class="Symfony\Cmf\Bundle\BlockBundle\Block\SimpleBlockService">
<tag name="sonata.block" />
<argument>cmf.block.imagine</argument>
<argument type="service" id="templating" />
<argument>LakionCmsPlugin:Block:block_imagine.html.twig</argument>
</service>

</services>
</container>
9 changes: 9 additions & 0 deletions src/Resources/config/validation/ImagineBlock.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Lakion\CmsPlugin\Document\ImagineBlock">
<property name="locale">
<constraint name="Locale"/>
</property>
</class>
</constraint-mapping>
17 changes: 17 additions & 0 deletions src/Resources/views/Block/block_imagine.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends sonata_block.templates.block_base %}

{% if block.filter %}
{% set filter = block.filter %}
{% else %}
{% set filter = "cmf_block" %}
{% endif %}

{% block block %}
{% if block.linkUrl %}
<a href="{{ block.linkUrl }}" title="{{ block.label|default('') }}">
{% endif %}
<span id="label">{{ block.label }}</span><img src="{{ block.image.id | imagine_filter(filter) }}" alt="{{ block.label }}" />
{% if block.linkUrl %}
</a>
{% endif %}
{% endblock %}