You are here

public function ElevateZoomPlusForm::form in ElevateZoom Plus 8

Gets the actual form array to be built.

Overrides BlazyEntityFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

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

Class

ElevateZoomPlusForm
Extends base form for elevatezoomplus instance configuration form.

Namespace

Drupal\elevatezoomplus_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $optionset = $this->entity;
  $tooltip = [
    'class' => [
      'is-tooltip',
    ],
  ];
  $admin_css = $this->manager
    ->configLoad('admin_css', 'blazy.settings');
  $form['#attributes']['class'][] = 'form--elevatezoomplus';
  if ($admin_css) {
    $form['#attached']['library'][] = 'elevatezoomplus/admin';

    // Attach Slick admin library.
    if ($this->manager
      ->getModuleHandler()
      ->moduleExists('splide_ui')) {
      $form['#attributes']['class'][] = 'form--splide';
      $form['#attached']['library'][] = 'splide_ui/admin.vtabs';
    }
    elseif ($this->manager
      ->getModuleHandler()
      ->moduleExists('slick_ui')) {
      $form['#attached']['library'][] = 'slick_ui/slick.admin.vtabs';
    }
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $optionset
      ->label(),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => $this
      ->t("Label for the ElevateZoomPlus optionset."),
    '#attributes' => $tooltip,
  ];

  // Keep the legacy CTools ID, i.e.: name as ID.
  $form['name'] = [
    '#type' => 'machine_name',
    '#default_value' => $optionset
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'source' => [
        'label',
      ],
      'exists' => '\\Drupal\\elevatezoomplus\\Entity\\ElevateZoomPlus::load',
    ],
    '#attributes' => $tooltip,
    '#disabled' => !$optionset
      ->isNew(),
  ];

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

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

  // Responsive JS options.
  $responds = $optionset
    ->getSetting('respond') ?: [];
  $form['respond'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Responsive display'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#attributes' => [
      'class' => [
        'details--respond',
        'has-tooltip',
      ],
    ],
    '#group' => 'options',
    '#parents' => [
      'options',
      'settings',
      'respond',
    ],
    '#description' => $this
      ->t('Containing breakpoints and settings objects.'),
  ];
  $classes = [
    'form-wrapper--table',
    'form-wrapper--table-respond',
  ];
  $form['respond']['settings'] = [
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => [
      $this
        ->t('Delta'),
      $this
        ->t('Range'),
      $this
        ->t('Enabled'),
      $this
        ->t('Settings'),
      $this
        ->t('Ops'),
    ],
    '#attributes' => [
      'class' => $classes,
    ],
    '#prefix' => '<div id="edit-respond-settings-wrapper">',
    '#suffix' => '</div>',
    '#group' => 'options',
    '#parents' => [
      'options',
      'settings',
      'respond',
    ],
  ];
  $num_responds = $form_state
    ->get('num_responds') ?: count($responds);
  if (empty($num_responds)) {
    $num_responds = 1;
  }
  $form_state
    ->set('num_responds', $num_responds);
  $excludes = [
    'cursor',
    'loadingIcon',
    'tint',
  ];
  for ($i = 0; $i <= $num_responds; $i++) {
    $form['respond']['settings'][$i]['delta'] = [
      '#markup' => $i,
    ];
    $form['respond']['settings'][$i]['range'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Range'),
      '#title_display' => 'invisible',
      '#default_value' => isset($responds[$i]['range']) ? $responds[$i]['range'] : '',
      '#size' => 40,
      '#max_length' => 120,
      '#description' => $this
        ->t('The window range to activate the responsive settings, e.g.: 600-799.'),
    ];
    $form['respond']['settings'][$i]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#title_display' => 'invisible',
      '#default_value' => isset($responds[$i]['enabled']) ? $responds[$i]['enabled'] : TRUE,
      '#size' => 40,
      '#max_length' => 120,
      '#description' => $this
        ->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'] = 'invisible';
    }
    $form['respond']['settings'][$i]['settings'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this
        ->t('Settings'),
      '#title_display' => 'invisible',
      '#attributes' => [
        'class' => [
          'form-wrapper--responsive',
        ],
      ],
      '#group' => $i,
      '#parents' => [
        'options',
        'settings',
        'respond',
        $i,
      ],
    ];
    $settings = isset($responds[$i]) ? $responds[$i] : [];
    $form['respond']['settings'][$i]['settings'] += $this
      ->attachSettingsForm($settings, $excludes);
    $form['respond']['settings'][$i]['remove_respond'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('x'),
      '#name' => 'remove_respond_' . $i,
      '#submit' => [
        [
          $this,
          'removeRespond',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'removeRespondCallback',
        ],
        'wrapper' => 'edit-respond-settings-wrapper',
      ],
      '#limit_validation_errors' => [],
    ];
  }
  $form['respond']['actions'] = [
    '#type' => 'actions',
  ];
  $form['respond']['actions']['add_respond'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another respond'),
    '#name' => 'add_respond',
    '#submit' => [
      [
        $this,
        'addRespond',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'addRespondCallback',
      ],
      'wrapper' => 'edit-respond-settings-wrapper',
    ],
    '#limit_validation_errors' => [],
  ];
  $form_state
    ->setCached(FALSE);
  return $form;
}