<?php

namespace Drupal\workflows\Plugin;

use Drupal\Component\Plugin\PluginAwareInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * A base class for workflow type transition forms.
 */
abstract class WorkflowTypeTransitionFormBase implements PluginFormInterface, PluginAwareInterface {

  use StringTranslationTrait;

  /**
   * The workflow type.
   *
   * @var \Drupal\workflows\WorkflowTypeInterface
   */
  protected $workflowType;

  /**
   * {@inheritdoc}
   */
  public function setPlugin(PluginInspectionInterface $plugin) {
    $this->workflowType = $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $transition = $form_state->get('transition');
    $configuration = $this->workflowType->getConfiguration();
    $configuration['transitions'][$transition->id()] = $values + $configuration['transitions'][$transition->id()];
    $this->workflowType->setConfiguration($configuration);
  }

}
