You are here

function color_scheme_form in Drupal 8

Same name and namespace in other branches
  1. 5 modules/color/color.module \color_scheme_form()
  2. 6 modules/color/color.module \color_scheme_form()
  3. 7 modules/color/color.module \color_scheme_form()
  4. 9 core/modules/color/color.module \color_scheme_form()

Form constructor for the color configuration form for a particular theme.

Parameters

$theme: The machine name of the theme whose color settings are being configured.

See also

color_scheme_form_validate()

color_scheme_form_submit()

1 call to color_scheme_form()
color_form_system_theme_settings_alter in core/modules/color/color.module
Implements hook_form_FORM_ID_alter().
1 string reference to 'color_scheme_form'
color_form_system_theme_settings_alter in core/modules/color/color.module
Implements hook_form_FORM_ID_alter().

File

core/modules/color/color.module, line 199
Allows users to change the color scheme of themes.

Code

function color_scheme_form($complete_form, FormStateInterface $form_state, $theme) {
  $info = color_get_info($theme);
  $info['schemes'][''] = [
    'title' => t('Custom'),
    'colors' => [],
  ];
  $color_sets = [];
  $schemes = [];
  foreach ($info['schemes'] as $key => $scheme) {
    $color_sets[$key] = $scheme['title'];
    $schemes[$key] = $scheme['colors'];
    $schemes[$key] += $info['schemes']['default']['colors'];
  }

  // See if we're using a predefined scheme.
  // Note: we use the original theme when the default scheme is chosen.
  // Note: we use configuration without overrides since this information is used
  // in a form and therefore without doing this would bleed overrides into
  // active configuration. Furthermore, color configuration is used to write
  // CSS to the file system making configuration overrides pointless.
  $current_scheme = \Drupal::configFactory()
    ->getEditable('color.theme.' . $theme)
    ->get('palette');
  foreach ($schemes as $key => $scheme) {
    if ($current_scheme == $scheme) {
      $scheme_name = $key;
      break;
    }
  }
  if (empty($scheme_name)) {
    if (empty($current_scheme)) {
      $scheme_name = 'default';
    }
    else {
      $scheme_name = '';
    }
  }

  // Add scheme selector.
  $default_palette = color_get_palette($theme, TRUE);
  $form['scheme'] = [
    '#type' => 'select',
    '#title' => t('Color set'),
    '#options' => $color_sets,
    '#default_value' => $scheme_name,
    '#attached' => [
      'library' => [
        'color/drupal.color',
        'color/admin',
      ],
      // Add custom JavaScript.
      'drupalSettings' => [
        'color' => [
          'reference' => $default_palette,
          'schemes' => $schemes,
        ],
        'gradients' => $info['gradients'],
      ],
    ],
  ];

  // Add palette fields. Use the configuration if available.
  $palette = $current_scheme ?: $default_palette;
  $names = $info['fields'];
  $form['palette']['#tree'] = TRUE;
  foreach ($palette as $name => $value) {
    if (isset($names[$name])) {
      $form['palette'][$name] = [
        '#type' => 'textfield',
        '#title' => $names[$name],
        '#value_callback' => 'color_palette_color_value',
        '#default_value' => $value,
        '#size' => 8,
        '#attributes' => [
          'dir' => LanguageInterface::DIRECTION_LTR,
        ],
      ];
    }
  }
  $form['theme'] = [
    '#type' => 'value',
    '#value' => $theme,
  ];
  if (isset($info['#attached'])) {
    $form['#attached'] = $info['#attached'];
    unset($info['#attached']);
  }
  $form['info'] = [
    '#type' => 'value',
    '#value' => $info,
  ];
  return $form;
}