1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_tabs\Plugin\Field\FieldFormatter; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Xss; |
6
|
|
|
use Drupal\Core\Field\FormatterBase; |
7
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
8
|
|
|
use Drupal\Core\Url; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Plugin implementation of the 'text_with_tab' formatter. |
12
|
|
|
* |
13
|
|
|
* @FieldFormatter( |
14
|
|
|
* id = "text_with_tab", |
15
|
|
|
* label = @Translation("Text with tab"), |
16
|
|
|
* field_types = { |
17
|
|
|
* "text_with_tab", |
18
|
|
|
* }, |
19
|
|
|
* quickedit = { |
20
|
|
|
* "editor" = "form" |
21
|
|
|
* } |
22
|
|
|
* ) |
23
|
|
|
*/ |
24
|
|
|
class TextWithTabFormatter extends FormatterBase { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function viewElements(FieldItemListInterface $items, $langcode) { |
30
|
|
|
$elements = []; |
31
|
|
|
|
32
|
|
|
foreach ($items as $delta => $item) { |
33
|
|
|
$elements[$delta] = [ |
34
|
|
|
'#type' => 'container', |
35
|
|
|
'#attributes' => [ |
36
|
|
|
'id' => $this->getFieldId($items, $delta), |
37
|
|
|
], |
38
|
|
|
'#prefix' => '<h3 class="df-tools-tabs-accordion-title">' . Xss::filter($item->tab_title) . '</h3>', |
39
|
|
|
]; |
40
|
|
|
$elements[$delta][] = [ |
41
|
|
|
'#type' => 'processed_text', |
42
|
|
|
'#text' => $item->value, |
43
|
|
|
'#format' => $item->format, |
44
|
|
|
'#langcode' => $item->getLangcode(), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $elements; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function view(FieldItemListInterface $items, $langcode = NULL) { |
55
|
|
|
$build = [ |
56
|
|
|
'#type' => 'container', |
57
|
|
|
'#attributes' => ['class' => ['df-tools-tabs-wrapper']], |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$tabs = [ |
61
|
|
|
'#theme' => 'item_list', |
62
|
|
|
'#items' => [], |
63
|
|
|
'#wrapper_attributes' => ['class' => ['df-tools-tabs-tabs', 'tabs']], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
foreach ($items as $delta => $item) { |
67
|
|
|
$url = Url::fromUri('internal:#' . $this->getFieldId($items, $delta))->toRenderArray(); |
68
|
|
|
$tabs['#items'][] = [ |
69
|
|
|
'#type' => 'link', |
70
|
|
|
'#title' => $item->tab_title, |
71
|
|
|
] + $url; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$build[] = $tabs; |
75
|
|
|
$build[] = parent::view($items, $langcode); |
76
|
|
|
|
77
|
|
|
$build['#attached']['library'][] = 'df_tools_tabs/tabs'; |
78
|
|
|
|
79
|
|
|
return $build; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Forms a string representing a field's unique ID. |
84
|
|
|
* |
85
|
|
|
* @param \Drupal\Core\Field\FieldItemListInterface $items |
86
|
|
|
* The field values to be rendered. |
87
|
|
|
* @param int $delta |
88
|
|
|
* The delta for the current item. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
* A string representing the field's ID |
92
|
|
|
*/ |
93
|
|
|
protected function getFieldId($items, $delta) { |
94
|
|
|
return implode('_', [$items->getEntity()->getEntityTypeId(), $items->getEntity()->id(), $this->fieldDefinition->getName(), $delta]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|