@@ -45,29 +45,47 @@ final class TagArrayToStringTransformer implements DataTransformerInterface
|
||||
*
|
||||
* @see \Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform()
|
||||
*
|
||||
* @param string|null $value
|
||||
* @param array<string>|string|null $value
|
||||
* @return Tag[]
|
||||
* @throws TransformationFailedException
|
||||
*/
|
||||
public function reverseTransform(mixed $value): mixed
|
||||
public function reverseTransform(mixed $value): array
|
||||
{
|
||||
// check for empty tag list
|
||||
if ('' === $value || null === $value) {
|
||||
return [];
|
||||
}
|
||||
$names = array_filter(array_unique(array_map('trim', explode(',', $value))));
|
||||
if (!\is_array($value)) {
|
||||
$names = array_filter(array_unique(array_map('trim', explode(',', $value))));
|
||||
} else {
|
||||
$names = $value;
|
||||
}
|
||||
|
||||
// get the current tags and find the new ones that should be created
|
||||
$tags = $this->tagRepository->findBy(['name' => $names]);
|
||||
if ($this->create) {
|
||||
// works, because of the implicit case: (string) $tag
|
||||
$newNames = array_diff($names, $tags);
|
||||
$tags = [];
|
||||
foreach ($names as $tagName) {
|
||||
if ($tagName === null || $tagName === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($newNames as $name) {
|
||||
$tagName = trim($tagName);
|
||||
$tag = null;
|
||||
|
||||
if (is_numeric($tagName)) {
|
||||
$tag = $this->tagRepository->find($tagName);
|
||||
}
|
||||
|
||||
if ($tag === null) {
|
||||
$tag = $this->tagRepository->findTagByName($tagName);
|
||||
}
|
||||
|
||||
// get the current tags and find the new ones that should be created
|
||||
if ($this->create && $tag === null) {
|
||||
$tag = new Tag();
|
||||
$tag->setName(mb_substr($name, 0, 100));
|
||||
$tag->setName(mb_substr($tagName, 0, 100));
|
||||
$this->tagRepository->saveTag($tag);
|
||||
}
|
||||
|
||||
if ($tag !== null) {
|
||||
$tags[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ trait FormTrait
|
||||
return;
|
||||
}
|
||||
|
||||
$options['projects'] = $data['project'];
|
||||
$options['projects'] = \is_string($data['project']) ? (int) $data['project'] : $data['project'];
|
||||
|
||||
$event->getForm()->add('activity', ActivityType::class, $options);
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ trait ToolbarFormTrait
|
||||
|
||||
protected function addPageSizeChoice(FormBuilderInterface $builder): void
|
||||
{
|
||||
$builder->add('pageSize', PageSizeType::class);
|
||||
$builder->add('size', PageSizeType::class);
|
||||
}
|
||||
|
||||
protected function addUserRoleChoice(FormBuilderInterface $builder): void
|
||||
|
||||
@@ -36,13 +36,15 @@ final class TagsSelectType extends AbstractType
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
if (!$options['allow_create']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
|
||||
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
|
||||
/** @var array<string> $tagIds */
|
||||
$tagIds = $event->getData();
|
||||
|
||||
// this is mainly here, because the link from tags index page uses the non-array syntax
|
||||
if (\is_string($tagIds) || \is_int($tagIds)) {
|
||||
$tagIds = array_filter(array_unique(array_map('trim', explode(',', $tagIds))));
|
||||
}
|
||||
|
||||
if (!\is_array($tagIds)) {
|
||||
return;
|
||||
}
|
||||
@@ -59,13 +61,15 @@ final class TagsSelectType extends AbstractType
|
||||
$tag = $this->tagRepository->findTagByName($tagId);
|
||||
}
|
||||
|
||||
if ($tag === null) {
|
||||
if ($options['allow_create'] && $tag === null) {
|
||||
$tag = new Tag();
|
||||
$tag->setName(mb_substr($tagId, 0, 100));
|
||||
$tag->setName($tagId);
|
||||
$this->tagRepository->saveTag($tag);
|
||||
}
|
||||
|
||||
$tags[] = $tag->getId();
|
||||
if ($tag !== null) {
|
||||
$tags[] = $tag->getId();
|
||||
}
|
||||
}
|
||||
|
||||
$event->setData($tags);
|
||||
@@ -79,6 +83,9 @@ final class TagsSelectType extends AbstractType
|
||||
'class' => Tag::class,
|
||||
'label' => 'tag',
|
||||
'allow_create' => false,
|
||||
'choice_value' => function (Tag $tag) {
|
||||
return $tag->getId();
|
||||
},
|
||||
'choice_attr' => function (Tag $tag) {
|
||||
$color = $tag->getColor();
|
||||
if ($color === null) {
|
||||
|
||||
Reference in New Issue
Block a user