You are here

public function SlideGroupAddForm::buildForm in Drupal Slider 8

Same name and namespace in other branches
  1. 8.2 src/Form/SlideGroupAddForm.php \Drupal\drupal_slider\Form\SlideGroupAddForm::buildForm()

Function buildForm.

Overrides SlideGroupBaseForm::buildForm

File

src/Form/SlideGroupAddForm.php, line 15

Class

SlideGroupAddForm
Class SlideGroupAddForm.

Namespace

Drupal\drupal_slider\Form

Code

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

  // Get anything we need from the base class.
  $form = parent::buildForm($form, $form_state);
  $entity = \Drupal::entityTypeManager()
    ->getStorage('drupal_slider_slide')
    ->loadMultiple();
  foreach ($entity as $key => $value) {
    $options[$key] = $key;
  }

  // Gather the number of names in the form already.
  $num_names = $form_state
    ->get('num_names');

  // We have to ensure that there is at least one name field.
  if ($num_names === NULL) {
    $form_state
      ->set('num_names', 1);
    $num_names = 1;
  }
  for ($i = 1; $i <= $num_names; $i++) {

    // TableDrag: Mark the table row as draggable.
    $form['slides'][$i]['#attributes']['class'][] = 'draggable';

    // TableDrag: Sort the table row according to its existing/configured
    // weight.
    $form['slides'][$i]['#weight'] = $i;

    // Some table columns containing raw markup.
    $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.'),
    ];

    // TableDrag: Weight column element.
    $form['slides'][$i]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => '',
      // Classify the weight element for #tabledrag.
      '#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 there is more than one name, add the remove button.
  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;
}