Skip to content
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

[issue-2901] fix(fixtures): non existent data mapped for fields of type select #2954

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions assets/js/app/editor/Components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,22 @@ export default {
},
},
mounted() {
const _values = this.value.map ? this.value : [this.value];
const _values = !this.value ? [] : this.value.map ? this.value : [this.value];
const _options = this.options;

let filterSelectedItems = _values.map(value => {
const item = _options.filter(opt => opt.key === value);
if (item) {
return item[0];
}
});
/**
* Filter method is necessary for required fields because the empty option is not
* set. If the field is empty, "filterSelectedItems" will contain an undefined
* element and "select" will not be filled with the first available option.
*/
let filterSelectedItems = _values
.map(value => {
const item = _options.filter(opt => opt.key === value);
if (item.length > 0) {
return item[0];
}
})
.filter(item => undefined !== item);

if (filterSelectedItems.length === 0) {
filterSelectedItems = [_options[0]];
Expand Down
69 changes: 69 additions & 0 deletions src/DataFixtures/ContentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bolt\Configuration\FileLocations;
use Bolt\Entity\Content;
use Bolt\Entity\Field;
use Bolt\Entity\Field\SelectField;
use Bolt\Enum\Statuses;
use Bolt\Repository\FieldRepository;
use Bolt\Utils\FakeContent;
Expand Down Expand Up @@ -75,6 +76,8 @@ public function load(ObjectManager $manager): void
$this->loadContent($manager);

$manager->flush();

$this->setSelectFieldsMappedWithContent($manager);
}

private function loadContent(ObjectManager $manager): void
Expand Down Expand Up @@ -366,6 +369,14 @@ private function getFieldTypeValue(DeepCollection $field, bool $singleton, Conte
];
}

break;
case 'select':
$data = [];

if (isset($field['values']) && ($field['values'] instanceof ContentType)) {
$data = [\array_rand($field['values']->all())];
}

break;
default:
$data = [$this->faker->sentence(6, true)];
Expand Down Expand Up @@ -474,4 +485,62 @@ private function getPreset(string $slug): array

return $preset;
}

private function setSelectFieldsMappedWithContent(ObjectManager $manager): void
{
/** @var Content[] $allContent */
$allContent = $manager->getRepository(Content::class)->findAll();

foreach ($allContent as $content) {
$contentDefaultLocale = $content->getDefaultLocale();
$contentLocales = $content->getLocales();
foreach ($content->getFields() as $field) {
if (! $this->isSelectFieldAndMappedWithContent($field)) {
continue;
}

/** @var SelectField $field */
$contentType = $field->getContentType();

if (! \is_string($contentType)) {
continue;
}

try {
/** @var Content $randomReference */
$randomReference = $this->getRandomReference(\sprintf('content_%s', $contentType));
} catch (\Exception $exception) {
continue;
}

$content->setFieldValue($field->getName(), [$randomReference->getId()], $this->defaultLocale);

foreach ($contentLocales as $locale) {
if (
$locale !== $contentDefaultLocale
&& array_search($locale, $contentLocales->toArray(), true) !== (count($contentLocales) - 1)
) {
$content->setFieldValue($field->getName(), [$randomReference->getId()], $locale);
}
}
}

$manager->persist($content);
}

$manager->flush();
}

private function isSelectFieldAndMappedWithContent(Field $field): bool
{
if (! $field instanceof SelectField) {
return FALSE;
}

if (! $field->isContentSelect()) {
return FALSE;
}

return TRUE;
}
}
11 changes: 11 additions & 0 deletions src/Twig/FieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ private function selectOptionsContentTypeCache(string $contentTypeSlug, array $p

$options = [];

// We need to add this as a 'dummy' option for when the user is allowed
// not to pick an option. This is needed, because otherwise the `select`
// would default to the one.
if (! $field->getDefinition()->get('required', true)) {
$options[] = [
'key' => '',
'value' => '',
'selected' => false,
];
}

foreach ($records as $record) {
if ($field->getDefinition()->get('mode') === 'format') {
$formattedKey = $this->contentHelper->get($record, $field->getDefinition()->get('format'));
Expand Down