forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sonata_type_model_autocomplete form type
Added support for many-to-many in sonata_type_model_autocomplete Changed `collection` form type to `sonata_type_native_collection` Fixed unit tests Fixed issues with Sf 2.3 Fixed dates in changelog Fixed minor issues Make code more universal. Another improvements. Fixed bugs in paging. Added toStringCallback option. Fixed datagrid parameters. Fixed unit tests Improved code coverage of Datagrid Improved doc and added some more tests Fixed CS
- Loading branch information
1 parent
6b101d7
commit 3d86eca
Showing
15 changed files
with
1,052 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Form\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Sonata\AdminBundle\Model\ModelManagerInterface; | ||
use Doctrine\Common\Util\ClassUtils; | ||
use RuntimeException; | ||
|
||
/** | ||
* Transform object to ID and property label | ||
* | ||
* @author Andrej Hudec <[email protected]> | ||
*/ | ||
class ModelToIdPropertyTransformer implements DataTransformerInterface | ||
{ | ||
protected $modelManager; | ||
|
||
protected $className; | ||
|
||
protected $property; | ||
|
||
protected $multiple; | ||
|
||
protected $toStringCallback; | ||
|
||
/** | ||
* @param ModelManagerInterface $modelManager | ||
* @param string $className | ||
* @param string $property | ||
*/ | ||
public function __construct(ModelManagerInterface $modelManager, $className, $property, $multiple=false, $toStringCallback=null) | ||
{ | ||
$this->modelManager = $modelManager; | ||
$this->className = $className; | ||
$this->property = $property; | ||
$this->multiple = $multiple; | ||
$this->toStringCallback = $toStringCallback; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
$collection = $this->modelManager->getModelCollectionInstance($this->className); | ||
|
||
if (empty($value) || empty($value['identifiers'])) { | ||
if (!$this->multiple) { | ||
return null; | ||
} else { | ||
return $collection; | ||
} | ||
} | ||
|
||
if (!$this->multiple) { | ||
return $this->modelManager->find($this->className, current($value['identifiers'])); | ||
} | ||
|
||
foreach ($value['identifiers'] as $id) { | ||
$collection->add($this->modelManager->find($this->className, $id)); | ||
} | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function transform($entityOrCollection) | ||
{ | ||
$result = array('identifiers' => array(), 'labels' => array()); | ||
|
||
if (!$entityOrCollection) { | ||
return $result; | ||
} | ||
if ($entityOrCollection instanceof \ArrayAccess) { | ||
$collection = $entityOrCollection; | ||
} else { | ||
$collection = array($entityOrCollection); | ||
} | ||
|
||
if (empty($this->property)) { | ||
throw new RuntimeException('Please define "property" parameter.'); | ||
} | ||
|
||
foreach ($collection as $entity) { | ||
$id = current($this->modelManager->getIdentifierValues($entity)); | ||
|
||
if ($this->toStringCallback !== null) { | ||
if (!is_callable($this->toStringCallback)) { | ||
throw new RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.'); | ||
} | ||
|
||
$label = call_user_func($this->toStringCallback, $entity, $this->property); | ||
} else { | ||
try { | ||
$label = (string) $entity; | ||
} catch (\Exception $e) { | ||
throw new RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e); | ||
} | ||
} | ||
|
||
$result['identifiers'][] = $id; | ||
$result['labels'][] = $label; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
|
||
/** | ||
* This type wrap native `collection` form type and render `add` and `delete` | ||
* buttons in standard Symfony` collection form type. | ||
* | ||
* @author Andrej Hudec <[email protected]> | ||
*/ | ||
class CollectionType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return 'collection'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'sonata_type_native_collection'; | ||
} | ||
} |
Oops, something went wrong.