Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 70 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;
case 'geolocation':
$data = ['selected' => 'latlong', 'zoom' => '7', 'search' => ''];
Expand All @@ -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)];
Expand Down Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions src/Twig/FieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down Expand Up @@ -303,4 +307,4 @@ public function selectOptionsHelper(string $contentTypeSlug, array $params, Fiel

return $options;
}
}
}