You are here

public static function ScssCompilerSystemPerformanceSettingsFormAlter::formAlter in SCSS/Less Compiler 8

Alters existing settings form.

1 call to ScssCompilerSystemPerformanceSettingsFormAlter::formAlter()
scss_compiler_form_system_performance_settings_alter in ./scss_compiler.module
Implements hook_form_FORM_ID_alter().

File

src/Form/ScssCompilerSystemPerformanceSettingsFormAlter.php, line 17

Class

ScssCompilerSystemPerformanceSettingsFormAlter
Adds compiler settings to the performance page.

Namespace

Drupal\scss_compiler\Form

Code

public static function formAlter(&$form, FormStateInterface $form_state) {
  $scss_compiler = \Drupal::service('scss_compiler');
  $form['scss_compiler'] = [
    '#type' => 'details',
    '#title' => t('SCSS Compiler'),
    '#open' => TRUE,
    '#weight' => 0,
  ];
  $form['scss_compiler']['scss_cache'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable compiler cache'),
    '#default_value' => $scss_compiler
      ->isCacheEnabled(),
  ];
  $form['scss_compiler']['advanced'] = [
    '#type' => 'details',
    '#title' => t('Advanced'),
  ];
  $form['scss_compiler']['advanced']['scss_sourcemaps'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable sourcemaps'),
    '#default_value' => $scss_compiler
      ->getOption('sourcemaps'),
  ];
  $form['scss_compiler']['advanced']['scss_check_modify_time'] = [
    '#type' => 'checkbox',
    '#title' => t('Check file modified time'),
    '#description' => t('Compiles only files which was changed based on last modified time. Supports only 1 level import.'),
    '#default_value' => $scss_compiler
      ->getOption('check_modify_time'),
  ];
  $form['scss_compiler']['advanced']['scss_output_format'] = [
    '#type' => 'select',
    '#title' => t('Output format'),
    '#description' => t('Default output format is compressed'),
    '#options' => [
      'expanded' => 'Expanded',
      'nested' => 'Nested',
      'compact' => 'Compact',
      'compressed' => 'Compressed',
      'crunched' => 'Crunched',
    ],
    '#default_value' => $scss_compiler
      ->getOption('output_format'),
  ];
  $form['scss_compiler']['advanced']['flush_cache_type'] = [
    '#type' => 'select',
    '#title' => t('Flush cache behaviour'),
    '#options' => [
      'system' => t('Delete old files on each system flush cache'),
      'default' => t('Delete old files on system flush cache if the compiler cache is disabled'),
      'manual' => t('Delete old files only on manual recompile'),
    ],
    '#default_value' => $scss_compiler
      ->getOption('flush_cache_type'),
  ];
  $form['scss_compiler']['advanced']['actions'] = [
    '#type' => 'actions',
    '#id' => 'scss_compiler_actions',
    'recompile' => [
      '#type' => 'submit',
      '#value' => t('Recompile'),
      '#submit' => [
        'scss_compiler_recompile',
      ],
    ],
  ];
  $compilerPluginManager = \Drupal::service('plugin.manager.scss_compiler');
  $compilers = $compilerPluginManager
    ->getDefinitions();
  $options = [];
  if (!empty($compilers)) {
    foreach ($compilers as $compiler) {
      foreach ($compiler['extensions'] as $key => $extension) {
        $options[$key][$compiler['id']] = $compiler['name'];
      }
    }
  }
  $form['scss_compiler']['advanced']['node_modules_path'] = [
    '#type' => 'textfield',
    '#title' => t('Node modules path'),
    '#description' => t('For example, /usr/local/lib/node_modules'),
    '#default_value' => $scss_compiler
      ->getOption('node_modules_path'),
  ];
  $form['scss_compiler']['advanced']['plugins'] = [
    '#type' => 'fieldset',
    '#title' => t('Default compilers'),
    '#tree' => TRUE,
  ];
  foreach ($options as $key => $option) {
    $plugins = $form_state
      ->getValue('plugins');
    $active = !empty($plugins[$key]) ? $plugins[$key] : NULL;
    $status = NULL;
    if (empty($active)) {
      if (!empty($scss_compiler
        ->getOption('plugins')[$key])) {
        $active = $scss_compiler
          ->getOption('plugins')[$key];
      }
    }
    if (!empty($compilers[$active]['class'])) {
      $status = $compilers[$active]['class']::getStatus();
      if ($status === TRUE) {
        $status = $compilers[$active]['class']::getVersion();
      }
    }
    $form['scss_compiler']['advanced']['plugins'][$key] = [
      '#type' => 'select',
      '#options' => [
        'none' => t('Disabled'),
      ] + $option,
      '#title' => $key,
      '#default_value' => !empty($scss_compiler
        ->getOption('plugins')[$key]) ? $scss_compiler
        ->getOption('plugins')[$key] : 'none',
      '#field_suffix' => !empty($status) ? $status : '',
      '#ajax' => [
        'wrapper' => $form['#id'],
        'callback' => __CLASS__ . '::ajaxCallback',
      ],
    ];
  }
  $form['#submit'][] = __CLASS__ . '::submit';
}