Skip to content

Commit 5ee3191

Browse files
yannickooEdouard Cunibil
authored andcommitted
implements entity extra fields source, fixes nuvoleweb#108
1 parent edb9ae0 commit 5ee3191

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
4+
5+
use Drupal\Core\Entity\EntityFieldManager;
6+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7+
use Drupal\ui_patterns\Plugin\PatternSourceBase;
8+
use Drupal\ui_patterns\UiPatternsManager;
9+
use Drupal\Core\TypedData\TypedDataManager;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
12+
/**
13+
* Defines Fields API pattern source plugin.
14+
*
15+
* @UiPatternsSource(
16+
* id = "extra_fields",
17+
* label = @Translation("Extra fields"),
18+
* provider = "field",
19+
* tags = {
20+
* "entity_display"
21+
* }
22+
* )
23+
*/
24+
class ExtraFieldSource extends PatternSourceBase implements ContainerFactoryPluginInterface {
25+
26+
/**
27+
* Entity field manager service.
28+
*
29+
* @var \Drupal\Core\Entity\EntityFieldManager
30+
*/
31+
protected $entityFieldManager;
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $ui_patterns_manager, TypedDataManager $typed_data_manager, EntityFieldManager $entity_field_manager) {
37+
parent::__construct($configuration, $plugin_id, $plugin_definition);
38+
$this->entityFieldManager = $entity_field_manager;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45+
return new static(
46+
$configuration,
47+
$plugin_id,
48+
$plugin_definition,
49+
$container->get('plugin.manager.ui_patterns'),
50+
$container->get('typed_data_manager'),
51+
$container->get('entity_field.manager')
52+
);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function getSourceFields() {
59+
$sources = [];
60+
$entity_type_id = $this->getContextProperty('entity_type');
61+
$bundle = $this->getContextProperty('entity_bundle');
62+
$extra_fields = $this->entityFieldManager->getExtraFields($entity_type_id, $bundle);
63+
64+
if (!isset($extra_fields['display'])) {
65+
return $sources;
66+
}
67+
68+
foreach ($extra_fields['display'] as $extra_field_name => $field) {
69+
if (!$this->getContextProperty('limit')) {
70+
$sources[] = $this->getSourceField($extra_field_name, $field['label']);
71+
}
72+
elseif (in_array($extra_field_name, $this->getContextProperty('limit'))) {
73+
$sources[] = $this->getSourceField($extra_field_name, $field['label']);
74+
}
75+
}
76+
77+
return $sources;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)