You are here

public function AvatarKitServicesForm::buildForm in Avatar Kit 8.2

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 ConfigFormBase::buildForm

File

src/Form/AvatarKitServicesForm.php, line 65

Class

AvatarKitServicesForm
Configure Avatar Kit services.

Namespace

Drupal\avatars\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) : array {
  $headers = [
    'label' => $this
      ->t('Service'),
    'plugin' => $this
      ->t('Plugin'),
    'weight' => $this
      ->t('Weight'),
    'operations' => $this
      ->t('Operations'),
  ];
  $table_drag_group = 'avatar-service-weight';
  $form['services'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#empty' => $this
      ->t('No avatar service instances found.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $table_drag_group,
      ],
    ],
    '#default_value' => [],
  ];

  /** @var \Drupal\avatars\Entity\AvatarKitServiceInterface[] $instances */
  $instances = AvatarKitService::loadMultiple();
  uasort($instances, [
    AvatarKitService::class,
    'sort',
  ]);
  foreach ($instances as $instance) {
    $row = [];
    $row['#attributes']['class'][] = 'draggable';
    $row['label']['#plain_text'] = $instance
      ->label();
    $definition = $instance
      ->getPlugin()
      ->getPluginDefinition();
    $row['plugin']['#plain_text'] = $definition['label'] ?? '';
    $row['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $instance
        ->getWeight(),
      '#attributes' => [
        'class' => [
          $table_drag_group,
        ],
      ],
    ];
    $row['operations'] = [
      '#type' => 'operations',
      '#links' => $this
        ->getOperations($instance),
    ];
    $id = $instance
      ->id();
    $form['services'][$id] = $row;
  }
  return parent::buildForm($form, $form_state);
}