You are here

public function BlazyAdminBase::finalizeForm in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Form/BlazyAdminBase.php \Drupal\blazy\Form\BlazyAdminBase::finalizeForm()
  2. 8 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 541

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' : '';
  $grid = !empty($definition['grid_required']) ? ' form--grid-required' : '';
  $plugind_id = !empty($definition['plugin_id']) ? ' form--plugin-' . str_replace('_', '-', $definition['plugin_id']) : '';
  $count = empty($definition['captions']) ? 0 : count($definition['captions']);
  $count = empty($definition['captions_count']) ? $count : $definition['captions_count'];
  $wide = $count > 2 ? ' form--wide form--caption-' . $count : ' form--caption-' . $count;
  $fallback = $namespace == 'slick' ? 'form--slick' : 'form--' . $namespace . ' form--slick';
  $custom = isset($definition['opening_class']) ? ' ' . $definition['opening_class'] : '';
  $plugins = ' form--namespace-' . $namespace;
  $classes = $fallback . ' form--half has-tooltip' . $wide . $vanilla . $grid . $plugind_id . $custom . $plugins;
  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 = $this
    ->manager()
    ->config('admin_css', TRUE, 'blazy.settings');
  $excludes = [
    'details',
    'fieldset',
    'hidden',
    'markup',
    'item',
    'table',
  ];
  $selects = [
    'cache',
    'optionset',
    'view_mode',
  ];
  drupal_alter('blazy_form_element', $form, $definition);
  foreach (element_children($form) as $key) {

    // Works around for non-existent #wrapper_attributes at D7.
    $form[$key]['#attributes']['data-blazy-form-item'] = str_replace('_', '-', $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];
        if (!empty($definition['grid_required']) && $key == 'grid' && empty($settings[$key])) {
          $value = 3;
        }
        $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']) && empty($form[$key]['#required'])) {
          $form[$key]['#empty_option'] = t('- None -');
        }
        if (!empty($form[$key]['#required'])) {
          unset($form[$key]['#empty_option']);
        }
      }
      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;
        }
      }
    }
    if (isset($form[$key]['#access']) && $form[$key]['#access'] == FALSE) {
      unset($form[$key]['#default_value']);
    }
  }
  if ($admin_css) {
    $form['closing']['#attached']['library'][] = [
      'blazy',
      'admin',
    ];
  }
  drupal_alter('blazy_complete_form_element', $form, $definition);
}