SettingsFormBuilder.php in UI Patterns Settings 8
File
src/Form/SettingsFormBuilder.php
View source
<?php
namespace Drupal\ui_patterns_settings\Form;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns_settings\UiPatternsSettings;
class SettingsFormBuilder {
public static function layoutForm(array &$form, PatternDefinition $definition, array $configuration) {
$settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
$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;
$settingType = UiPatternsSettings::createSettingType($setting);
$form['settings'] += $settingType
->buildConfigurationForm([], $setting_value);
}
SettingsFormBuilder::buildVariantsForm(".ui-patterns-variant-selector-" . $definition
->id(), $form['settings'], $definition);
}
}
public static function displayForm(array &$form, array $configuration) {
foreach (UiPatterns::getPatternDefinitions() as $pattern_id => $definition) {
$settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
$form['variants'][$pattern_id]['#attributes']['class'][] = 'ui-patterns-variant-selector-' . $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($setting);
$setting_value = isset($configuration['pattern_settings'][$pattern_id][$key]) ? $configuration['pattern_settings'][$pattern_id][$key] : "";
$fieldset += $settingType
->buildConfigurationForm([], $setting_value);
}
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();
}
}
}
}
}