You are here

public function GenerateForm::buildForm in AT Tools 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

at_theme_generator/src/Form/GenerateForm.php, line 55
Generator form.

Class

GenerateForm
@file Generator form.

Namespace

Drupal\at_theme_generator\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $base_themes = $this->sourceThemeOptions;
  $at_core = array_key_exists('at_core', $this->themeInfoData) ? TRUE : FALSE;
  $form['#attached']['library'][] = 'at_theme_generator/theme_generator';
  $form['generate'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
  );
  $form['generate']['docs'] = array(
    '#type' => 'container',
    '#markup' => t('<a class="at-docs" href="@docs" target="_blank" title="External link: docs.adaptivethemes.com/theme-generator">View generator documentation <svg class="docs-ext-link-icon" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1408 928v320q0 119-84.5 203.5t-203.5 84.5h-832q-119 0-203.5-84.5t-84.5-203.5v-832q0-119 84.5-203.5t203.5-84.5h704q14 0 23 9t9 23v64q0 14-9 23t-23 9h-704q-66 0-113 47t-47 113v832q0 66 47 113t113 47h832q66 0 113-47t47-113v-320q0-14 9-23t23-9h64q14 0 23 9t9 23zm384-864v512q0 26-19 45t-45 19-45-19l-176-176-652 652q-10 10-23 10t-23-10l-114-114q-10-10-10-23t10-23l652-652-176-176q-19-19-19-45t19-45 45-19h512q26 0 45 19t19 45z"/></svg></a>', array(
      '@docs' => '//docs.adaptivethemes.com/theme-generator/',
    )),
    '#weight' => -1000,
  );
  if ($at_core == FALSE) {
    $form['generate']['#disabled'] = TRUE;
    drupal_set_message(t('<a href="@download_href" target="_blank">Adaptivetheme</a> is a required base theme for all generated themes, <a href="@download_href" target="_blank">download the latest version for Drupal 8</a> and place in the themes directory.', array(
      '@download_href' => 'https://www.drupal.org/project/adaptivetheme',
    )), 'error');
  }

  // Friendly name.
  $form['generate']['generate_friendly_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Theme name'),
    '#maxlength' => 50,
    // the maximum allowable length of a module or theme name.
    '#size' => 30,
    '#required' => TRUE,
    '#default_value' => '',
    '#description' => t('Enter a unique theme name. Letters, spaces and underscores only.'),
  );

  // Machine name.
  $form['generate']['generate_machine_name'] = array(
    '#type' => 'machine_name',
    '#maxlength' => 50,
    '#size' => 30,
    '#title' => t('Machine name'),
    '#required' => TRUE,
    '#field_prefix' => '',
    '#default_value' => '',
    '#machine_name' => array(
      'exists' => array(
        $this->themeSettingsInfo,
        'themeNameExists',
      ),
      // class method for call_user_func()
      'source' => array(
        'generate',
        'generate_friendly_name',
      ),
      'label' => t('Machine name'),
      'replace_pattern' => '[^a-z_]+',
      'replace' => '_',
    ),
  );
  $generate_type_options = array(
    'standard' => t('Standard kit'),
  );
  if (!empty($base_themes)) {
    $generate_type_options = array(
      'standard' => t('Standard kit'),
      'clone' => t('Clone'),
    );
  }
  $form['generate']['generate_type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#required' => TRUE,
    '#options' => $generate_type_options,
  );
  $form['generate']['generate_type_description_standard_kit'] = array(
    '#type' => 'container',
    '#markup' => t('Standard kit includes an advanced layout and is designed to fully support the UIKit and Color module (both optional).'),
    '#attributes' => array(
      'class' => array(
        'generate-type__description',
      ),
    ),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          'value' => 'standard',
        ),
      ),
    ),
  );
  $form['generate']['generate_clone_source'] = array(
    '#type' => 'select',
    '#title' => t('Clone source'),
    '#options' => $base_themes,
    '#default_value' => '',
    '#description' => t('Clones are direct copies of existing sub-themes. Use a unique name.'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          'value' => 'clone',
        ),
      ),
    ),
  );

  // Options
  $form['generate']['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          array(
            'value' => 'standard',
          ),
          array(
            'value' => 'clone',
          ),
        ),
      ),
    ),
  );

  // UI Kit
  $form['generate']['options']['generate_uikit'] = array(
    '#type' => 'checkbox',
    '#title' => t('UI Kit'),
    '#default_value' => 0,
    '#description' => t('Include the User Interface Kit - a SASS/Compass UI Kit for Adaptivetheme and Drupal.'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          array(
            'value' => 'standard',
          ),
        ),
      ),
    ),
  );

  // Color module
  $form['generate']['options']['generate_color'] = array(
    '#type' => 'checkbox',
    '#title' => t('Color Module'),
    '#default_value' => 0,
    '#description' => t('Provides Color module support - includes a starter color.inc file. Requires UI Kit.'),
    '#states' => array(
      'disabled' => array(
        'input[name="generate[options][generate_uikit]"]' => array(
          'checked' => FALSE,
        ),
      ),
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          array(
            'value' => 'standard',
          ),
        ),
      ),
    ),
  );

  // Templates
  $form['generate']['options']['generate_templates'] = array(
    '#type' => 'checkbox',
    '#title' => t('Templates'),
    '#default_value' => 0,
    '#description' => t('Include copies of Drupals front end twig templates (page.html.twig is always included regardless of this setting).'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          array(
            'value' => 'standard',
          ),
        ),
      ),
    ),
  );

  // Block config
  $form['generate']['options']['generate_block_config'] = array(
    '#type' => 'checkbox',
    '#title' => t('Block Config'),
    '#default_value' => 1,
    '#description' => t('Include configuration for blocks. Un-check this setting if you want your theme to inherit the default themes block configuration.'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          [
            'value' => 'standard',
          ],
        ),
      ),
    ),
  );

  // theme-settings.php file
  $form['generate']['options']['generate_themesettingsfile'] = array(
    '#type' => 'checkbox',
    '#title' => t('theme-settings.php'),
    '#default_value' => 0,
    '#description' => t('Include a theme-settings.php file. Includes skeleton code for the form alter, custom validation and submit functions.'),
    '#states' => array(
      'visible' => array(
        'select[name="generate[generate_type]"]' => array(
          array(
            'value' => 'standard',
          ),
        ),
      ),
    ),
  );

  // Description.
  $form['generate']['options']['generate_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#default_value' => '',
    '#description' => t('Descriptions are used on the Appearance list page.'),
  );

  // Version.
  $form['generate']['options']['generate_version'] = array(
    '#type' => 'textfield',
    '#title' => t('Version string'),
    '#default_value' => '',
    '#description' => t('Numbers, hyphens and periods only. E.g. 8.x-1.0'),
  );
  $form['generate']['actions']['#type'] = 'actions';
  $form['generate']['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#button_type' => 'primary',
  );
  return $form;
}