|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\easy_entity_reader\Converters; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Field\FieldItemInterface; |
|
6
|
|
|
use Drupal\easy_entity_reader\EntityWrapper; |
|
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
8
|
|
|
use Drupal\image\Plugin\Field\FieldType\ImageItem; |
|
9
|
|
|
use Drupal\easy_entity_reader\CompositeArrayAccess; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Convert image class to value. |
|
13
|
|
|
* |
|
14
|
|
|
* Class DefaultConverter. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ImageItemConverter implements ConverterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var EntityWrapper |
|
20
|
|
|
*/ |
|
21
|
|
|
private $entityWrapper; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var EntityTypeManagerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $entityManager; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param EntityWrapper $entityWrapper |
|
30
|
|
|
* @param EntityTypeManagerInterface $entityManager |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(EntityWrapper $entityWrapper, |
|
33
|
|
|
EntityTypeManagerInterface $entityManager) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->entityWrapper = $entityWrapper; |
|
36
|
|
|
$this->entityManager = $entityManager; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param FieldItemInterface $value |
|
41
|
|
|
*/ |
|
42
|
|
|
public function convert(FieldItemInterface $value) |
|
43
|
|
|
{ |
|
44
|
|
|
$values = $value->getValue(); |
|
45
|
|
|
$type = str_replace('default:', '', $value->getParent()->getSettings()['handler']); |
|
|
|
|
|
|
46
|
|
|
$node = $this->entityManager->getStorage($type)->load($values['target_id']); |
|
47
|
|
|
|
|
48
|
|
|
$frontArray = []; |
|
49
|
|
|
if (isset($values['alt'])) { |
|
50
|
|
|
$frontArray['alt'] = $values['alt']; |
|
51
|
|
|
} |
|
52
|
|
|
if (isset($values['title'])) { |
|
53
|
|
|
$frontArray['title'] = $values['title']; |
|
54
|
|
|
} |
|
55
|
|
|
if (isset($values['width'])) { |
|
56
|
|
|
$frontArray['width'] = $values['width']; |
|
57
|
|
|
} |
|
58
|
|
|
if (isset($values['height'])) { |
|
59
|
|
|
$frontArray['height'] = $values['height']; |
|
60
|
|
|
} |
|
61
|
|
|
$wrap = $this->entityWrapper->wrap($node); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return new CompositeArrayAccess([$frontArray, $wrap]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns whether the FieldItemInterface can be converted or not. |
|
68
|
|
|
* |
|
69
|
|
|
* @param FieldItemInterface $value |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function canConvert(FieldItemInterface $value) : bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $value instanceof ImageItem; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: