You are here

protected function ChannelForm::buildSortsTable in Entity Share 8.3

Same name and namespace in other branches
  1. 8 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::buildSortsTable()
  2. 8.2 modules/entity_share_server/src/Form/ChannelForm.php \Drupal\entity_share_server\Form\ChannelForm::buildSortsTable()

Helper function to generate sort form elements.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

1 call to ChannelForm::buildSortsTable()
ChannelForm::form in modules/entity_share_server/src/Form/ChannelForm.php
Gets the actual form array to be built.

File

modules/entity_share_server/src/Form/ChannelForm.php, line 634

Class

ChannelForm
Entity form for the channel entity.

Namespace

Drupal\entity_share_server\Form

Code

protected function buildSortsTable(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\entity_share_server\Entity\ChannelInterface $channel */
  $channel = $this->entity;
  $channel_sorts = $channel
    ->get('channel_sorts');
  if (is_null($channel_sorts)) {
    $channel_sorts = [];
  }
  $form['channel_sorts'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('sorts'),
  ];
  if ($channel
    ->isNew()) {
    $form['channel_sorts']['sort_message'] = [
      '#markup' => $this
        ->t("It will be possible to add sorts after the channel's creation."),
    ];
  }
  else {
    $form['channel_sorts']['sort_actions'] = [
      '#type' => 'actions',
      '#weight' => -5,
    ];
    $form['channel_sorts']['sort_actions']['sort_add'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add a new sort'),
      '#url' => Url::fromRoute('entity_share_server.sort_add_form', [
        'channel' => $channel
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'button',
          'button--primary',
        ],
      ],
    ];
    $header = [
      'id' => [
        'data' => $this
          ->t('ID'),
      ],
      'path' => [
        'data' => $this
          ->t('Path'),
      ],
      'direction' => [
        'data' => $this
          ->t('Direction'),
      ],
      'weight' => [
        'data' => $this
          ->t('Weight'),
      ],
      'operations' => [
        'data' => $this
          ->t('Operations'),
      ],
    ];
    $form['channel_sorts']['sort_table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#empty' => $this
        ->t('There is currently no sort for this channel.'),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        ],
      ],
    ];
    uasort($channel_sorts, [
      SortArray::class,
      'sortByWeightElement',
    ]);
    foreach ($channel_sorts as $sort_id => $sort) {
      $row = [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'id' => [
          '#markup' => $sort_id,
        ],
        'path' => [
          '#markup' => $sort['path'],
        ],
        'direction' => [
          '#markup' => $sort['direction'],
        ],
        'weight' => [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight'),
          '#title_display' => 'invisible',
          '#default_value' => $sort['weight'],
          '#attributes' => [
            'class' => [
              'weight',
            ],
          ],
        ],
        'operations' => [
          '#type' => 'dropbutton',
          '#links' => [
            'edit' => [
              'title' => $this
                ->t('Edit'),
              'url' => Url::fromRoute('entity_share_server.sort_edit_form', [
                'channel' => $channel
                  ->id(),
                'sort' => $sort_id,
              ]),
            ],
            'delete' => [
              'title' => $this
                ->t('Delete'),
              'url' => Url::fromRoute('entity_share_server.sort_delete_form', [
                'channel' => $channel
                  ->id(),
                'sort' => $sort_id,
              ]),
            ],
          ],
        ],
      ];
      $form['channel_sorts']['sort_table'][$sort_id] = $row;
    }
  }
}