You are here

public function AnimateAnyForm::buildForm in Animate Any 8

Build Animate Any Setting Form.

Overrides FormInterface::buildForm

File

src/Form/AnimateAnyForm.php, line 23

Class

AnimateAnyForm
Provides the Animate Any form.

Namespace

Drupal\animate_any\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Fetch animate.css from library.
  $animate_css = DRUPAL_ROOT . '/libraries/animate.css/animate.css';

  // Check animate.css file exists.
  if (!file_exists($animate_css)) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('animate.css library is missing.'), 'warning');
  }

  // Building add more form element to add animation.
  $form['#attached']['library'][] = 'animate_any/animate';
  $form['parent_class'] = [
    '#title' => $this
      ->t('Add Parent Class / ID'),
    '#description' => $this
      ->t('You can add body class like <em>body.front (for front page)</em> OR class with dot(.) prefix and Id with hash(#) prefix.'),
    '#type' => 'textfield',
  ];
  $form['#tree'] = TRUE;
  $form['animate_fieldset'] = [
    '#prefix' => '<div id="item-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => [],
    '#rows' => [],
    '#attributes' => [
      'class' => 'animation',
    ],
  ];
  $field_deltas = $form_state
    ->get('field_deltas');
  if (is_null($field_deltas)) {
    $field_deltas = \NULL;
    $form_state
      ->set('field_deltas', $field_deltas);
  }
  if (!\is_null($field_deltas)) {
    for ($delta = 0; $delta < $field_deltas; $delta++) {
      $section_identity = [
        '#title' => $this
          ->t('Add section class/Id'),
        '#description' => $this
          ->t('Add class with dot(.) prefix and Id with hash(#) prefix.'),
        '#type' => 'textfield',
        '#size' => 20,
      ];
      $section_event = [
        '#title' => $this
          ->t('Select event'),
        '#type' => 'select',
        '#options' => animate_on_event(),
        '#attributes' => [
          'class' => [
            'select_event',
          ],
        ],
      ];
      $section_animation = [
        '#title' => $this
          ->t('Select animation'),
        '#type' => 'select',
        '#options' => animate_any_options(),
        '#attributes' => [
          'class' => [
            'select_animate',
          ],
        ],
      ];
      $animation = [
        '#markup' => 'ANIMATE ANY',
        '#prefix' => '<h1 id="animate" class="" style="font-size: 30px;">',
        '#suffix' => '</h1>',
      ];
      $remove = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Remove'),
        '#submit' => [
          '::animate_any_custom_add_more_remove_one',
        ],
        '#ajax' => [
          'callback' => '::animate_any_custom_remove_callback',
          'wrapper' => 'item-fieldset-wrapper',
        ],
        '#name' => 'remove_name_' . $delta,
      ];
      $form['animate_fieldset'][$delta] = [
        'section_identity' => &$section_identity,
        'section_event' => &$section_event,
        'section_animation' => &$section_animation,
        'animation' => &$animation,
        'remove' => &$remove,
      ];
      $form['animate_fieldset']['#rows'][$delta] = [
        [
          'data' => &$section_identity,
        ],
        [
          'data' => &$section_event,
        ],
        [
          'data' => &$section_animation,
        ],
        [
          'data' => &$animation,
        ],
        [
          'data' => &$remove,
        ],
      ];
      unset($section_identity);
      unset($section_event);
      unset($section_animation);
      unset($animation);
      unset($remove);
    }
  }
  $form['instruction'] = [
    '#markup' => '<strong>Click on <i>Add item</i> button to add animation section.</strong>',
    '#prefix' => '<div class="form-item">',
    '#suffix' => '</div>',
  ];

  // Add more button with ajax callback.
  $form['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add Item'),
    '#submit' => [
      '::animate_any_custom_add_more_add_one',
    ],
    '#ajax' => [
      'callback' => '::animate_any_custom_add_more_callback',
      'wrapper' => 'item-fieldset-wrapper',
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save Settings'),
  ];
  return $form;
}