|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\easy_entity_reader\Converters; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Field\FieldItemInterface; |
|
6
|
|
|
use Drupal\field_collection\Plugin\Field\FieldType\FieldCollection; |
|
7
|
|
|
use Drupal\easy_entity_reader\EntityWrapper; |
|
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
9
|
|
|
use Drupal\field_collection\Entity\FieldCollectionItem; |
|
10
|
|
|
use Drupal\Core\Language\LanguageInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Convert collection class to value. |
|
14
|
|
|
* |
|
15
|
|
|
* Class DefaultConverter. |
|
16
|
|
|
*/ |
|
17
|
|
|
class CollectionConverter implements ConverterInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var EntityWrapper |
|
21
|
|
|
*/ |
|
22
|
|
|
private $entityWrapper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityTypeManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param EntityWrapper $entityWrapper |
|
31
|
|
|
* @param EntityTypeManagerInterface $entityManager |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(EntityWrapper $entityWrapper, |
|
34
|
|
|
EntityTypeManagerInterface $entityManager) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->entityWrapper = $entityWrapper; |
|
37
|
|
|
$this->entityManager = $entityManager; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param FieldItemInterface $value |
|
42
|
|
|
*/ |
|
43
|
|
|
public function convert(FieldItemInterface $value) |
|
44
|
|
|
{ |
|
45
|
|
|
$values = $value->getValue(); |
|
46
|
|
|
if(isset($values['value'])) { |
|
47
|
|
|
$node = $this->entityManager->getStorage('field_collection_item')->load($values['value']); |
|
48
|
|
|
} |
|
49
|
|
|
else { |
|
50
|
|
|
/* |
|
51
|
|
|
* If there is a default language this is because there is |
|
52
|
|
|
* a field collection in another. This remove the language default |
|
53
|
|
|
*/ |
|
54
|
|
|
|
|
55
|
|
|
if(isset($values[LanguageInterface::LANGCODE_DEFAULT])) { |
|
56
|
|
|
$values = $values[LanguageInterface::LANGCODE_DEFAULT]; |
|
57
|
|
|
} |
|
58
|
|
|
$valuesFormat = []; |
|
59
|
|
|
|
|
60
|
|
|
//If the array is not format with language, the value is not store in object |
|
61
|
|
|
// Change the index to use the default value of language |
|
62
|
|
|
$this->convertArrayToValues($values, $valuesFormat); |
|
63
|
|
|
$node = new FieldCollectionItem($valuesFormat, 'field_collection_item', $values['field_collection_item']->bundle()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if($node) { |
|
67
|
|
|
return $this->entityWrapper->wrap($node); |
|
68
|
|
|
} |
|
69
|
|
|
else { |
|
70
|
|
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function convertArrayToValues($toConvert, &$convert) { |
|
75
|
|
|
foreach ($toConvert as $k => $v) { |
|
76
|
|
|
if(is_array($v) && isset($v[0]) && count($v) == 1) { |
|
77
|
|
|
$convert[$k][LanguageInterface::LANGCODE_DEFAULT] = $v[0]; |
|
78
|
|
|
} |
|
79
|
|
|
elseif(count($v) > 1) { |
|
80
|
|
|
$convert[$k][LanguageInterface::LANGCODE_DEFAULT] = []; |
|
81
|
|
|
$this->convertArrayToValues($v, $convert[$k][LanguageInterface::LANGCODE_DEFAULT]); |
|
82
|
|
|
} |
|
83
|
|
|
else { |
|
84
|
|
|
$convert[$k] = $v; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns whether the FieldItemInterface can be converted or not. |
|
91
|
|
|
* |
|
92
|
|
|
* @param FieldItemInterface $value |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function canConvert(FieldItemInterface $value) : bool |
|
97
|
|
|
{ |
|
98
|
|
|
return $value instanceof FieldCollection; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|