1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Recipe plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
6
|
|
|
* conversion, portion calculation, and JSON-LD microdata support |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2020 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\recipe\integrations; |
13
|
|
|
|
14
|
|
|
use Cake\Utility\Hash; |
15
|
|
|
use craft\feedme\base\Field; |
16
|
|
|
use craft\feedme\base\FieldInterface; |
17
|
|
|
use craft\feedme\helpers\DataHelper; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
21
|
|
|
* @package Recipe |
|
|
|
|
22
|
|
|
* @since 1.2.0 |
|
|
|
|
23
|
|
|
* |
24
|
|
|
* @property-read string $mappingTemplate |
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class RecipeFeedMeField extends Field implements FieldInterface |
27
|
|
|
{ |
28
|
|
|
// Properties |
29
|
|
|
// ========================================================================= |
30
|
|
|
|
31
|
|
|
public static $name = 'Recipe'; |
32
|
|
|
public static $class = 'nystudio107\recipe\fields\Recipe'; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
// Templates |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
public function getMappingTemplate(): string |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return 'recipe/_integrations/feed-me'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
// Public Methods |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
public function parseField(): mixed |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$preppedData = []; |
50
|
|
|
|
51
|
|
|
$fields = Hash::get($this->fieldInfo, 'fields'); |
52
|
|
|
|
53
|
|
|
if (!$fields) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($fields as $subFieldHandle => $subFieldInfo) { |
58
|
|
|
// Check for sub-sub fields - bit dirty... |
59
|
|
|
$subfields = Hash::get($subFieldInfo, 'fields'); |
60
|
|
|
|
61
|
|
|
if ($subfields) { |
62
|
|
|
foreach ($subfields as $subSubFieldHandle => $subSubFieldInfo) { |
63
|
|
|
// Handle array data, man I hate Feed Me's data mapping now... |
64
|
|
|
$content = DataHelper::fetchArrayValue($this->feedData, $subSubFieldInfo); |
65
|
|
|
|
66
|
|
|
if (is_array($content)) { |
67
|
|
|
foreach ($content as $key => $value) { |
68
|
|
|
$preppedData[$subFieldHandle][$key][$subSubFieldHandle] = $value; |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$preppedData[$subFieldHandle][$subSubFieldHandle] = $content; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$preppedData[$subFieldHandle] = DataHelper::fetchValue($this->feedData, $subFieldInfo); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Protect against sending an empty array |
80
|
|
|
if (!$preppedData) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $preppedData; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|