You are here

public function ElevateZoomPlusForm::edit_form in ElevateZoom Plus 7

Provide the actual editing form.

Overrides ctools_export_ui::edit_form

File

modules/ui/src/Form/ElevateZoomPlusForm.php, line 36

Class

ElevateZoomPlusForm
Extends base form for elevatezoomplus instance configuration form.

Namespace

Drupal\elevatezoomplus_ui\Form

Code

public function edit_form(&$form, &$form_state) {
  parent::edit_form($form, $form_state);
  $optionset = $form_state['item'];
  $admin_css = $this->manager
    ->config('admin_css', 'blazy.settings');
  $form['#attributes']['class'][] = 'form--elevatezoomplus form--blazy form--slick form--optionset has-tooltip';
  if ($admin_css) {
    $form['#attached']['library'][] = [
      'slick_ui',
      'ui',
    ];
    $form['#attached']['library'][] = [
      'elevatezoomplus',
      'admin',
    ];
  }
  $form['info']['label']['#attributes']['class'][] = 'is-tooltip';
  $form['info']['name']['#attributes']['class'][] = 'is-tooltip';

  // Options.
  $form['options'] = [
    '#type' => 'vertical_tabs',
    '#tree' => TRUE,
    '#parents' => [
      'options',
    ],
  ];

  // Main JS options.
  $form['settings'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('Settings'),
    '#attributes' => [
      'class' => [
        'fieldset--settings',
        'has-tooltip',
      ],
    ],
    '#group' => 'options',
    '#parents' => [
      'options',
      'settings',
    ],
  ];
  $settings = $optionset
    ->getSettings();
  $form['settings'] += $this
    ->attachSettingsForm($settings);

  // Responsive JS options.
  $responds = $optionset
    ->getSetting('respond') ?: [];
  $form['respond'] = [
    '#type' => 'fieldset',
    '#title' => t('Responsive display'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
    '#attributes' => [
      'class' => [
        'details--respond',
        'has-tooltip',
      ],
    ],
    '#group' => 'options',
    '#parents' => [
      'options',
      'settings',
      'respond',
    ],
    '#description' => t('Containing breakpoints and settings objects.'),
  ];
  $form['respond']['settings'] = [
    '#type' => 'container',
    '#title' => t('Responsive settings'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
    '#attributes' => [
      'class' => [
        'form-wrapper--table',
        'form-wrapper--table-ajax',
      ],
    ],
    '#group' => 'respond',
    '#parents' => [
      'options',
      'settings',
      'respond',
    ],
    '#description' => t('Containing breakpoints and settings objects.'),
    '#prefix' => '<div id="edit-respond-settings-wrapper">',
    '#suffix' => '</div>',
  ];
  $num_responds = isset($form_state['num_responds']) ? $form_state['num_responds'] : count($responds);
  if (empty($num_responds)) {
    $num_responds = 1;
  }
  $form_state['num_responds'] = $num_responds;
  $excludes = [
    'cursor',
    'loadingIcon',
    'tint',
  ];

  // Individual breakpoint fieldset depends on the breakpoints amount.
  if ($form_state['num_responds'] > 0) {
    for ($i = 0; $i <= $form_state['num_responds']; $i++) {
      $form['respond']['settings'][$i] = [
        '#type' => 'fieldset',
        '#title' => t('Breakpoint #@num', [
          '@num' => $i,
        ]),
        '#tree' => TRUE,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#group' => 'settings',
        '#parents' => [
          'options',
          'settings',
          'respond',
          $i,
        ],
        '#attributes' => [
          'class' => [
            'fieldset--responsive',
            'fieldset--responsive--ajax',
            'fieldset--breakpoint-' . $i,
          ],
        ],
      ];
      $form['respond']['settings'][$i]['range'] = [
        '#type' => 'textfield',
        '#title' => t('Range'),
        '#default_value' => isset($responds[$i]['range']) ? $responds[$i]['range'] : '',
        '#size' => 40,
        '#max_length' => 120,
        '#description' => t('The window range to activate the responsive settings, e.g.: 600-799.'),
      ];
      $form['respond']['settings'][$i]['enabled'] = [
        '#type' => 'checkbox',
        '#title' => t('Enabled'),
        '#default_value' => isset($responds[$i]['enabled']) ? $responds[$i]['enabled'] : TRUE,
        '#size' => 40,
        '#max_length' => 120,
        '#description' => t('Uncheck to disable the zoom at this range.'),
      ];
      if ($admin_css) {
        $form['respond']['settings'][$i]['enabled']['#field_suffix'] = '&nbsp;';
        $form['respond']['settings'][$i]['enabled']['#title_display'] = 'before';
      }
      $form['respond']['settings'][$i]['remove_respond'] = [
        '#type' => 'submit',
        '#value' => t('x'),
        '#name' => 'remove_respond_' . $i,
        '#submit' => [
          '_elevatezoomplus_ui_remove_respond',
        ],
        '#ajax' => [
          'callback' => '_elevatezoomplus_ui_remove_respond_callback',
          'wrapper' => 'edit-respond-settings-wrapper',
        ],
        '#prefix' => '<div class="form-item form-item--button-remove">',
        '#suffix' => '</div>',
        '#limit_validation_errors' => [],
      ];
      $form['respond']['settings'][$i]['settings'] = [
        '#type' => 'container',
        '#open' => FALSE,
        '#title' => t('Settings'),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'form-wrapper--responsive',
            'fieldset--settings',
          ],
        ],
        '#group' => 'settings',
        '#parents' => [
          'options',
          'settings',
          'respond',
          $i,
        ],
      ];
      $settings = isset($responds[$i]) ? $responds[$i] : [];
      $form['respond']['settings'][$i]['settings'] += $this
        ->attachSettingsForm($settings, $excludes);
    }
  }
  $form['respond']['actions'] = [
    '#type' => 'actions',
  ];
  $form['respond']['actions']['add_respond'] = [
    '#type' => 'submit',
    '#value' => t('Add another respond'),
    '#name' => 'add_respond',
    '#submit' => [
      'elevatezoomplus_ui_add_respond',
    ],
    '#ajax' => [
      'callback' => 'elevatezoomplus_ui_add_respond_callback',
      'wrapper' => 'edit-respond-settings-wrapper',
    ],
    '#limit_validation_errors' => [],
  ];
}