-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds an option to export CSV with labels as heading #361
base: 2.5
Are you sure you want to change the base?
Changes from all commits
6f82a79
006312c
5828b8e
120be44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\FormBundle\ListBuilder; | ||
|
||
use Sulu\Bundle\FormBundle\Entity\Dynamic; | ||
use Sulu\Bundle\FormBundle\Entity\Form; | ||
|
||
class DynamicLabeledListBuilder extends DynamicListBuilder | ||
{ | ||
private array $formFieldsLabelCache = []; | ||
|
||
public function build(Dynamic $dynamic, string $locale): array | ||
{ | ||
$entry = parent::build($dynamic, $locale); | ||
|
||
if (empty($entry)) { | ||
return $entry; | ||
} | ||
|
||
$labels = $this->getLabels($dynamic->getForm(), $locale); | ||
$entryLabeled = []; | ||
|
||
foreach ($entry[0] as $key => $value) { | ||
$entryLabeled[$labels[$key] ?? $key] = $value; | ||
} | ||
|
||
return [$entryLabeled]; | ||
} | ||
|
||
private function getLabels(Form $form, string $locale): array | ||
{ | ||
if (!isset($this->formFieldsLabelCache[$form->getId()])) { | ||
$labels = []; | ||
|
||
foreach ($form->getFields() as $field) { | ||
if (\in_array($field->getType(), Dynamic::$HIDDEN_TYPES)) { | ||
continue; | ||
} | ||
|
||
$translation = $field->getTranslation($locale, false, true); | ||
|
||
if ($translation) { | ||
$label = $translation->getShortTitle() ?: \strip_tags($translation->getTitle()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was testing this and I found an edge case: The "spacer" field does have a translation, but both IMHO when there is no translation or all titles are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found two more special fields that need different handling: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are some other special fields that I cannot test like recaptcha etc. which probably should be checked as well how it behaves with this new feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there is already a constant with these special fields: https://github.com/sulu/SuluFormBundle/blob/2.5/Entity/Dynamic.php#L36 And the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, sorry for the delay. I've updated the PR with your suggestions. |
||
$labels[$field->getKey()] = $label; | ||
} | ||
} | ||
|
||
$this->formFieldsLabelCache[$form->getId()] = $labels; | ||
} | ||
|
||
return $this->formFieldsLabelCache[$form->getId()]; | ||
} | ||
} |
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.
Interestingly, this is actually fixing a bug. The second argument for the
build
method inDynamicListFactoryInterface
is the locale. But until now, theview
was passed instead.The only reason it was working is because the third argument has a fallback and in the existing
DynamicListBuilder
thelocale
(which would bedefault
) is not being used.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.
I just noticed #360 😄