You are here

public function ParagraphsFrontendUIAddSet::buildForm in Paragraphs frontend ui 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/Form/ParagraphsFrontendUIAddSet.php, line 30

Class

ParagraphsFrontendUIAddSet
Class CleanupUrlAliases.

Namespace

Drupal\paragraphs_frontend_ui\Form

Code

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

  // Set the paragraph to the form state.
  $form_state
    ->addBuildInfo('paragraph', $paragraph);

  // Render the sets.
  $sets = \Drupal::entityTypeManager()
    ->getListBuilder('paragraph_set')
    ->load();
  $view_builder = \Drupal::entityTypeManager()
    ->getViewBuilder('paragraph_set');
  if (empty($sets)) {
    $form['empty_text'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('You have not created any paragraph sets.'),
    ];
  }
  foreach ($sets as $set) {
    $form['sets'][$set
      ->id()] = [
      '#type' => 'container',
    ];
    $form['sets'][$set
      ->id()]['element'] = $view_builder
      ->view($set);
    $form['sets'][$set
      ->id()]['add_set'] = [
      '#type' => 'button',
      '#name' => $set
        ->id() . '_add_set',
      '#value' => $this
        ->t('Add'),
      '#ajax' => [
        'callback' => [
          get_class($this),
          'addMoreAjax',
        ],
        'effect' => 'fade',
      ],
    ];
  }
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('add paragraph set entities')) {
    $form['add_set'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add a new set'),
      '#url' => Url::fromRoute('entity.paragraph_set.add_form'),
    ];
  }
  return $form;
}