Last active
August 29, 2015 14:04
-
-
Save ju1ius/9d482c2d80b0ef66e91b to your computer and use it in GitHub Desktop.
Trying to implement https://github.com/sonata-project/SonataMediaBundle/issues/605#issuecomment-51250006
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<doctrine-mapping | |
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | |
xsi="http://www.w3.org/2001/XMLSchema-instance" | |
schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping | |
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> | |
<entity name="Application\Sonata\MediaBundle\Entity\Media" | |
table="media__media"> | |
<id name="id" type="integer" column="id"> | |
<generator strategy="AUTO"/> | |
</id> | |
<one-to-many field="sources" | |
target-entity="Application\Sonata\MediaBundle\Entity\Media" | |
mapped-by="parent" /> | |
<many-to-one field="parent" | |
target-entity="Application\Sonata\MediaBundle\Entity\Media" | |
inversed-by="sources" > | |
<join-column name="parent_id" referenced-colum-name="id" on-delete="CASCADE" /> | |
</many-to-one> | |
</entity> | |
</doctrine-mapping> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Sonata\MediaBundle\Entity; | |
use Sonata\MediaBundle\Entity\BaseMedia as BaseMedia; | |
use Sonata\MediaBundle\Model\MediaInterface; | |
use Doctrine\Common\Collections\ArrayCollection; | |
class Media extends BaseMedia | |
{ | |
/** | |
* @var integer $id | |
*/ | |
protected $id; | |
/** | |
* @var MediaInterface | |
*/ | |
protected $parent; | |
/** | |
* @var ArrayCollection | |
*/ | |
protected $sources; | |
/** | |
* @param mixed | |
*/ | |
public function __construct() | |
{ | |
$this->sources = new ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer $id | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Get parent | |
* @return MediaInterface | |
*/ | |
public function getParent() | |
{ | |
return $this->parent; | |
} | |
/** | |
* Set parent | |
* @param MediaInterface $media | |
* @return $this | |
*/ | |
public function setParent(MediaInterface $media = null) | |
{ | |
$this->parent = $media; | |
return $this; | |
} | |
public function removeParent() | |
{ | |
$this->parent = null; | |
return $this; | |
} | |
/** | |
* Get sources | |
* @return ArrayCollection | |
*/ | |
public function getSources() | |
{ | |
return $this->sources; | |
} | |
/** | |
* Set sources | |
* @param ArrayCollection $sources | |
* @return $this | |
*/ | |
public function setSources(ArrayCollection $sources) | |
{ | |
foreach ($sources as $source) { | |
$this->addSource($source); | |
} | |
return $this; | |
} | |
public function addSource(MediaInterface $source) | |
{ | |
if (!$this->sources->contains($source)) { | |
$this->sources->add($source); | |
$source->setParent($this); | |
} | |
return $this; | |
} | |
public function removeSource(MediaInterface $source) | |
{ | |
$this->sources->remove($source); | |
$source->removeParent(); | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Sonata\MediaBundle\Admin; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Sonata\MediaBundle\Admin\ORM\MediaAdmin as BaseMediaAdmin; | |
use Sonata\AdminBundle\Datagrid\ListMapper; | |
use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer; | |
use Sonata\MediaBundle\Model\MediaInterface; | |
class MediaAdmin extends BaseMediaAdmin | |
{ | |
// [... configure list fields ...] | |
/** | |
* Override Sonata\MediaBundle\Admin\BaseMediaAdmin | |
* | |
* @param FormMapper $formMapper | |
*/ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$media = $this->getSubject(); | |
if (!$media) { | |
$media = $this->getNewInstance(); | |
} | |
if ($this->hasParentFieldDescription()) { | |
// the admin is embedded | |
$this->configureSourcesFormFields($media, $formMapper); | |
} else { | |
$this->configureMediaFormFields($media, $formMapper); | |
} | |
} | |
private function configureMediaFormFields($media, $formMapper) | |
{ | |
$formMapper | |
->add('name') | |
->add('enabled') | |
->add('description') | |
->add('authorName') | |
->add('copyright') | |
->with('Sources') | |
->add('sources', 'sonata_type_collection', [ | |
'required' => true, | |
'label' => 'Sources', | |
'by_reference' => false, | |
], [ | |
'edit' => 'inline', | |
'inline' => 'table', | |
// needed for the Add button | |
'link_parameters' => [ | |
'context' => $this->request->get('context'), | |
'provider' => $this->request->get('provider') | |
] | |
]) | |
->end() | |
; | |
$formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { | |
if ($this->request->isXmlHttpRequest() || $this->request->get('_xml_http_request')) { | |
return; | |
} | |
$toSource = [ | |
'name', 'enabled', 'providerName', | |
'context' | |
]; | |
$toMedia = [ | |
'providerReference', 'providerStatus' | |
]; | |
foreach ($media->getSources() as $source) { | |
foreach ($toSource as $prop) { | |
$prop = ucfirst($prop); | |
$get = 'get' . $prop; | |
$set = 'set' . $prop; | |
$source->$set($media->$get()); | |
} | |
foreach ($toMedia as $prop) { | |
$prop = ucfirst($prop); | |
$get = 'get' . $prop; | |
$set = 'set' . $prop; | |
$media->$set($source->$get()); | |
} | |
} | |
}); | |
} | |
private function configureSourcesFormFields($media, $formMapper) | |
{ | |
$parent = $media->getParent(); | |
if (!$parent || !$parent->getProviderName()) { | |
return; | |
} | |
$transformer = new ProviderDataTransformer($this->pool, $this->getClass()); | |
$formMapper->getFormBuilder()->addModelTransformer($transformer, true); | |
$provider = $this->pool->getProvider($parent->getProviderName()); | |
if ($media->getId()) { | |
$provider->buildEditForm($formMapper); | |
} else { | |
$provider->buildCreateForm($formMapper); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
sonata.media.admin.media: | |
class: Application\Sonata\MediaBundle\Admin\MediaAdmin | |
arguments: | |
- ~ | |
- Application\Sonata\MediaBundle\Entity\Media | |
- %sonata.media.admin.media.controller% | |
- @sonata.media.pool | |
tags: | |
- | |
name: sonata.admin | |
manager_type: orm | |
group: sonata_media | |
label_catalogue: "%sonata.media.admin.media.translation_domain" | |
label: Medias | |
translator_strategy: sonata.admin.label.strategy.underscore | |
calls: | |
- [setModelManager, [@sonata.media.admin.media.manager]] | |
- [setTranslationDomain, [%sonata.media.admin.media.translation_domain%]] | |
- [setTemplates, [{list: SonataMediaBundle:MediaAdmin:list.html.twig}]] | |
# Custom templates | |
- [setTemplate, [list_custom, ApplicationSonataMediaBundle:MediaAdmin:list_custom.html.twig]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment