Skip to content

Instantly share code, notes, and snippets.

@wizhippo
Created March 14, 2018 16:39
Show Gist options
  • Save wizhippo/f27a68580de437dc57a29a22f85cb078 to your computer and use it in GitHub Desktop.
Save wizhippo/f27a68580de437dc57a29a22f85cb078 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace AppBundle\Form\Extension;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use EzSystems\EzPlatformAdminUi\Form\Type\ContentType\ContentTypeChoiceType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContentTypeChoiceTypeExtension extends AbstractTypeExtension
{
/** @var ContentTypeService */
protected $contentTypeService;
/** @var array */
protected $excludedContentTypeIds;
/**
* @param ContentTypeService $contentTypeService
* @param array $excludedContentTypeIds
*/
public function __construct(ContentTypeService $contentTypeService, $excludedContentTypeIds = [])
{
$this->contentTypeService = $contentTypeService;
$this->excludedContentTypeIds = $excludedContentTypeIds;
}
public function getExtendedType()
{
return ContentTypeChoiceType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'choice_loader' => new CallbackChoiceLoader(function () {
$contentTypes = [];
$contentTypeGroups = $this->contentTypeService->loadContentTypeGroups();
foreach ($contentTypeGroups as $contentTypeGroup) {
$contentTypes[$contentTypeGroup->identifier] = array_filter(
$this->contentTypeService->loadContentTypes($contentTypeGroup),
function (ContentType $contentType) {
return !in_array($contentType->id, $this->excludedContentTypeIds);
});
}
return $contentTypes;
}),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment