Skip to content

Commit f17c8b2

Browse files
author
Edouard Cunibil
committed
Create an UiPatternsSource to be able to use field groups in patterns fields.
1 parent cd03369 commit f17c8b2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Drupal\ui_patterns_field_group\Plugin\UiPatterns\Source;
4+
5+
use Drupal\ui_patterns\Plugin\PatternSourceBase;
6+
7+
/**
8+
* Defines Fields API pattern source plugin.
9+
*
10+
* @UiPatternsSource(
11+
* id = "fieldgroup",
12+
* label = @Translation("Fieldgroups"),
13+
* provider = "field_group",
14+
* tags = {
15+
* "entity_display"
16+
* }
17+
* )
18+
*/
19+
class FieldgroupSource extends PatternSourceBase {
20+
21+
/**
22+
* Return list of source fields.
23+
*
24+
* @return \Drupal\ui_patterns\Definition\PatternSourceField[]
25+
* List of source fields.
26+
*
27+
* @throws \Drupal\Component\Plugin\Exception\PluginException
28+
*/
29+
public function getSourceFields() {
30+
$sources = [];
31+
$entity_type_id = $this->getContextProperty('entity_type');
32+
$bundle = $this->getContextProperty('entity_bundle');
33+
$view_mode = $this->getContextProperty('entity_view_mode');
34+
35+
$groups = field_group_info_groups($entity_type_id, $bundle, 'view', $view_mode);
36+
37+
foreach ($groups as $group_name => $group) {
38+
if (empty($this->getContextProperty('limit')) || in_array($group_name, $this->getContextProperty('limit'))) {
39+
$sources[] = $this->getSourceField($group_name, $group->label);
40+
}
41+
}
42+
43+
return $sources;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)