This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Collection support #10
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Component\ContentType\Field; | ||
|
||
use Symfony\Cmf\Component\ContentType\FieldInterface; | ||
use Symfony\Cmf\Component\ContentType\Form\Extension\Type\FieldCollectionType; | ||
use Symfony\Cmf\Component\ContentType\MappingBuilder; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class CollectionField implements FieldInterface | ||
{ | ||
public function getViewType() | ||
{ | ||
return CollectionView::class; | ||
} | ||
|
||
public function getFormType() | ||
{ | ||
return FieldCollectionType::class; | ||
} | ||
|
||
public function getMapping(MappingBuilder $builder) | ||
{ | ||
return $builder->collection(); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $options) | ||
{ | ||
$options->setRequired([ | ||
'entry_type', | ||
'allow_add', | ||
]); | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Component\ContentType\Form\Extension; | ||
|
||
use Metadata\MetadataFactoryInterface; | ||
use Symfony\Cmf\Component\ContentType\FieldRegistry; | ||
use Symfony\Cmf\Component\ContentType\Form\Extension\Type\FieldCollectionType; | ||
use Symfony\Cmf\Component\ContentType\Form\Extension\Type\SurrogateType; | ||
use Symfony\Component\Form\AbstractExtension; | ||
|
||
/** | ||
* Form type extension to provide form types for user content-type-managed | ||
* classes in addition to content-type component specific types. | ||
*/ | ||
class FieldExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @var MetadataFactoryInterface | ||
*/ | ||
private $metadataFactory; | ||
|
||
/** | ||
* @var FieldRegistry | ||
*/ | ||
private $fieldRegistry; | ||
|
||
public function __construct( | ||
MetadataFactoryInterface $metadataFactory, | ||
FieldRegistry $fieldRegistry | ||
) { | ||
$this->metadataFactory = $metadataFactory; | ||
$this->fieldRegistry = $fieldRegistry; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasType($type) | ||
{ | ||
if ($this->metadataFactory->getMetadataForClass($type)) { | ||
return true; | ||
} | ||
|
||
return parent::hasType($type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadTypes() | ||
{ | ||
return [ | ||
new FieldCollectionType($this->fieldRegistry), | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getType($type) | ||
{ | ||
if (parent::hasType($type)) { | ||
return parent::getType($type); | ||
} | ||
|
||
$metadata = $this->metadataFactory->getMetadataForClass($type); | ||
|
||
if (!$metadata) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The type "%s" cannot be loaded by this extension', | ||
$type | ||
)); | ||
} | ||
|
||
$surrogateType = new SurrogateType( | ||
$type, | ||
$this->fieldRegistry, | ||
$metadata | ||
); | ||
|
||
return $surrogateType; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Component\ContentType\Form\Extension\Type; | ||
|
||
use Symfony\Cmf\Component\ContentType\FieldRegistry; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class FieldCollectionType extends AbstractType | ||
{ | ||
private $registry; | ||
|
||
public function __construct(FieldRegistry $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $options) | ||
{ | ||
$options->setNormalizer('entry_type', function (Options $options, $value) { | ||
// get the field type | ||
$field = $this->registry->get($value); | ||
|
||
return $field->getFormType(); | ||
}); | ||
} | ||
|
||
public function getParent() | ||
{ | ||
return CollectionType::class; | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Component\ContentType\Form\Extension\Type; | ||
|
||
use Symfony\Cmf\Component\ContentType\FieldRegistry; | ||
use Symfony\Cmf\Component\ContentType\Metadata\ClassMetadata; | ||
use Symfony\Cmf\Component\ContentType\OptionsResolver\FieldOptionsResolver; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Surrogate type for virtual user "content" form types. | ||
* | ||
* For example, if the user manages an "Article" class with the content type | ||
* system, this class will act as its form type. | ||
*/ | ||
class SurrogateType extends AbstractType | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $contentFqn; | ||
|
||
/** | ||
* @var ClassMetadata | ||
*/ | ||
private $classMetadata; | ||
|
||
/** | ||
* @var FieldRegistry | ||
*/ | ||
private $fieldRegistry; | ||
|
||
public function __construct( | ||
$contentFqn, | ||
FieldRegistry $fieldRegistry, | ||
ClassMetadata $classMetadata | ||
) { | ||
$this->contentFqn = $contentFqn; | ||
$this->fieldRegistry = $fieldRegistry; | ||
$this->classMetadata = $classMetadata; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
foreach ($this->classMetadata->getPropertyMetadata() as $propertyMetadata) { | ||
$field = $this->fieldRegistry->get($propertyMetadata->getType()); | ||
$formOptions = $propertyMetadata->getOptions(); | ||
|
||
$resolver = new FieldOptionsResolver(); | ||
$field->configureOptions($resolver); | ||
$formOptions = $resolver->resolveFormOptions($formOptions); | ||
|
||
$builder->add( | ||
$propertyMetadata->getName(), | ||
$field->getFormType(), | ||
$formOptions | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefault('data_class', $this->contentFqn); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing doc