EntityDrupalWrapper::wrapper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
final class EntityDrupalWrapper
8
{
9
    /**
10
     * The Drupal entity name.
11
     *
12
     * @var string
13
     */
14
    private $entity = '';
15
    /**
16
     * The Drupal entity bundle.
17
     *
18
     * @var string
19
     */
20
    private $bundle = '';
21
    /**
22
     * @var array
23
     */
24
    private $fields = [
25
        'locators' => [],
26
        'required' => [],
27
    ];
28
    /**
29
     * @var array
30
     */
31
    private $instances = [];
32
33
    /**
34
     * @param string $entity
35
     * @param string $bundle
36
     */
37
    public function __construct($entity, $bundle = '')
38
    {
39
        $this->entity = $entity;
40
41
        if (empty($bundle)) {
42
            $this->bundle = $this->entity;
43
        }
44
45
        // The fields in "locators" array stored by machine name of a field and duplicates by field label.
46
        foreach (field_info_instances($this->entity, $this->bundle) as $field_name => $definition) {
47
            $this->fields['locators'][$definition['label']] = $this->fields['locators'][$field_name] = $field_name;
48
49
            if ($definition['required']) {
50
                $this->fields['required'][$field_name] = $definition['label'];
51
            }
52
53
            $this->instances[$field_name] = $definition;
54
        }
55
    }
56
57
    /**
58
     * Load the Drupal entity wrapper.
59
     *
60
     * @see entity_metadata_wrapper()
61
     *
62
     * @param mixed $data
63
     * @param array $info
64
     *
65
     * @return \EntityDrupalWrapper
66
     */
67
    public function wrapper($data = null, array $info = [])
68
    {
69
        return entity_metadata_wrapper($this->entity, $data, $info);
70
    }
71
72
    /**
73
     * @param string $field_name
74
     *   Machine name or label of a field.
75
     *
76
     * @return string
77
     */
78
    public function getFieldNameByLocator($field_name)
79
    {
80
        return isset($this->fields['locators'][$field_name]) ? $this->fields['locators'][$field_name] : '';
81
    }
82
83
    /**
84
     * @return array[]
85
     */
86
    public function getRequiredFields()
87
    {
88
        return $this->fields['required'];
89
    }
90
91
    /**
92
     * @param string $field_name
93
     *   Machine name or label of a field.
94
     *
95
     * @return array[]
96
     *   Drupal field definition.
97
     */
98
    public function getFieldInfo($field_name)
99
    {
100
        $field_name = $this->getFieldNameByLocator($field_name);
101
102
        return empty($field_name) ? [] : field_info_field($field_name);
103
    }
104
105
    /**
106
     * @param string $field_name
107
     *   Machine name or label of a field.
108
     *
109
     * @return array[]
110
     *   Drupal field definition.
111
     */
112
    public function getFieldInstance($field_name)
113
    {
114
        $field_name = $this->getFieldNameByLocator($field_name);
115
116
        return empty($this->instances[$field_name]) ? [] : $this->instances[$field_name];
117
    }
118
}
119