Skip to content

Commit

Permalink
Create an UiPatternsSource to be able to use field groups in patterns…
Browse files Browse the repository at this point in the history
… fields.
  • Loading branch information
Edouard Cunibil committed Oct 29, 2020
1 parent cd03369 commit f17c8b2
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\ui_patterns_field_group\Plugin\UiPatterns\Source;

use Drupal\ui_patterns\Plugin\PatternSourceBase;

/**
* Defines Fields API pattern source plugin.
*
* @UiPatternsSource(
* id = "fieldgroup",
* label = @Translation("Fieldgroups"),
* provider = "field_group",
* tags = {
* "entity_display"
* }
* )
*/
class FieldgroupSource extends PatternSourceBase {

/**
* Return list of source fields.
*
* @return \Drupal\ui_patterns\Definition\PatternSourceField[]
* List of source fields.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function getSourceFields() {
$sources = [];
$entity_type_id = $this->getContextProperty('entity_type');
$bundle = $this->getContextProperty('entity_bundle');
$view_mode = $this->getContextProperty('entity_view_mode');

$groups = field_group_info_groups($entity_type_id, $bundle, 'view', $view_mode);

foreach ($groups as $group_name => $group) {
if (empty($this->getContextProperty('limit')) || in_array($group_name, $this->getContextProperty('limit'))) {
$sources[] = $this->getSourceField($group_name, $group->label);
}
}

return $sources;
}

}

0 comments on commit f17c8b2

Please sign in to comment.