FieldSource.php in UI Patterns 8
File
src/Plugin/UiPatterns/Source/FieldSource.php
View source
<?php
namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ui_patterns\Plugin\PatternSourceBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldSource extends PatternSourceBase implements ContainerFactoryPluginInterface {
protected $entityFieldManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'));
}
public function getSourceFields() {
$sources = [];
$fields = $this->entityFieldManager
->getFieldDefinitions($this
->getContextProperty('entity_type'), $this
->getContextProperty('entity_bundle'));
foreach ($fields as $field) {
if (!$this
->getContextProperty('limit')) {
$sources[] = $this
->getSourceField($field
->getName(), $field
->getLabel());
}
elseif (in_array($field
->getName(), $this
->getContextProperty('limit'))) {
$sources[] = $this
->getSourceField($field
->getName(), $field
->getLabel());
}
}
return $sources;
}
}