Skip to content

Commit

Permalink
Merge branch 'v4' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/helpers/DataHelper.php
#	src/web/assets/feedme/dist/FeedMe.js
#	src/web/assets/feedme/dist/FeedMe.js.map
#	src/web/assets/feedme/dist/css/FeedMe.css
#	src/web/assets/feedme/dist/css/FeedMe.css.map
  • Loading branch information
angrybrad committed Nov 25, 2023
2 parents c30fb38 + ec9ed23 commit 734842d
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ tests/.env
/tests/_craft/storage
/.sass-cache
/tests/_craft/.env
/src/web/assets/feedme/.env
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Added the ability to provide a filename for importing assets via the assets field if you choose to “Create asset from URL”. ([#749](https://github.com/craftcms/feed-me/issues/749))
- Fixed a bug where you could not override feed settings with `false` if you used a `config/feed-me.php` config file. ([#1352](https://github.com/craftcms/feed-me/issues/1352))
- Fixed a bug where `queueTtr`, `queueMaxRetry`, and `assetDownloadCurl` settings were being ignored if you used a `config/feed-me.php` config file. ([#1356](https://github.com/craftcms/feed-me/issues/1356))
- Fixed a PHP error that could occur when you map an entry field with a default value and there is an empty value in the feed. ([#1250](https://github.com/craftcms/feed-me/pull/1250))
Expand Down
31 changes: 31 additions & 0 deletions src/events/AssetFilenameEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace craft\feedme\events;

use yii\base\Event;

class AssetFilenameEvent extends Event
{
// Properties
// =========================================================================

/**
* @var
*/
public $field;

/**
* @var
*/
public $element;

/**
* @var
*/
public $fieldValue;

/**
* @var
*/
public $filenames;
}
43 changes: 39 additions & 4 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use craft\elements\Asset as AssetElement;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\events\AssetFilenameEvent;
use craft\feedme\helpers\AssetHelper;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;
Expand All @@ -23,6 +24,8 @@
*/
class Assets extends Field implements FieldInterface
{
const EVENT_ASSET_FILENAME = 'onAssetFilename';

// Properties
// =========================================================================

Expand Down Expand Up @@ -113,6 +116,21 @@ public function parseField(): mixed
$urlsToUpload = [];
$base64ToUpload = [];

$filenamesFromFeed = $upload ? DataHelper::fetchArrayValue($this->feedData, $this->fieldInfo, 'options.filenameNode') : null;

// Fire an 'onAssetFilename' event
$event = new AssetFilenameEvent([
'field' => $this->field,
'element' => $this->element,
'fieldValue' => $value,
'filenames' => $filenamesFromFeed,
]);

$this->trigger(self::EVENT_ASSET_FILENAME, $event);

// Allow event to overwrite filenames to be used
$filenamesFromFeed = $event->filenames;

foreach ($value as $key => $dataValue) {
// Prevent empty or blank values (string or array), which match all elements
if (empty($dataValue) && empty($default)) {
Expand Down Expand Up @@ -155,8 +173,15 @@ public function parseField(): mixed
// If we're uploading files, this will need to be an absolute URL. If it is, save until later.
// We also don't check for existing assets here, so break out instantly.
if ($upload && UrlHelper::isAbsoluteUrl($dataValue)) {
$urlsToUpload[$key] = $dataValue;
$filename = AssetHelper::getRemoteUrlFilename($dataValue);
$urlsToUpload[$key]['value'] = $dataValue;

if (isset($filenamesFromFeed[$key])) {
$filename = $filenamesFromFeed[$key] . '.' . AssetHelper::getRemoteUrlExtension($urlsToUpload[$key]['value']);
$urlsToUpload[$key]['newFilename'] = $filename;
} else {
$filename = AssetHelper::getRemoteUrlFilename($dataValue);
$urlsToUpload[$key]['newFilename'] = null;
}
} else {
$filename = basename($dataValue);
}
Expand Down Expand Up @@ -190,8 +215,18 @@ public function parseField(): mixed

if ($upload) {
if ($urlsToUpload) {
$uploadedElements = AssetHelper::fetchRemoteImage($urlsToUpload, $this->fieldInfo, $this->feed, $this->field, $this->element);
$foundElements = array_merge($foundElements, $uploadedElements);
foreach ($urlsToUpload as $item) {
$uploadedElements = AssetHelper::fetchRemoteImage(
[$item['value']],
$this->fieldInfo,
$this->feed,
$this->field,
$this->element,
null,
$item['newFilename']
);
$foundElements = array_merge($foundElements, $uploadedElements);
}
}

if ($base64ToUpload) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/DataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public static function fetchSimpleValue($feedData, $fieldInfo): mixed
* @param $fieldInfo
* @return array|ArrayAccess|mixed
*/
public static function fetchArrayValue($feedData, $fieldInfo): mixed
public static function fetchArrayValue($feedData, $fieldInfo, $nodeName = 'node'): mixed
{
$value = null;

$node = Hash::get($fieldInfo, 'node');
$node = Hash::get($fieldInfo, $nodeName);

$dataDelimiter = Plugin::$plugin->service->getConfig('dataDelimiter');

Expand Down
10 changes: 10 additions & 0 deletions src/templates/_includes/fields/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,15 @@
],
value: hash_get(feed.fieldMapping, optionsPath ~ '.conflict') ?: '',
}) }}

<div class="asset-filename-node">
{{ forms.selectField({
label: 'Use this filename for assets created from URL:'|t('feed-me'),
name: 'options[filenameNode]',
value: hash_get(feed.fieldMapping, optionsPath ~ '.filenameNode') ?: '',
options: feedData,
class: 'selectize fullwidth',
}) }}
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion src/web/assets/feedme/dist/FeedMe.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/feedme/dist/FeedMe.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/feedme/dist/css/FeedMe.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/feedme/dist/css/FeedMe.css.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/web/assets/feedme/src/js/feed-me.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ $(function () {
var $label = $(this)
.parents('.field-extra-settings')
.find('.asset-label-hide');
var $filenameNode = $(this)
.parents('.field-extra-settings')
.find('.asset-filename-node');

if ($(this).prop('checked')) {
$label.css({opacity: 1, visibility: 'visible'});
$options.css({opacity: 1, visibility: 'visible'});
$filenameNode.show();
} else {
$label.css({opacity: 0, visibility: 'hidden'});
$options.css({opacity: 0, visibility: 'hidden'});
$filenameNode.hide();
}
});

Expand Down
38 changes: 24 additions & 14 deletions src/web/assets/feedme/src/scss/feed-me.scss
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,32 @@ body.ltr .table-feed-me .thin.action {
// Assets Uploading
//

.feedme-mapping .field-extra-settings .assets-uploads .asset-label-hide {
visibility: hidden;
}
.feedme-mapping .field-extra-settings .assets-uploads {
.asset-label-hide {
visibility: hidden;
}

.feedme-mapping .field-extra-settings .assets-uploads .checkboxfield {
float: left;
margin-right: 10px;
padding-top: 0;
margin-bottom: 3px;
z-index: 1;
}
.checkboxfield {
float: left;
margin-right: 10px;
padding-top: 0;
margin-bottom: 3px;
z-index: 1;
}

.feedme-mapping .field-extra-settings .assets-uploads .select {
height: auto;
opacity: 0;
visibility: hidden;
.select {
height: auto;
opacity: 0;
visibility: hidden;

&.selectize {
height: 32px;
}
}

.asset-filename-node {
margin-top: 15px;
}
}

.feedme-mapping .field-extra-settings .assets-create .heading label {
Expand Down

0 comments on commit 734842d

Please sign in to comment.