View source
<?php
namespace Drupal\drupal_slider\Form;
use Drupal\Core\Form\FormStateInterface;
class SlideAddForm extends SlideBaseForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$num_names = $form_state
->get('num_names');
if ($num_names === NULL) {
$form_state
->set('num_names', 1);
$num_names = 1;
}
for ($i = 1; $i <= $num_names; $i++) {
$form['layers'][$i]['#attributes']['class'][] = 'draggable';
$form['layers'][$i]['#weight'] = $i;
$form['layers'][$i]['name'] = [
'#markup' => $i,
];
$form['layers'][$i]['attributes'] = [
'#type' => 'fieldset',
'#title' => '',
'#open' => TRUE,
];
$form['layers'][$i]['attributes']['horizontal'] = [
'#title' => 'Horizontal',
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => '',
];
$form['layers'][$i]['attributes']['vertical'] = [
'#title' => 'Vertical',
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => '',
];
$form['layers'][$i]['attributes']['show_delay'] = [
'#title' => 'Show delay',
'#type' => 'number',
'#required' => TRUE,
'#default_value' => 0,
];
$form['layers'][$i]['attributes']['hide_delay'] = [
'#title' => 'Hide delay',
'#type' => 'number',
'#required' => TRUE,
'#default_value' => 0,
];
$form['layers'][$i]['attributes']['show_transition'] = [
'#type' => 'select',
'#title' => $this
->t('Show transition'),
'#options' => [
'left' => $this
->t('Left'),
'right' => $this
->t('Right'),
'top' => $this
->t('Top'),
'bottom' => $this
->t('Bottom'),
'up' => $this
->t('Up'),
'down' => $this
->t('Down'),
],
];
$form['layers'][$i]['attributes']['hide_transition'] = [
'#type' => 'select',
'#title' => $this
->t('Hide transition'),
'#options' => [
'left' => $this
->t('Left'),
'right' => $this
->t('Right'),
'top' => $this
->t('Top'),
'bottom' => $this
->t('Bottom'),
'up' => $this
->t('Up'),
'down' => $this
->t('Down'),
],
];
$form['layers'][$i]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#title_display' => 'invisible',
'#default_value' => '',
'#attributes' => [
'class' => [
'table-sort-weight',
],
],
];
}
$form['actions']['add_name'] = [
'#type' => 'submit',
'#value' => $this
->t('Add one more'),
'#submit' => [
'::addOne',
],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'names-fieldset-wrapper',
],
];
if ($num_names >= 1) {
$form['actions']['remove_name'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove one'),
'#submit' => [
'::removeCallback',
],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'names-fieldset-wrapper',
],
];
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Create Slide');
return $actions;
}
}