diff --git a/src/fields/Entries.php b/src/fields/Entries.php index 6cf59dbc..b5371ce3 100644 --- a/src/fields/Entries.php +++ b/src/fields/Entries.php @@ -1,5 +1,4 @@ count($foundElements), 'j' => Json::encode($foundElements)]); - // Check if we should create the element. But only if title is provided (for the moment) - if ((count($ids) == 0) && $create && $match === 'title') { - $foundElements[] = $this->_createElement($dataValue); + // Check if we should create the element. + if (count($ids) == 0) { + if ($create) { + $foundElements[] = $this->_createElement($dataValue, $match); + } } $nodeKey = $this->getArrayKeyFromNode($node); @@ -193,18 +195,18 @@ public function parseField(): mixed return $foundElements; } - // Private Methods // ========================================================================= /** * @param $dataValue + * @param string $match * @return int|null * @throws Throwable * @throws ElementNotFoundException * @throws Exception */ - private function _createElement($dataValue): ?int + private function _createElement($dataValue, $match): ?int { $sectionId = Hash::get($this->fieldInfo, 'options.group.sectionId'); $typeId = Hash::get($this->fieldInfo, 'options.group.typeId'); @@ -219,32 +221,49 @@ private function _createElement($dataValue): ?int } $element = new EntryElement(); - $element->title = $dataValue; $element->sectionId = $sectionId; $element->typeId = $typeId; $siteId = Hash::get($this->feed, 'siteId'); $section = Craft::$app->getSections()->getSectionById($element->sectionId); - if ($siteId) { - $element->siteId = $siteId; + if ($match === 'title') { + $element->title = $dataValue; + + if ($siteId) { + $element->siteId = $siteId; + + // Set the default site status based on the section's settings + foreach ($section->getSiteSettings() as $siteSettings) { + if ($siteSettings->siteId == $siteId) { + $element->enabledForSite = $siteSettings->enabledByDefault; + break; + } + } + } else { + // Set the default entry status based on the section's settings + foreach ($section->getSiteSettings() as $siteSettings) { + if (!$siteSettings->enabledByDefault) { + $element->enabled = false; + } - // Set the default site status based on the section's settings - foreach ($section->getSiteSettings() as $siteSettings) { - if ($siteSettings->siteId == $siteId) { - $element->enabledForSite = $siteSettings->enabledByDefault; break; } } } else { - // Set the default entry status based on the section's settings - foreach ($section->getSiteSettings() as $siteSettings) { - if (!$siteSettings->enabledByDefault) { - $element->enabled = false; - } - - break; + // If the new element has no title: Create a random title and disable the element. + $randomString = '__feed-me__' . strtolower(StringHelper::randomString(10)); + $entryType = $element->getType(); + + // If the element has no title field, we only set a random slug. + // Otherwise we would not be able to save the element. + if ($entryType->hasTitleField) { + $element->title = $randomString; + } else { + $element->slug = $randomString; } + $element->setFieldValue(str_replace('field_', '', $match), $dataValue); + $element->enabled = false; } $element->setScenario(BaseElement::SCENARIO_ESSENTIALS);