You are here

public function UserBadgesListBuilder::buildForm in User Badges 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Controller/UserBadgesListBuilder.php, line 92
Contains \Drupal\user_badges\Controller\UserBadgesListBuilder.

Class

UserBadgesListBuilder
Class UserBadgesListBuilder.

Namespace

Drupal\user_badges\Controller

Code

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

  /** @var \Drupal\user\Entity\User $user */
  $user = $this->user;
  $form['badge'] = array(
    '#type' => 'table',
    '#header' => array(
      t('Label'),
      t('Weight'),
      t('Select Weight'),
    ),
    '#empty' => t('There are no badges yet'),
  );
  $field_item_list = $user
    ->get('field_user_badges');
  foreach ($field_item_list
    ->filterEmptyItems() as $index => $item) {

    /** @var \Drupal\user_badges\Entity\Badge $badge */
    $badge = $item
      ->get('entity')
      ->getValue();
    $form['badge'][$badge
      ->id()]['#attributes']['class'][] = 'draggable';
    $form['badge'][$badge
      ->id()]['#weight'] = $badge
      ->getBadgeWeight();

    // Some table columns containing raw markup.
    $form['badge'][$badge
      ->id()]['label'] = array(
      '#plain_text' => $badge
        ->label(),
    );
    $form['badge'][$badge
      ->id()]['id'] = array(
      '#plain_text' => $badge
        ->getBadgeWeight(),
    );

    // TableDrag: Weight column element.
    $form['badge'][$badge
      ->id()]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array(
        '@title' => $badge
          ->label(),
      )),
      '#title_display' => 'invisible',
      '#default_value' => $badge
        ->getBadgeWeight(),
      '#attributes' => array(
        'class' => array(
          'user-badges-order-weight',
        ),
      ),
    );
  }
  $form['badge']['#tabledrag'][] = array(
    'action' => 'order',
    'relationship' => 'sibling',
    'group' => 'user-badges-order-weight',
  );
  $form['actions'] = array(
    '#tree' => FALSE,
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Save Badges'),
    '#button_type' => 'primary',
  );
  return $form;
}