View source  
  <?php
namespace Drupal\blocktabs\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\blocktabs\ConfigurableTabInterface;
use Drupal\blocktabs\BlocktabsInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class TabFormBase extends FormBase {
  
  protected $blocktabs;
  
  protected $tab;
  
  public function getFormId() {
    return 'tab_form';
  }
  
  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;
    
    $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;
  }
  
  public function validateForm(array &$form, FormStateInterface $form_state) {
    
    $tab_data = (new FormState())
      ->setValues($form_state
      ->getValue('data'));
    $this->tab
      ->validateConfigurationForm($form, $tab_data);
    
    $form_state
      ->setValue('data', $tab_data
      ->getValues());
  }
  
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->cleanValues();
    
    $tab_data = (new FormState())
      ->setValues($form_state
      ->getValue('data'));
    $this->tab
      ->submitConfigurationForm($form, $tab_data);
    
    $form_state
      ->setValue('data', $tab_data
      ->getValues());
    $this->tab
      ->setTitle($form_state
      ->getValue('title'));
    $this->tab
      ->setWeight($form_state
      ->getValue('weight'));
    if (!$this->tab
      ->getUuid()) {
      $this->blocktabs
        ->addTab($this->tab
        ->getConfiguration());
    }
    else {
      $uuid = $this->tab
        ->getUuid();
      $config = $this->tab
        ->getConfiguration();
      $this->blocktabs
        ->getTabs()
        ->setInstanceConfiguration($uuid, $config);
    }
    
    $this->blocktabs
      ->save();
    \Drupal::messenger()
      ->addMessage($this
      ->t('The tab was successfully applied.'));
    $form_state
      ->setRedirectUrl($this->blocktabs
      ->toUrl('edit-form'));
  }
  
  protected abstract function prepareTab($tab);
}