-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiledGallery.php
50 lines (44 loc) · 1.35 KB
/
TiledGallery.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
<?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 TiledGallery extends BaseBlockTransformer
{
public static function blockName(): string
{
return 'jetpack/tiled-gallery';
}
public function render(array $data, Entry $entry): string
{
$assetIds = Collection::make($data['attrs']['ids'])
->map(function(int $id) {
try {
return $this->command->import(Media::SLUG, $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);
});
}
}