/** * Set the configuration for all plugins. * * @param ConfigInterface $config * A configuration object containing only configuration for all plugins */ public function setConfig(ConfigInterface $config) { // Set the configuration object to the one passed in. $this->config = $config; // Add the init/default values to the config object so they will always exist. // @TODO: Make this cascade happen when the config key is requested. // That will allow read-only or runtime generation config object to be passed // This would work by creating a CascadeConfig object which takes an array // of ConfigInterface objects and queries each in order to find the given key. $defaults = $this->configDefaults(); $init = $this->init; foreach ([$init, $defaults] as $config_object) { foreach ($config_object->toArray() as $key => $value) { if (!$this->config->keyIsSet($key)) { $this->config->set($key, $value); } } } }
/** * Add the schema fields to the given form array. * * @param array $schema * A configuration schema from one or more Backup and Migrate plugins. * @param \BackupMigrate\Core\Config\ConfigInterface $config * The configuration object containing the default values. * @param array $parents * The form parents array. */ public static function addFieldsFromSchema(&$form, $schema, ConfigInterface $config, $parents = []) { // Add the specified groups. foreach ($schema['groups'] as $group_key => $item) { // If the group is just called 'default' then use the key from the plugin as the group key. // @TODO: make this less ugly. if ($group_key == 'default' && $parents) { $group_key = end($parents); } if (!isset($form[$group_key])) { $form[$group_key] = ['#type' => 'fieldset', '#title' => $item['title'], '#tree' => FALSE]; } } // Add each of the fields. foreach ($schema['fields'] as $field_key => $item) { $form_item = []; $value = $config->get($field_key); switch ($item['type']) { case 'text': $form_item['#type'] = 'textfield'; if (!empty($item['multiple'])) { $form_item['#type'] = 'textarea'; $form_item['#description'] .= ' ' . t('Add one item per line.'); $form_item['#element_validate'] = ['BackupMigrate\\Drupal\\Config\\DrupalConfigHelper::validateMultiText']; $value = implode("\n", $value); } if (!empty($item['multiline'])) { $form_item['#type'] = 'textarea'; } break; case 'password': $form_item['#type'] = 'password'; $form_item['#value_callback'] = 'BackupMigrate\\Drupal\\Config\\DrupalConfigHelper::valueCallbackSecret'; break; case 'number': $form_item['#type'] = 'textfield'; $form_item['#size'] = 5; if (!empty($item['max'])) { $form_item['#size'] = strlen((string) $item['max']) + 3; } break; case 'boolean': $form_item['#type'] = 'checkbox'; break; case 'enum': $form_item['#type'] = 'select'; $form_item['#multiple'] = !empty($item['multiple']); if (empty($item['#required']) && empty($item['multiple'])) { $item['options'] = ['' => '--' . t('None') . '--'] + $item['options']; } $form_item['#options'] = $item['options']; break; } // If there is a form item add it to the form. if ($form_item) { // Add the common form elements. $form_item['#title'] = $item['title']; $form_item['#parents'] = array_merge($parents, [$field_key]); $form_item['#required'] = !empty($item['required']); $form_item['#default_value'] = $value; if (!empty($item['description'])) { $form_item['#description'] = $item['description']; } // Add the field to it's group or directly to the top level of the form. if (!empty($item['group'])) { $group_key = $item['group']; if ($group_key == 'default' && $parents) { $group_key = end($parents); } $form[$group_key][$field_key] = $form_item; } else { $form[$field_key] = $form_item; } } } }
/** * @param array $schema * A configuration schema from one or more Backup and Migrate plugins. * @param \BackupMigrate\Core\Config\ConfigInterface $config * @return array */ public static function buildFormFromSchema($schema, ConfigInterface $config) { $form = array(); foreach ($schema as $plugin_key => $plugin_schema) { // Get the configuration for the plugin to use as the form default values. $plugin_config = $config->get($plugin_key); // Add the specified groups. foreach ($plugin_schema['groups'] as $group_key => $item) { if (!isset($form[$group_key])) { $form[$group_key] = ['#type' => 'fieldset', '#title' => $item['title'], '#tree' => FALSE]; } } // Add each of the fields. foreach ($plugin_schema['fields'] as $field_key => $item) { $form_item = array(); $value = $plugin_config->get($field_key); switch ($item['type']) { case 'text': $form_item['#type'] = 'textfield'; if (!empty($item['multiple'])) { $form_item['#type'] = 'textarea'; $form_item['#description'] .= ' ' . t('Add one item per line.'); $form_item['#element_validate'] = [[new DrupalConfigHelper(), 'validateMultiText']]; $value = implode("\n", $plugin_config->get($field_key)); } if (!empty($item['multiline'])) { $form_item['#type'] = 'textarea'; } break; case 'password': $form_item['#type'] = 'password'; break; case 'number': $form_item['#type'] = 'textfield'; $form_item['#size'] = 5; if (!empty($item['max'])) { $form_item['#size'] = strlen((string) $item['max']) + 3; } break; case 'boolean': $form_item['#type'] = 'checkbox'; break; case 'enum': $form_item['#type'] = 'select'; $form_item['#multiple'] = !empty($item['multiple']); if (empty($item['#required']) && empty($item['multiple'])) { $item['options'] = array('' => '--' . t('None') . '--') + $item['options']; } $form_item['#options'] = $item['options']; break; } // If there is a form item add it to the form. if ($form_item) { // Add the common form elements. $form_item['#title'] = $item['title']; $form_item['#parents'] = array($plugin_key, $field_key); $form_item['#required'] = !empty($item['required']); $form_item['#default_value'] = $value; if (!empty($item['description'])) { $form_item['#description'] = $item['description']; } // Add the field to it's group or directly to the top level of the form. if (!empty($item['group'])) { $form[$item['group']][$field_key] = $form_item; } else { $form[$field_key] = $form_item; } } } } return $form; }