You are here

public function TabFormBase::buildForm in Block Tabs 8

Parameters

\Drupal\blocktabs\BlocktabsInterface $blocktabs: The block tabs.

string $tab: The tab ID.

Return value

array The form structure.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Overrides FormInterface::buildForm

2 calls to TabFormBase::buildForm()
TabAddForm::buildForm in src/Form/TabAddForm.php
Form constructor.
TabEditForm::buildForm in src/Form/TabEditForm.php
Form constructor.
2 methods override TabFormBase::buildForm()
TabAddForm::buildForm in src/Form/TabAddForm.php
Form constructor.
TabEditForm::buildForm in src/Form/TabEditForm.php
Form constructor.

File

src/Form/TabFormBase.php, line 52

Class

TabFormBase
Provides a base form for tab.

Namespace

Drupal\blocktabs\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, BlocktabsInterface $blocktabs = NULL, $tab = NULL) {
  $this->blocktabs = $blocktabs;
  try {
    $this->tab = $this
      ->prepareTab($tab);
  } catch (PluginNotFoundException $e) {
    throw new NotFoundHttpException("Invalid tab id: '{$tab}'.");
  }
  $request = $this
    ->getRequest();
  if (!$this->tab instanceof ConfigurableTabInterface) {
    throw new NotFoundHttpException();
  }
  $form['#attached']['library'][] = 'blocktabs/admin';
  $form['uuid'] = [
    '#type' => 'hidden',
    '#value' => $this->tab
      ->getUuid(),
  ];
  $form['id'] = [
    '#type' => 'hidden',
    '#value' => $this->tab
      ->getPluginId(),
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Tab title'),
    '#default_value' => $this->tab
      ->getTitle(),
    '#required' => TRUE,
  ];
  $form['data'] = $this->tab
    ->buildConfigurationForm([], $form_state);
  $form['data']['#tree'] = TRUE;

  // Check the URL for a weight, then the tab, otherwise use default.
  $form['weight'] = [
    '#type' => 'hidden',
    '#value' => $request->query
      ->has('weight') ? (int) $request->query
      ->get('weight') : $this->tab
      ->getWeight(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#button_type' => 'primary',
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => $this->blocktabs
      ->toUrl('edit-form'),
    '#attributes' => [
      'class' => [
        'button',
      ],
    ],
  ];
  return $form;
}