You are here

abstract class WorkflowConfigTransitionFormBase in Workflow 8

Defines a class to build a draggable listing of Workflow Config Transitions entities.

Hierarchy

Expanded class hierarchy of WorkflowConfigTransitionFormBase

See also

\Drupal\workflow\Entity\WorkflowConfigTransition

1 file declares its use of WorkflowConfigTransitionFormBase
WorkflowAccessRoleForm.php in modules/workflow_access/src/Form/WorkflowAccessRoleForm.php

File

src/Form/WorkflowConfigTransitionFormBase.php, line 16

Namespace

Drupal\workflow\Form
View source
abstract class WorkflowConfigTransitionFormBase extends ConfigFormBase {

  /**
   * The key to use for the form element containing the entities.
   *
   * @var string
   */
  protected $entitiesKey = 'entities';

  /**
   * The WorkflowConfigTransition form type.
   *
   * @var string
   */
  protected $type;

  /**
   * The entities being listed.
   *
   * @var \Drupal\Core\Entity\EntityInterface[]
   */
  protected $entities = [];

  /**
   * The workflow object.
   *
   * @var \Drupal\workflow\Entity\Workflow
   */
  protected $workflow;

  /**
   * The messenger / logger service.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, LoggerInterface $logger) {

    // N.B. The $this->type and $this->entitiesKey must be set in the var section.
    parent::__construct($config_factory);
    $this->logger = $logger;

    // Get the Workflow from the page.
    $this->workflow = workflow_url_get_workflow();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('logger.factory')
      ->get('workflow'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'workflow_config_transition_' . $this->type . '_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [];
  }

  /**
   * {@inheritdoc}
   *
   * Create an $entity for every ConfigTransition.
   */
  public function load() {
    $entities = [];
    $entity_type = $this->entitiesKey;
    $workflow = $this->workflow;
    $states = $workflow
      ->getStates($all = 'CREATION');
    if ($states) {

      /** @var \Drupal\workflow\Entity\WorkflowState $from_state */

      /** @var \Drupal\workflow\Entity\WorkflowState $to_state */
      switch ($entity_type) {
        case 'workflow_state':
          foreach ($states as $from_state) {
            $from_sid = $from_state
              ->id();
            $entities[$from_sid] = $from_state;
          }
          break;
        case 'workflow_config_transition':
          foreach ($states as $from_state) {
            $from_sid = $from_state
              ->id();
            foreach ($states as $to_state) {
              $to_sid = $to_state
                ->id();

              // Don't allow transition TO (creation).
              if ($to_state
                ->isCreationState()) {
                continue;
              }

              // Only allow transitions from $from_state.
              if ($to_sid != $from_sid) {

                // continue.
              }

              // Load existing config_transitions. Create if not found.
              $config_transitions = $workflow
                ->getTransitionsByStateId($from_sid, $to_sid);
              if (!($config_transition = reset($config_transitions))) {
                $config_transition = $workflow
                  ->createTransition($from_sid, $to_sid);
              }
              $entities[] = $config_transition;
            }
          }
          break;
        default:
          $this
            ->messenger()
            ->addError($this
            ->t('Improper type provided in load method.'));
          $this->logger
            ->notice('Improper type provided in load method.', []);
      }
    }
    return $entities;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = [];
    if (!$this->workflow) {
      return $form;
    }

    /*
     * Begin of copied code DraggableListBuilder::buildForm()
     */
    $form[$this->entitiesKey] = [
      '#type' => 'table',
      '#header' => $this
        ->buildHeader(),
      '#sticky' => TRUE,
      '#empty' => $this
        ->t('There is no @label yet.', [
        '@label' => 'Transition',
      ]),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        ],
      ],
    ];
    $this->entities = $this
      ->load();
    $delta = 10;

    // Change the delta of the weight field if have more than 20 entities.
    if (!empty($this->weightKey)) {
      $count = count($this->entities);
      if ($count > 20) {
        $delta = ceil($count / 2);
      }
    }
    foreach ($this->entities as $entity) {
      $row = $this
        ->buildRow($entity);
      if (isset($row['label'])) {
        $row['label'] = [
          '#markup' => $row['label'],
        ];
      }
      if (isset($row['weight'])) {
        $row['weight']['#delta'] = $delta;
      }
      $form[$this->entitiesKey][$entity
        ->id()] = $row;
    }

    /*
     * End of copied code DraggableListBuilder::buildForm()
     */
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::submitForm public function Form submission handler. Overrides FormInterface::submitForm 26
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WorkflowConfigTransitionFormBase::$entities protected property The entities being listed.
WorkflowConfigTransitionFormBase::$entitiesKey protected property The key to use for the form element containing the entities. 3
WorkflowConfigTransitionFormBase::$logger protected property The messenger / logger service.
WorkflowConfigTransitionFormBase::$type protected property The WorkflowConfigTransition form type. 3
WorkflowConfigTransitionFormBase::$workflow protected property The workflow object.
WorkflowConfigTransitionFormBase::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
WorkflowConfigTransitionFormBase::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
WorkflowConfigTransitionFormBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames 1
WorkflowConfigTransitionFormBase::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 1
WorkflowConfigTransitionFormBase::load public function Create an $entity for every ConfigTransition.
WorkflowConfigTransitionFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct