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

Add ability to create sub entries without a title match #660

Open
wants to merge 11 commits into
base: 5.x
Choose a base branch
from
59 changes: 39 additions & 20 deletions src/fields/Entries.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace craft\feedme\fields;

use Cake\Utility\Hash;
Expand All @@ -13,6 +12,7 @@
use craft\feedme\Plugin;
use craft\fields\Entries as EntriesField;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use craft\helpers\Json;
use Throwable;
use yii\base\Exception;
Expand Down Expand Up @@ -165,9 +165,11 @@ public function parseField(): mixed

Plugin::info('Found `{i}` existing entries: `{j}`', ['i' => 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);
Expand All @@ -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');
Expand All @@ -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);
Expand Down