-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGallery.php
51 lines (45 loc) · 1.43 KB
/
Gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/
namespace craft\wpimport\blocktransformers;
use craft\elements\Entry;
use craft\wpimport\BaseBlockTransformer;
use craft\wpimport\generators\entrytypes\Media as MediaEntryType;
use craft\wpimport\generators\fields\Media as MediaField;
use craft\wpimport\importers\Media;
use Illuminate\Support\Collection;
use Throwable;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Gallery extends BaseBlockTransformer
{
public static function blockName(): string
{
return 'core/gallery';
}
public function render(array $data, Entry $entry): string
{
$assetIds = Collection::make($data['innerBlocks'])
->filter(fn(array $block) => $block['blockName'] === 'core/image')
->map(function(array $block) {
try {
return $this->command->import(Media::SLUG, $block['attrs']['id']);
} catch (Throwable) {
return null;
}
})
->filter()
->all();
if (empty($assetIds)) {
return '';
}
return $this->createNestedEntry($entry, function(Entry $nestedEntry) use ($assetIds) {
$nestedEntry->setTypeId(MediaEntryType::get()->id);
$nestedEntry->setFieldValue(MediaField::get()->handle, $assetIds);
});
}
}