Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CrmUserContact | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
query | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Plugin\views\field; |
4 | |
5 | use Drupal\Core\Field\FieldDefinitionInterface; |
6 | use Drupal\Core\Link; |
7 | use Drupal\Core\Path\CurrentPathStack; |
8 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
9 | use Drupal\Core\Url; |
10 | use Drupal\crm\CrmUserContactSyncRelationInterface; |
11 | use Drupal\views\Plugin\views\field\FieldPluginBase; |
12 | use Drupal\views\ResultRow; |
13 | use Symfony\Component\DependencyInjection\ContainerInterface; |
14 | |
15 | /** |
16 | * User Contact field plugin. |
17 | * |
18 | * @ViewsField("crm_user_contact") |
19 | */ |
20 | class CrmUserContact extends FieldPluginBase implements ContainerFactoryPluginInterface { |
21 | |
22 | /** |
23 | * The crm core user sync relation service. |
24 | * |
25 | * @var \Drupal\crm\CrmCoreUserSyncRelation |
26 | */ |
27 | protected $service; |
28 | |
29 | /** |
30 | * The current path service. |
31 | * |
32 | * @var \Drupal\Core\Path\CurrentPathStack |
33 | */ |
34 | protected $currentPath; |
35 | |
36 | /** |
37 | * Constructs a StringFormatter instance. |
38 | * |
39 | * @param string $plugin_id |
40 | * The plugin_id for the formatter. |
41 | * @param mixed $plugin_definition |
42 | * The plugin implementation definition. |
43 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
44 | * The definition of the field to which the formatter is associated. |
45 | * @param array $settings |
46 | * The formatter settings. |
47 | * @param string $label |
48 | * The formatter label display setting. |
49 | * @param string $view_mode |
50 | * The view mode. |
51 | * @param array $third_party_settings |
52 | * Any third party settings settings. |
53 | * @param \Drupal\crm\CrmUserContactSyncRelationInterface $crm_user_contact |
54 | * The crm user service. |
55 | * @param \Drupal\Core\Path\CurrentPathStack $current_path |
56 | * The current path service. |
57 | */ |
58 | public function __construct( |
59 | $plugin_id, |
60 | $plugin_definition, |
61 | FieldDefinitionInterface $field_definition, |
62 | array $settings, |
63 | $label, |
64 | $view_mode, |
65 | array $third_party_settings, |
66 | CrmUserContactSyncRelationInterface $crm_user_contact, |
67 | CurrentPathStack $current_path, |
68 | ) { |
69 | parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); |
70 | $this->service = $crm_user_contact; |
71 | $this->currentPath = $current_path; |
72 | } |
73 | |
74 | /** |
75 | * {@inheritdoc} |
76 | */ |
77 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
78 | return new static( |
79 | $plugin_id, |
80 | $plugin_definition, |
81 | $configuration['field_definition'], |
82 | $configuration['settings'], |
83 | $configuration['label'], |
84 | $configuration['view_mode'], |
85 | $configuration['third_party_settings'], |
86 | $container->get('crm.user'), |
87 | $container->get('path.current') |
88 | ); |
89 | } |
90 | |
91 | /** |
92 | * {@inheritdoc} |
93 | */ |
94 | public function query() { |
95 | // Leave empty to avoid a query on this field. |
96 | } |
97 | |
98 | /** |
99 | * {@inheritdoc} |
100 | */ |
101 | public function render(ResultRow $values) { |
102 | |
103 | $entity = $values->_entity; |
104 | |
105 | $entity_type = $entity->getEntityTypeId(); |
106 | $entity_id = $entity->id(); |
107 | |
108 | $current_path = $this->currentPath->getPath(); |
109 | if ($entity_type == 'crm_contact') { |
110 | $uid = $this->service->getUserIdFromContactId($entity_id); |
111 | if (empty($uid)) { |
112 | $url = Url::fromRoute( |
113 | 'entity.crm_user_contact.add_form', |
114 | ['crm_contact' => $entity_id], |
115 | [ |
116 | 'query' => [ |
117 | 'destination' => Url::fromUri("internal:$current_path")->toString(), |
118 | ], |
119 | ] |
120 | ); |
121 | $link = Link::fromTextAndUrl('Add Relation', $url)->toString(); |
122 | } |
123 | else { |
124 | $rid = $this->service->getRelationIdFromContactId($entity_id); |
125 | $url = Url::fromRoute( |
126 | 'entity.crm_user_contact.edit_form', |
127 | ['crm_user_contact' => $rid], |
128 | [ |
129 | 'query' => [ |
130 | 'destination' => Url::fromUri("internal:$current_path")->toString(), |
131 | ], |
132 | ] |
133 | ); |
134 | $link = Link::fromTextAndUrl($rid, $url)->toString(); |
135 | } |
136 | |
137 | } |
138 | elseif ($entity_type == 'user') { |
139 | $contact_id = $this->service->getContactIdFromUserId($entity_id); |
140 | if (empty($contact_id)) { |
141 | $url = Url::fromRoute( |
142 | 'entity.crm_user_contact.add_form', |
143 | ['user' => $entity_id], |
144 | [ |
145 | 'query' => [ |
146 | 'destination' => Url::fromUri("internal:$current_path")->toString(), |
147 | ], |
148 | ] |
149 | ); |
150 | $link = Link::fromTextAndUrl('Add Relation', $url)->toString(); |
151 | } |
152 | else { |
153 | $rid = $this->service->getRelationIdFromUserId($entity_id); |
154 | $url = Url::fromRoute( |
155 | 'entity.crm_user_contact.edit_form', |
156 | ['crm_user_contact' => $rid], |
157 | [ |
158 | 'query' => [ |
159 | 'destination' => Url::fromUri("internal:$current_path")->toString(), |
160 | ], |
161 | ] |
162 | ); |
163 | $link = Link::fromTextAndUrl($rid, $url)->toString(); |
164 | } |
165 | } |
166 | |
167 | return $link; |
168 | } |
169 | |
170 | } |