You are here

public function DynamicLayoutForm::form in Dynamic Layouts 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/DynamicLayoutForm.php, line 47

Class

DynamicLayoutForm
Form controller for the DynamicLayout entity edit forms.

Namespace

Drupal\dynamic_layouts\Form

Code

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

  /** @var \Drupal\dynamic_layouts\DynamicLayoutInterface $entity */
  $entity = $this->entity;

  // Attach dynamic layout & popups dialogs/modals libraries.
  $form['#attached']['library'][] = 'dynamic_layouts/dynamic_layouts';
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

  // Disable caching for the form.
  $form['#cache'] = [
    'max-age' => 0,
  ];

  // Do not flatten nested form fields.
  $form['#tree'] = TRUE;

  // Check if the entity is new, set some vars.
  $entity_is_new = FALSE;
  if ($entity
    ->isNew()) {
    $entity_is_new = TRUE;
  }
  $this
    ->addSettingsFormFields($form, $entity);
  $form[Constants::DYNAMIC_LAYOUT] = array(
    '#type' => 'vertical_tabs',
    '#title' => t('Dynamic layout'),
  );
  if (!$entity_is_new) {

    // We need to set the action to current entity url because form action
    // is changing when using: new ReplaceCommand in all Ajax forms.
    $url = $entity
      ->toUrl()
      ->toString();
    $form['#action'] = $url;
    $form[Constants::LAYOUT] = [
      '#type' => 'container',
      '#title' => $this
        ->t('Layout'),
      '#tree' => TRUE,
      // Set up the wrapper so that AJAX will be able to replace the fieldset.
      '#prefix' => '<div id="js-ajax-elements-wrapper">',
      '#suffix' => '</div>',
    ];
    $form[Constants::LAYOUT][Constants::LAYOUT_GROUP] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Layout'),
      '#tree' => TRUE,
      '#group' => Constants::DYNAMIC_LAYOUT,
      '#description' => t('To change the default names of the columns, click "Edit column".'),
      '#attributes' => [
        'class' => [
          'layout-group',
        ],
      ],
    ];
    $addRowTitle = t('Add row');
    $form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['add_row_top'] = [
      '#type' => 'link',
      '#name' => 'add_row',
      '#title' => $addRowTitle,
      '#prefix' => '<div class="add-row-wrapper">',
      '#suffix' => '</div>',
      '#url' => Url::fromRoute('dynamic_layouts.add_row', [
        'dynamic_layout_id' => $entity
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'btn',
          'add-row',
          'use-ajax',
        ],
        'title' => $addRowTitle,
      ],
    ];
  }

  // Add the row fields.
  $this
    ->addRowFormFields($form);
  $form_state
    ->setCached(FALSE);

  // Submit.
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Testing'),
  ];
  return parent::form($form, $form_state);
}