View source
<?php
namespace Drupal\ui_patterns_settings\Form;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns_settings\UiPatternsSettings;
use Drupal\ui_patterns_settings\UiPatternsSettingsManager;
class SettingsFormBuilder {
private static function buildTokenLink(array &$form) {
$content_entity_types = [];
$entity_type_definations = \Drupal::entityTypeManager()
->getDefinitions();
foreach ($entity_type_definations as $definition) {
if ($definition instanceof ContentEntityType) {
$content_entity_types[] = $definition
->id();
}
}
$form['token_link'] = [
'#prefix' => '<div id="ui-patterns-settings-token-link">',
'#suffix' => '</div>',
'#theme' => 'token_tree_link',
'#token_types' => $content_entity_types,
'#show_restricted' => TRUE,
'#weight' => 90,
];
}
public static function layoutForm(array &$form, PatternDefinition $definition, array $configuration) {
$settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
self::buildTokenLink($form);
$form['#attached']['library'][] = 'ui_patterns_settings/widget';
if (UiPatternsSettingsManager::allowVariantToken($definition)) {
$variant_token_value = isset($configuration['pattern']['variant_token']) ? $configuration['pattern']['variant_token'] : NULL;
$form['variant_token'] = [
'#type' => 'textfield',
'#title' => 'Variant token',
'#attributes' => [
'class' => [
'js-ui-patterns-settings-show-token-link',
],
],
'#default_value' => $variant_token_value,
];
}
$form['variant']['#attributes']['class'][] = 'ui-patterns-variant-selector-' . $definition
->id();
if (!empty($settings)) {
foreach ($settings as $key => $setting) {
if (empty($setting
->getType()) || !$setting
->isFormVisible()) {
continue;
}
if (!isset($form['settings'])) {
$form['settings'] = [
'#type' => 'fieldset',
'#title' => t('Settings'),
];
}
$setting_value = isset($configuration['pattern']['settings'][$key]) ? $configuration['pattern']['settings'][$key] : NULL;
$token_value = isset($configuration['pattern']['settings'][$key . "_token"]) ? $configuration['pattern']['settings'][$key . "_token"] : "";
$settingType = UiPatternsSettings::createSettingType($definition, $setting);
$form['settings'] += $settingType
->buildConfigurationForm([], $setting_value, $token_value, 'layouts_display');
}
SettingsFormBuilder::buildVariantsForm(".ui-patterns-variant-selector-" . $definition
->id(), $form['settings'], $definition);
}
}
public static function displayForm(array &$form, array $configuration) {
$form['#attached']['library'][] = 'ui_patterns_settings/widget';
self::buildTokenLink($form);
foreach (UiPatterns::getPatternDefinitions() as $pattern_id => $definition) {
$settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
$form['variants'][$pattern_id]['#attributes']['class'][] = 'ui-patterns-variant-selector-' . $pattern_id;
if (UiPatternsSettingsManager::allowVariantToken($definition)) {
$variant_token_value = isset($configuration['variants_token'][$pattern_id]) ? $configuration['variants_token'][$pattern_id] : NULL;
$form['variants']['#weight'] = 20;
$form['pattern_mapping']['#weight'] = 30;
$form['pattern_settings']['#weight'] = 40;
$form['variants_token'] = [
'#type' => 'container',
'#title' => t('Pattern Variant'),
'#weight' => 25,
'#states' => [
'visible' => [
'select[id="patterns-select"]' => [
'value' => $pattern_id,
],
],
],
];
$form['variants_token'][$pattern_id] = [
'#type' => 'textfield',
'#title' => t('Variant token'),
'#default_value' => $variant_token_value,
'#attributes' => [
'class' => [
'js-ui-patterns-settings-show-token-link',
],
],
'#states' => [
'visible' => [
'select[id="patterns-select"]' => [
'value' => $pattern_id,
],
],
],
];
}
if (!empty($settings)) {
foreach ($settings as $key => $setting) {
if (empty($setting
->getType()) || !$setting
->isFormVisible()) {
continue;
}
if (!isset($form['pattern_settings'][$pattern_id])) {
$form['pattern_settings'][$pattern_id] = [
'#type' => 'fieldset',
'#title' => t('Settings'),
'#states' => [
'visible' => [
'select[id="patterns-select"]' => [
'value' => $pattern_id,
],
],
],
];
}
$fieldset =& $form['pattern_settings'][$pattern_id];
$settingType = UiPatternsSettings::createSettingType($definition, $setting);
$setting_value = isset($configuration['pattern_settings'][$pattern_id][$key]) ? $configuration['pattern_settings'][$pattern_id][$key] : NULL;
$token_value = isset($configuration['pattern_settings'][$pattern_id][$key . "_token"]) ? $configuration['pattern_settings'][$pattern_id][$key . "_token"] : NULL;
$fieldset += $settingType
->buildConfigurationForm([], $setting_value, $token_value, 'display');
}
SettingsFormBuilder::buildVariantsForm('.ui-patterns-variant-selector-' . $pattern_id, $fieldset, $definition);
}
}
}
private static function buildVariantsForm($select_selector, array &$fieldset, PatternDefinition $definition) {
$variants = $definition
->getVariants();
foreach ($variants as $variant) {
$variant_ary = $variant
->toArray();
$settings = isset($variant_ary['settings']) ? $variant_ary['settings'] : [];
foreach ($settings as $name => $setting) {
if (isset($fieldset[$name])) {
if (isset($fieldset[$name]['#states']['invisible']) && count($fieldset[$name]['#states']['invisible']) != 0) {
$fieldset[$name]['#states']['invisible'][] = 'or';
}
$fieldset[$name]['#states']['invisible'][][$select_selector]['value'] = $variant
->getName();
if (isset($fieldset[$name . '_token'])) {
$fieldset[$name . '_token']['#states']['invisible'][][$select_selector]['value'] = $variant
->getName();
}
}
}
}
}
}