You are here

protected function ColorsSettingsForm::getDefaultFormElements in Colors 8

Get form element for default settings.

Parameters

array $form:

1 call to ColorsSettingsForm::getDefaultFormElements()
ColorsSettingsForm::buildForm in src/Form/ColorsSettingsForm.php
Form constructor.

File

src/Form/ColorsSettingsForm.php, line 147

Class

ColorsSettingsForm
Configure color settings.

Namespace

Drupal\colors\Form

Code

protected function getDefaultFormElements(&$form) {
  $config = colors_get_info();
  $palette = colors_get_palette($config);
  $names = $config
    ->get('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(
          'class' => array(
            'colorpicker-input',
          ),
        ),
      );
    }
  }
  $form['process_order'] = [
    '#tree' => TRUE,
    'info' => [
      '#type' => 'item',
      '#title' => t('Process order'),
    ],
    'enabled' => [
      '#type' => 'checkbox',
      '#title' => t('Change the CSS processing order.'),
      '#default_value' => \Drupal::configFactory()
        ->getEditable('colors.settings')
        ->get('override'),
      '#description' => t('Color order is cascading, CSS from modules at the bottom will override the top.'),
    ],
  ];
  $form['order_settings'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="process_order[enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['order_settings']['entities'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('No entities available.'),
    '#attributes' => [
      'id' => 'colors-entities',
    ],
  ];
  $configs = \Drupal::configFactory()
    ->getEditable('colors.settings')
    ->get('order');
  $configs = is_array($configs) ? $configs : [];
  foreach ($configs as $config_id => $weight) {
    $plugin = \Drupal::service('plugin.manager.colors')
      ->getDefinition($config_id);
    $form['order_settings']['entities'][$config_id]['entity'] = [
      '#plain_text' => $plugin['title'],
    ];
    $form['order_settings']['entities'][$config_id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for added entity'),
      '#title_display' => 'invisible',
      '#default_value' => $weight,
      '#attributes' => [
        'class' => [
          'color-weight',
        ],
      ],
    ];
    $form['order_settings']['entities'][$config_id]['#attributes']['class'][] = 'draggable';
    $form['order_settings']['entities']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'color-weight',
    ];
  }
}