You are here

public static function DrupalConfigHelper::addFieldsFromSchema in Backup and Migrate 8.4

Add the schema fields to the given form array.

Parameters

array $schema: A configuration schema from one or more Backup and Migrate plugins.

\BackupMigrate\Core\Config\ConfigInterface $config: The configuration object containing the default values.

array $parents: The form parents array.

2 calls to DrupalConfigHelper::addFieldsFromSchema()
DrupalConfigHelper::buildAllPluginsForm in src/Config/DrupalConfigHelper.php
Build the configuration form for all plugins in a manager.
DrupalConfigHelper::buildFormFromSchema in src/Config/DrupalConfigHelper.php

File

src/Config/DrupalConfigHelper.php, line 86

Class

DrupalConfigHelper
Class DrupalConfigHelper.

Namespace

BackupMigrate\Drupal\Config

Code

public static function addFieldsFromSchema(&$form, $schema, ConfigInterface $config, $parents = []) {

  // Add the specified groups.
  if (isset($schema['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.
  if (isset($schema['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';
            if (!isset($form_item['#description'])) {
              $form_item['#description'] = '';
            }
            $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;
        }
      }
    }
  }
}