diff --git a/assets/js/app/editor/Components/Select.vue b/assets/js/app/editor/Components/Select.vue index 106489794..2f1b85984 100644 --- a/assets/js/app/editor/Components/Select.vue +++ b/assets/js/app/editor/Components/Select.vue @@ -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]]; diff --git a/src/DataFixtures/ContentFixtures.php b/src/DataFixtures/ContentFixtures.php index b7ae0a975..d1ac25f41 100644 --- a/src/DataFixtures/ContentFixtures.php +++ b/src/DataFixtures/ContentFixtures.php @@ -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; @@ -75,6 +76,8 @@ public function load(ObjectManager $manager): void $this->loadContent($manager); $manager->flush(); + + $this->setSelectFieldsMappedWithContent($manager); } private function loadContent(ObjectManager $manager): void @@ -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; case 'geolocation': $data = ['selected' => 'latlong', 'zoom' => '7', 'search' => '']; @@ -374,6 +385,7 @@ private function getFieldTypeValue(DeepCollection $field, bool $singleton, Conte $data['long'] =$coordinates['longitude']; $data = [json_encode($data)]; + break; default: $data = [$this->faker->sentence(6, true)]; @@ -482,4 +494,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; + } } diff --git a/src/Twig/FieldExtension.php b/src/Twig/FieldExtension.php index 60ea08483..775794101 100644 --- a/src/Twig/FieldExtension.php +++ b/src/Twig/FieldExtension.php @@ -256,10 +256,14 @@ private function selectOptionsContentType(Field $field): Collection $options = []; - if (! $field->getDefinition()->get('required')) { + // 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, ]; } @@ -303,4 +307,4 @@ public function selectOptionsHelper(string $contentTypeSlug, array $params, Fiel return $options; } -} +} \ No newline at end of file