View source
<?php
namespace Drupal\ui_patterns_field_group\Plugin\field_group\FieldGroupFormatter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field_group\FieldGroupFormatterBase;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\UiPatternsSourceManager;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\ui_patterns_field_group\Utility\EntityFinder;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PatternFormatter extends FieldGroupFormatterBase implements ContainerFactoryPluginInterface {
use PatternDisplayFormTrait;
protected $moduleHandler = NULL;
protected $patternsManager;
protected $sourceManager;
protected $entityFinder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $patterns_manager, UiPatternsSourceManager $source_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($plugin_id, $plugin_definition, $configuration['group'], $configuration['settings'], $configuration['label']);
$this->configuration = $configuration;
$this->patternsManager = $patterns_manager;
$this->sourceManager = $source_manager;
$this->entityFinder = new EntityFinder();
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.ui_patterns'), $container
->get('plugin.manager.ui_patterns_source'), $container
->get('module_handler'));
}
public function preRender(&$element, $rendering_object) {
$fields = [];
$mapping = $this
->getSetting('pattern_mapping');
foreach ($mapping as $field) {
$fields[$field['destination']][] = $element[$field['source']];
}
$element['#type'] = 'pattern';
$element['#id'] = $this
->getSetting('pattern');
$element['#fields'] = $fields;
$element['#multiple_sources'] = TRUE;
$element['#variant'] = $this
->getSetting('pattern_variant');
$element['#context']['type'] = 'field_group';
$element['#context']['group_name'] = $this->configuration['group']->group_name;
$element['#context']['entity_type'] = $this->configuration['group']->entity_type;
$element['#context']['bundle'] = $this->configuration['group']->bundle;
$element['#context']['view_mode'] = $this->configuration['group']->mode;
$element['#context']['entity'] = $this->entityFinder
->findEntityFromFields($element['#fields']);
}
protected function getFieldGroupName() {
return $this->configuration['group']->group_name;
}
public function settingsForm() {
$form = parent::settingsForm();
unset($form['id']);
unset($form['classes']);
if (isset($this->configuration['group']->children) && !empty($this->configuration['group']->children)) {
$context = [
'entity_type' => $this->configuration['group']->entity_type,
'entity_bundle' => $this->configuration['group']->bundle,
'limit' => $this->configuration['group']->children,
];
$this
->buildPatternDisplayForm($form, 'entity_display', $context, $this->configuration['settings']);
}
else {
$form['message'] = [
'#markup' => $this
->t('<b>Attention:</b> you have to add fields to this field group and save the whole entity display before being able to to access the pattern display configuration.'),
];
}
return $form;
}
public function settingsSummary() {
$label = $this
->t('None');
if (!empty($this
->getSetting('pattern'))) {
$label = $this->patternsManager
->getDefinition($this
->getSetting('pattern'))
->getLabel();
}
return [
$this
->t('Pattern: @pattern', [
'@pattern' => $label,
]),
];
}
public static function defaultContextSettings($context) {
return [
'pattern' => '',
'pattern_mapping' => [],
'pattern_variant' => '',
] + parent::defaultContextSettings($context);
}
}