Skip to content

Commit

Permalink
Merge branch 'v4' of https://github.com/craftcms/feed-me into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybrad committed Nov 25, 2023
2 parents 8f5bdd7 + d134755 commit ec9ed23
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 24 deletions.
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))

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 @@ -22,6 +23,8 @@
*/
class Assets extends Field implements FieldInterface
{
const EVENT_ASSET_FILENAME = 'onAssetFilename';

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

Expand Down Expand Up @@ -112,6 +115,21 @@ public function parseField()
$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 @@ -154,8 +172,15 @@ public function parseField()
// 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 @@ -189,8 +214,18 @@ public function parseField()

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 @@ -58,11 +58,11 @@ public static function fetchSimpleValue($feedData, $fieldInfo)
* @param $fieldInfo
* @return array|ArrayAccess|mixed
*/
public static function fetchArrayValue($feedData, $fieldInfo)
public static function fetchArrayValue($feedData, $fieldInfo, $nodeName = 'node')
{
$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 @@ -253,22 +253,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 ec9ed23

Please sign in to comment.