You are here

protected function ColorsSettingsForm::getFormElements in Colors 8

Get settings form elements.

Parameters

array $form:

string $entity:

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

File

src/Form/ColorsSettingsForm.php, line 234

Class

ColorsSettingsForm
Configure color settings.

Namespace

Drupal\colors\Form

Code

protected function getFormElements(&$form, $entity) {
  $plugin = \Drupal::service('plugin.manager.colors')
    ->getDefinition($entity);
  if (!$plugin['id']) {
    return;
  }
  $form[$plugin['id']] = [
    '#type' => 'item',
    '#title' => t('@title colors', [
      '@title' => $plugin['title'],
    ]),
    '#description' => $plugin['description'],
  ];
  $multiple = !empty($plugin['multiple']);
  $repeat = !empty($multiple) ? $plugin['multiple']() : [
    NULL => NULL,
  ];
  $enabled = colors_get_enabled($entity) ? TRUE : FALSE;
  foreach ($repeat as $id => $repeat_value) {
    $config_name = "colors-{$entity}";
    $config_name .= !empty($multiple) ? "-{$id}" : '';
    $element = [
      '#type' => 'details',
      '#title' => !empty($multiple) ? $repeat_value
        ->label() : t('@title colors', array(
        '@title' => $plugin['title'],
      )),
      '#collapsible' => TRUE,
      '#open' => $enabled,
      '#attributes' => [
        'class' => [
          'overflow-hidden',
        ],
      ],
      '#prefix' => '<div class="clearfix">',
      '#suffix' => '</div>',
    ];
    $element[$config_name] = [
      '#type' => 'checkbox',
      '#title' => $plugin['label'],
      '#default_value' => $enabled,
    ];
    $element['plugin'] = [
      '#type' => 'value',
      '#value' => $plugin,
    ];
    $element['id'] = [
      '#type' => 'value',
      '#value' => $config_name,
    ];

    //      @todo: don't display vocab fieldset if there's no terms yet.
    foreach ($plugin['callback']($id) as $key => $label) {

      //$config_id = "colors.$entity.$key";
      $config_id = str_replace('-', '.', $config_name) . ".{$key}";
      $element['palette'][$config_id] = [
        '#type' => 'details',
        '#title' => t($label),
        '#collapsible' => TRUE,
        '#open' => TRUE,
        '#attributes' => [
          'class' => [
            'views-left-25',
          ],
        ],
        '#states' => [
          'visible' => [
            ':input[name="' . $config_name . '"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];

      // Get configs.
      $default_config = colors_get_info();
      $config = colors_get_info($config_id);
      $config = $config
        ->isNew() ? $default_config : $config;

      // Get palette.
      $palette = colors_get_palette($config);
      $names = $default_config
        ->get('fields');
      $element['palette']['#tree'] = TRUE;
      foreach ($palette as $name => $value) {
        if (isset($names[$name])) {
          $element['palette'][$config_id][$name] = [
            '#type' => 'textfield',
            '#title' => $names[$name],
            '#value_callback' => 'color_palette_color_value',
            '#default_value' => $value,
            '#maxlength' => 7,
            '#size' => 7,
            '#attributes' => [
              'class' => [
                'colorpicker-input',
              ],
            ],
            '#states' => [
              'visible' => [
                ':input[name="' . $config_name . '"]' => [
                  'checked' => TRUE,
                ],
              ],
            ],
          ];
        }
      }
    }
    $element_key = !empty($multiple) ? $id : 'details';
    $form[$element_key] = $element;
  }
}