View source
<?php
namespace Drupal\drupal_slider\Form;
use Drupal\Core\Form\FormStateInterface;
class SlideGroupAddForm extends SlideGroupBaseForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = \Drupal::entityTypeManager()
->getStorage('drupal_slider_slide')
->loadMultiple();
foreach ($entity as $key => $value) {
$options[$key] = $key;
}
$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['slides'][$i]['#attributes']['class'][] = 'draggable';
$form['slides'][$i]['#weight'] = $i;
$form['slides'][$i]['name'] = [
'#markup' => $i,
];
$form['slides'][$i]['slide_list'] = [
'#type' => 'select',
'#title' => $this
->t('Slide'),
'#options' => $options,
'#description' => $this
->t('Choose the slide.'),
];
$form['slides'][$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 Group');
return $actions;
}
}