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
File
- core/modules/color/color.module, line 200
- Allows users to change the color scheme of themes.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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 ; } |
Please login to continue.