You are here

protected function DynamicLayoutForm::addRowFormFields in Dynamic Layouts 8

1 call to DynamicLayoutForm::addRowFormFields()
DynamicLayoutForm::form in src/Form/DynamicLayoutForm.php
Gets the actual form array to be built.

File

src/Form/DynamicLayoutForm.php, line 228

Class

DynamicLayoutForm
Form controller for the DynamicLayout entity edit forms.

Namespace

Drupal\dynamic_layouts\Form

Code

protected function addRowFormFields(array &$form) {

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

  // Check if we need to render rows.
  if ($rows = $entity
    ->getRows()) {
    $wrapper_classes = $this
      ->getWrapperClasses();
    $form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'] = [
      '#type' => 'markup',
      '#prefix' => '<div class="' . $wrapper_classes . '">',
      '#suffix' => '</div>',
    ];

    // Loop over the rows.
    $row_count = 1;
    foreach ($rows as $row) {
      if (!isset($row['row_id'])) {
        continue;
      }
      $row_id = $row['row_id'];
      $row['row_count'] = $row_count;
      $addLinkOptions = $editLinkOptions = $deleteLinkOptions = [
        'attributes' => [
          'class' => [
            'use-ajax',
            'btn',
          ],
        ],
      ];
      $addLinkTitle = t('Add column');
      $editLinkTitle = t('Edit row');
      $deleteLinkTitle = t('Delete row');
      $addLinkOptions['attributes']['class'][] = 'add-column-link';
      $editLinkOptions['attributes']['class'][] = 'edit-link';
      $deleteLinkOptions['attributes']['class'][] = 'delete-link';
      $addLinkOptions['attributes']['title'] = $addLinkTitle;
      $editLinkOptions['attributes']['title'] = $editLinkTitle;
      $deleteLinkOptions['attributes']['title'] = $deleteLinkTitle;
      $row['add_column_link'] = $entity
        ->getRowLink($row_id, 'dynamic_layouts.add_column', $addLinkTitle, $addLinkOptions);
      $row['edit_row_link'] = $entity
        ->getRowLink($row_id, 'dynamic_layouts.edit_row_modal_form', $editLinkTitle, $editLinkOptions);
      $row['delete_row_link'] = $entity
        ->getRowLink($row_id, 'dynamic_layouts.delete_row', $deleteLinkTitle, $deleteLinkOptions);
      $elements = [
        '#theme' => 'dynamic_layouts_backend',
        '#row' => $row,
      ];
      $rendered_row = $this->renderer
        ->render($elements);
      $form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'][$row_id] = [
        '#type' => 'inline_template',
        '#template' => '{{ row | raw }}',
        '#context' => [
          'row' => $rendered_row,
        ],
      ];
      $form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['rows']['wrapper'][$row_id]['actions'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'actions-wrapper',
            'col-12',
          ],
        ],
      ];
      $row_count++;
    }
    $addRowTitle = t('Add row');
    $form[Constants::LAYOUT][Constants::LAYOUT_GROUP]['add_row_bottom'] = [
      '#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,
      ],
    ];
  }
}