color_scheme_form

color_scheme_form($complete_form, FormStateInterface $form_state, $theme)

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()

File

core/modules/color/color.module, line 200
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'][''] = array('title' => t('Custom'), 'colors' => array());
  $color_sets = array();
  $schemes = array();
  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'] = array(
    '#type' => 'select',
    '#title' => t('Color set'),
    '#options' => $color_sets,
    '#default_value' => $scheme_name,
    '#attached' => array(
      'library' => array(
        '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] = array(
        '#type' => 'textfield',
        '#title' => $names[$name],
        '#value_callback' => 'color_palette_color_value',
        '#default_value' => $value,
        '#size' => 8,
        '#attributes' => array('dir' => LanguageInterface::DIRECTION_LTR),
      );
    }
  }
  $form['theme'] = array('#type' => 'value', '#value' => $theme);
  if (isset($info['#attached'])) {
    $form['#attached'] = $info['#attached'];
    unset($info['#attached']);
  }
  $form['info'] = array('#type' => 'value', '#value' => $info);

  return $form;
}
doc_Drupal
2016-10-29 08:50:08
Comments
Leave a Comment

Please login to continue.