View source
<?php
namespace Drupal\ui_patterns_ds\Plugin\DsFieldTemplate;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ds\Plugin\DsFieldTemplate\DsFieldTemplateBase;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\UiPatternsSourceManager;
use Drupal\ui_patterns\UiPatternsManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Entity\EntityFieldManager;
class Pattern extends DsFieldTemplateBase implements ContainerFactoryPluginInterface {
use PatternDisplayFormTrait;
protected $moduleHandler = NULL;
protected $patternsManager;
protected $sourceManager;
protected $parameters;
protected $fieldManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $patterns_manager, UiPatternsSourceManager $source_manager, RequestStack $parameters, EntityFieldManager $field_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->patternsManager = $patterns_manager;
$this->sourceManager = $source_manager;
$this->parameters = $parameters
->getCurrentRequest()->request;
$this->fieldManager = $field_manager;
$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('request_stack'), $container
->get('entity_field.manager'), $container
->get('module_handler'));
}
public function alterForm(&$form) {
$context = $this
->getContext();
if ($this
->isSupportedField($context)) {
$this
->buildPatternDisplayForm($form, 'ds_field_template', $context, $this
->getConfiguration());
}
else {
$form['#markup'] = $this
->t("The current field is not supported.");
}
}
protected function getContext() {
$fields = $this->parameters
->get('fields');
$field_name = $this
->getCurrentField();
return [
'field_name' => $field_name,
'field_settings' => $fields[$field_name],
'entity_type' => $this->parameters
->get('ds_entity_type'),
'bundle' => $this->parameters
->get('ds_bundle'),
'view_mode' => $this->parameters
->get('ds_view_mode'),
];
}
public function defaultConfiguration() {
return [
'pattern' => '',
'pattern_variant' => '',
'pattern_mapping' => [],
];
}
protected function getCurrentField() {
$fields = array_filter($this->parameters
->get('fields', []), function ($field) {
return isset($field['settings_edit_form']['third_party_settings']['ds']['ft']['id']) && $field['settings_edit_form']['third_party_settings']['ds']['ft']['id'] == 'pattern';
});
$fields = array_keys($fields);
$field = reset($fields);
if (empty($field)) {
$trigger_element = $this->parameters
->get('_triggering_element_name');
$field = str_replace('_plugin_settings_edit', '', $trigger_element);
}
return $field;
}
protected function isSupportedField(array $context) {
if ($context['entity_type'] && $context['bundle']) {
$field = $this->fieldManager
->getFieldDefinitions($context['entity_type'], $context['bundle']);
return isset($field[$context['field_name']]);
}
return FALSE;
}
}