Commit 0c5527f9 authored by Crell's avatar Crell

Issue #2695543 by Crell: Form mismatch loses order setting in Workbench Transitions

Merge branch '2695543-transition-weight' into 8.x-1.x
parents 0f7a0719 58ebb589
......@@ -3,8 +3,10 @@
namespace Drupal\workbench_moderation\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\workbench_moderation\Entity\ModerationState;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class ModerationStateTransitionForm.
......@@ -12,6 +14,34 @@ use Drupal\workbench_moderation\Entity\ModerationState;
* @package Drupal\workbench_moderation\Form
*/
class ModerationStateTransitionForm extends EntityForm {
/**
* @var
*/
protected $entityTypeManager;
/**
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $queryFactory;
/**
* Constructs a new ModerationStateTransitionForm.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, QueryFactory $query_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->queryFactory = $query_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'), $container->get('entity.query'));
}
/**
* {@inheritdoc}
*/
......@@ -39,7 +69,7 @@ class ModerationStateTransitionForm extends EntityForm {
];
$options = [];
foreach (ModerationState::loadMultiple() as $moderation_state) {
foreach ($this->entityTypeManager->getStorage('moderation_state')->loadMultiple() as $moderation_state) {
$options[$moderation_state->id()] = $moderation_state->label();
}
......@@ -68,8 +98,15 @@ class ModerationStateTransitionForm extends EntityForm {
'#default_value' => $moderation_state_transition->getToState(),
];
// Make sure there's always at least a wide enough delta on weight to cover
// the current value or the total number of transitions. That way we
// never end up forcing a transition to change its weight needlessly.
$num_transitions = $this->queryFactory->get('moderation_state_transition')->count()->execute();
$delta = max(abs($moderation_state_transition->getWeight()), $num_transitions);
$form['weight'] = [
'#type' => 'weight',
'#delta' => $delta,
'#options' => $options,
'#title' => $this->t('Weight'),
'#default_value' => $moderation_state_transition->getWeight(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment