首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ImagineBlock的Symfony CMF多图像域

使用ImagineBlock的Symfony CMF多图像域
EN

Stack Overflow用户
提问于 2015-06-11 07:36:28
回答 2查看 421关注 0票数 0

问题

你好,我使用Symfony CMF 1.2,liip/imagine-bundle 1.3,symfony-cmf/media-bundle 1.2。我想在扩展ImagineBlock的块中添加2个额外的图像字段,因为我上传的每一幅图像都会有一个移动版和平板版,这不是简单的调整大小、纵横比或诸如此类的东西。我不能只是裁剪/调整大小而不影响图像的质量。

尝试

我的街区

代码语言:javascript
复制
namespace xx\BlockBundle\Document;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\ImagineBlock;
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
use Symfony\Cmf\Bundle\MediaBundle\ImageInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Class ClickableBlock
 * @package xx\BlockBundle\Document
 * @PHPCR\Document(referenceable=true)
 */
class ClickableBlock extends ImagineBlock
{
    /**
     * @PHPCR\Child(nodeName="image-mobile", cascade={"persist"})
     * @var Image
     */
    protected $imageMobile;

    /**
     * @PHPCR\Child(nodeName="image-tablet", cascade={"persist"})
     * @var Image
     */
    protected $imageTablet;

    public function setIsPublishable($publishable)
    {
        $this->setPublishable($publishable);
    }

    /**
     * @return Image
     */
    public function getImageMobile()
    {
        return $this->imageMobile;
    }

    /**
     * @return Image
     */
    public function getImageTablet()
    {
        return $this->imageTablet;
    }

    /**
     * Set the imageMobile for this block.
     *
     * @param ImageInterface|UploadedFile|null $image optional the imageMobile to update
     * @return $this
     * @throws \InvalidArgumentException If the $image parameter can not be handled.
     */
    public function setImageMobile($image = null)
    {
        return $this->processImage($image, 'image-mobile', $this->imageMobile);
    }

    /**
     * Set the imageTablet for this block.
     *
     * @param ImageInterface|UploadedFile|null $image optional the imageTablet to update
     * @return $this
     * @throws \InvalidArgumentException If the $image parameter can not be handled.
     */
    public function setImageTablet($image = null)
    {
        return $this->processImage($image, 'image-tablet', $this->imageTablet);
    }

    /**
     * @param ImageInterface|UploadedFile|null $image
     * @param string $imageName
     * @param Image $imageRef
     * @return $this
     */
    protected function processImage($image, $imageName, $imageRef)
    {
        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 ($imageRef) {
            // existing imageTablet, only update content
            $imageRef->copyContentFromFile($image);
        } elseif ($image instanceof ImageInterface) {
            $image->setName($imageName); // ensure document has right name
            $imageRef = $image;
        } else {
            $imageRef = new Image();
            $imageRef->copyContentFromFile($image);
        }

        return $this;
    }
}

管理:

代码语言:javascript
复制
namespace xx\BlockBundle\Admin;

use xx\BlockBundle\Document\ClickableBlock;
use xx\MainBundle\Form\Common\FormMapper as CommonFormMapper;
use Cocur\Slugify\Slugify;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Cmf\Bundle\BlockBundle\Admin\Imagine\ImagineBlockAdmin;

class ClickableBlockAdmin extends ImagineBlockAdmin
{
    /**
     * {@inheritdoc}
     */
    public function toString($object)
    {
        return $object instanceof ClickableBlock && $object->getLabel()
            ? $object->getLabel()
            : parent::toString($object);
    }

    /**
     * {@inheritdoc}
     */
    public function prePersist($document)
    {
        parent::prePersist($document);
        $this->InitialiseDocument($document);
    }

    /**
     * @param $document
     */
    private function InitialiseDocument(&$document)
    {
        $documentManager = $this->getModelManager();
        $parentDocument = $documentManager->find(null, '/cms/xx/block');

        $document->setParentDocument($parentDocument);
        $slugifier = new Slugify();
        $document->setName($slugifier->slugify($document->getLabel()));
    }

    /**
     * {@inheritdoc}
     */
    public function preUpdate($document)
    {
        parent::preUpdate($document);
        $this->InitialiseDocument($document);
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        parent::configureFormFields($formMapper);

        if (null === $this->getParentFieldDescription()) {
            $imageRequired = ($this->getSubject() && $this->getSubject()->getParentDocument()) ? false : true;
            $formMapper
                ->with('form.group_general')
                ->remove('parentDocument')
                ->remove('filter')
                ->add('parentDocument', 'hidden', ['required' => false, 'data' => 'filler'])
                ->add('name', 'hidden', ['required' => false, 'data' => 'filler'])
                ->add('imageMobile', 'cmf_media_image', array('required' => $imageRequired))
                ->add('imageTablet', 'cmf_media_image', array('required' => $imageRequired))
                ->end();

            // Append common fields to FormMapper
            $commonFormMapper = new CommonFormMapper($formMapper);
            $formMapper = $commonFormMapper->getPublishingFields();
        }
    }

}

注意,我无法将服务容器注入这个类(通过构造函数/方法),这就是为什么我现在使用硬编码节点路径和实例化Slugify类而不是使用它的服务。我也会倾听解决这个问题的办法。参参-

代码语言:javascript
复制
xx.main.admin.pageadmin.container:
    class: xx\MainBundle\Admin\PageAdmin
    calls:
        - [setContainer,[ @service_container ]]
#        arguments: ["@service_container"]

图像字段上的注释基于在\vendor\symfony-cmf\block-bundle\Resources\config\doctrine-phpcr\ImagineBlock.phpcr.xml中找到的以下配置

代码语言:javascript
复制
<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="Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\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>

结果

虽然默认的" image“字段保持正常状态,但其他两个添加的图像字段没有被考虑,因为当我在prePersist上调试时,当图像字段包含其上载的文件时,两个字段都为null。

我尝试添加一个正常的文本字段,该字段通常保存并显示在我的页面上。

我在我的项目中使用YAML,所以我不确定给定的XML到底是如何转换的,如果它是要定义的正确映射的话。

请帮帮忙。:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-16 07:10:15

一位同事认为这个问题如下:

代码语言:javascript
复制
protected function processImage($image, $imageName, $imageRef)

应该是

代码语言:javascript
复制
protected function processImage($image, $imageName, &$imageRef)

$imageRef不是通过引用传递的,因此始终为空。我真傻。希望这段代码至少能帮助到其他人。:)

票数 1
EN

Stack Overflow用户

发布于 2015-06-13 09:44:16

对于管理问题: phpcr-odm管理员有一个rootPath,用于您所做的工作。您可以在服务定义中添加如下内容:

代码语言:javascript
复制
<call method="setRootPath">
    <argument>%cmf_content.persistence.phpcr.content_basepath%</argument>
</call>

然后执行$this->getRootPath()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30774498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档