View source
<?php
namespace Drupal\drupal_slider\Form;
use Drupal\Core\Form\FormStateInterface;
class SlideGroupEditForm extends SlideGroupBaseForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$slideGroup = $this->entity;
$entity = \Drupal::entityTypeManager()
->getStorage('drupal_slider_slide')
->loadMultiple();
foreach ($entity as $key => $value) {
$options[$key] = $key;
}
$slides = $slideGroup
->get('slides');
$slides_count = count($slides);
$num_names = $form_state
->get('num_names');
if ($num_names === NULL) {
$num_names = $slides_count;
$form_state
->set('num_names', $num_names);
}
for ($i = 1; $i <= $num_names; $i++) {
$single_slide = array_slice($slides, $i - 1, 1);
$form['slides'][$i]['#attributes']['class'][] = 'draggable';
$form['slides'][$i]['#weight'] = isset($single_slide[0]['weight']) ? $single_slide[0]['weight'] : $i;
$form['slides'][$i]['name'] = [
'#markup' => $i,
];
$form['slides'][$i]['slide_list'] = [
'#type' => 'select',
'#title' => $this
->t('Slide'),
'#options' => $options,
'#default_value' => isset($single_slide[0]['slide_list']) ? $single_slide[0]['slide_list'] : '',
'#description' => $this
->t('Choose the slide.'),
];
$form['slides'][$i]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#title_display' => 'invisible',
'#default_value' => isset($single_slide[0]['weight']) ? $single_slide[0]['weight'] : $i,
'#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('Update Group');
return $actions;
}
}