You are here

public function BlazyAdminBase::finalizeForm in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::finalizeForm()
  2. 7 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::finalizeForm()

Returns re-usable logic, styling and assets across fields and Views.

1 call to BlazyAdminBase::finalizeForm()
BlazyAdminBase::closingForm in src/Form/BlazyAdminBase.php
Returns shared ending form elements across field formatter and Views.

File

src/Form/BlazyAdminBase.php, line 577

Class

BlazyAdminBase
A base for blazy admin integration to have re-usable methods in one place.

Namespace

Drupal\blazy\Form

Code

public function finalizeForm(array &$form, $definition = []) {
  $namespace = isset($definition['namespace']) ? $definition['namespace'] : 'slick';
  $settings = isset($definition['settings']) ? $definition['settings'] : [];
  $vanilla = !empty($definition['vanilla']) ? ' form--vanilla' : '';
  $captions = empty($definition['captions']) ? 0 : count($definition['captions']);
  $wide = $captions > 2 ? ' form--wide form--caption-' . $captions : ' form--caption-' . $captions;
  $fallback = $namespace == 'slick' ? 'form--slick' : 'form--' . $namespace . ' form--slick';
  $classes = isset($definition['form_opening_classes']) ? $definition['form_opening_classes'] : $fallback . ' form--half has-tooltip' . $wide . $vanilla;
  if (!empty($definition['field_type'])) {
    $classes .= ' form--' . str_replace('_', '-', $definition['field_type']);
  }
  $form['opening'] = [
    '#markup' => '<div class="' . $classes . '">',
    '#weight' => -120,
  ];
  $form['closing'] = [
    '#markup' => '</div>',
    '#weight' => 120,
  ];
  $admin_css = isset($definition['admin_css']) ? $definition['admin_css'] : '';
  $admin_css = $admin_css ?: $this->blazyManager
    ->configLoad('admin_css', 'blazy.settings');

  // @todo: Check if needed: 'button', 'container', 'submit'.
  $excludes = [
    'details',
    'fieldset',
    'hidden',
    'markup',
    'item',
    'table',
  ];
  $selects = [
    'cache',
    'optionset',
    'view_mode',
  ];
  foreach (Element::children($form) as $key) {
    if (isset($form[$key]['#type']) && !in_array($form[$key]['#type'], $excludes)) {
      if (!isset($form[$key]['#default_value']) && isset($settings[$key])) {
        $value = is_array($settings[$key]) ? array_values((array) $settings[$key]) : $settings[$key];
        $form[$key]['#default_value'] = $value;
      }
      if (!isset($form[$key]['#attributes']) && isset($form[$key]['#description'])) {
        $form[$key]['#attributes'] = [
          'class' => [
            'is-tooltip',
          ],
        ];
      }
      if ($admin_css) {
        if ($form[$key]['#type'] == 'checkbox' && $form[$key]['#type'] != 'checkboxes') {
          $form[$key]['#field_suffix'] = '&nbsp;';
          $form[$key]['#title_display'] = 'before';
        }
        elseif ($form[$key]['#type'] == 'checkboxes' && !empty($form[$key]['#options'])) {
          $form[$key]['#attributes']['class'][] = 'form-wrapper--checkboxes';
          $form[$key]['#attributes']['class'][] = 'form-wrapper--' . str_replace('_', '-', $key);
          $count = count($form[$key]['#options']);
          $form[$key]['#attributes']['class'][] = 'form-wrapper--count-' . ($count > 3 ? 'max' : $count);
          foreach ($form[$key]['#options'] as $i => $option) {
            $form[$key][$i]['#field_suffix'] = '&nbsp;';
            $form[$key][$i]['#title_display'] = 'before';
          }
        }
      }
      if ($form[$key]['#type'] == 'select' && !in_array($key, $selects)) {
        if (!isset($form[$key]['#empty_option']) && !isset($form[$key]['#required'])) {
          $form[$key]['#empty_option'] = $this
            ->t('- None -');
        }
      }
      if (!isset($form[$key]['#enforced']) && !empty($definition['vanilla']) && isset($form[$key]['#type'])) {
        $states['visible'][':input[name*="[vanilla]"]'] = [
          'checked' => FALSE,
        ];
        if (isset($form[$key]['#states'])) {
          $form[$key]['#states']['visible'][':input[name*="[vanilla]"]'] = [
            'checked' => FALSE,
          ];
        }
        else {
          $form[$key]['#states'] = $states;
        }
      }
    }
    $form[$key]['#wrapper_attributes']['class'][] = 'form-item--' . str_replace('_', '-', $key);
    if (isset($form[$key]['#access']) && $form[$key]['#access'] == FALSE) {
      unset($form[$key]['#default_value']);
    }
  }
  if ($admin_css) {
    $form['closing']['#attached']['library'][] = 'blazy/admin';
  }
  $this->blazyManager
    ->getModuleHandler()
    ->alter('blazy_complete_form_element', $form, $definition);
}