Unverified Commit a16b9c74 authored by larowlan's avatar larowlan
Browse files

Issue #2893778 by timmillwood, Sam152, Wim Leers: Remove deprecated Workflows methods

parent 04d69926
......@@ -103,7 +103,7 @@ public function entityPresave(EntityInterface $entity) {
if ($entity->moderation_state->value) {
$workflow = $this->moderationInfo->getWorkflowForEntity($entity);
/** @var \Drupal\content_moderation\ContentModerationState $current_state */
$current_state = $workflow->getState($entity->moderation_state->value);
$current_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
// This entity is default if it is new, a new translation, the default
// revision, or the default revision is not published.
......
......@@ -59,7 +59,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) {
->read($unprocessed_configuration);
$diff = array_diff_key($workflow_config['type_settings']['states'], $original_workflow_config['type_settings']['states']);
foreach (array_keys($diff) as $state_id) {
$state = $workflow->getState($state_id);
$state = $workflow->getTypePlugin()->getState($state_id);
if ($workflow->getTypePlugin()->workflowStateHasData($workflow, $state)) {
$event->getConfigImporter()->logError($this->t('The moderation state @state_label is being used, but is not in the source storage.', ['@state_label' => $state->label()]));
}
......
......@@ -89,7 +89,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ContentEn
$form['current'] = [
'#type' => 'item',
'#title' => $this->t('Status'),
'#markup' => $workflow->getState($current_state)->label(),
'#markup' => $workflow->getTypePlugin()->getState($current_state)->label(),
];
}
......@@ -137,7 +137,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_set_message($this->t('The moderation state has been updated.'));
$new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getState($new_state);
$new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getTypePlugin()->getState($new_state);
// The page we're on likely won't be visible if we just set the entity to
// the default state, as we hide that latest-revision tab if there is no
// forward revision. Redirect to the canonical URL instead, since that will
......
......@@ -154,7 +154,7 @@ public function isLiveRevision(ContentEntityInterface $entity) {
return $this->isLatestRevision($entity)
&& $entity->isDefaultRevision()
&& $entity->moderation_state->value
&& $workflow->getState($entity->moderation_state->value)->isPublishedState();
&& $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
}
/**
......@@ -171,13 +171,13 @@ public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
// Load the translated revision.
$language_revision = $default_revision->getTranslation($language->getId());
// Return TRUE if a translation with a published state is found.
if ($workflow->getState($language_revision->moderation_state->value)->isPublishedState()) {
if ($workflow->getTypePlugin()->getState($language_revision->moderation_state->value)->isPublishedState()) {
return TRUE;
}
}
}
return $workflow->getState($default_revision->moderation_state->value)->isPublishedState();
return $workflow->getTypePlugin()->getState($default_revision->moderation_state->value)->isPublishedState();
}
/**
......
......@@ -24,7 +24,7 @@ public function transitionPermissions() {
$permissions = [];
/** @var \Drupal\workflows\WorkflowInterface $workflow */
foreach (Workflow::loadMultipleByType('content_moderation') as $id => $workflow) {
foreach ($workflow->getTransitions() as $transition) {
foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
$permissions['use ' . $workflow->id() . ' transition ' . $transition->id()] = [
'title' => $this->t('Use %transition transition from %workflow workflow.', [
'%transition' => $transition->label(),
......
......@@ -61,7 +61,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
$workflow = $this->moderationInformation->getWorkflowForEntity($items->getEntity());
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $workflow->getState($item->value)->label(),
'#markup' => $workflow->getTypePlugin()->getState($item->value)->label(),
];
}
return $elements;
......
......@@ -116,7 +116,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
}
$workflow = $this->moderationInformation->getWorkflowForEntity($entity);
$default = $items->get($delta)->value ? $workflow->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
$default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
/** @var \Drupal\workflows\Transition[] $transitions */
$transitions = $this->validator->getValidTransitions($entity, $this->currentUser);
......
......@@ -132,9 +132,9 @@ public function onChange($delta) {
// Change the entity's default revision flag and the publishing status only
// if the new workflow state is a valid one.
if ($workflow->hasState($current_state_id)) {
if ($workflow->getTypePlugin()->hasState($current_state_id)) {
/** @var \Drupal\content_moderation\ContentModerationState $current_state */
$current_state = $workflow->getState($current_state_id);
$current_state = $workflow->getTypePlugin()->getState($current_state_id);
// This entity is default if it is new, a new translation, the default
// revision state, or the default revision is not published.
......
......@@ -78,7 +78,7 @@ public function validate($value, Constraint $constraint) {
$workflow = $this->moderationInformation->getWorkflowForEntity($entity);
if (!$workflow->hasState($entity->moderation_state->value)) {
if (!$workflow->getTypePlugin()->hasState($entity->moderation_state->value)) {
// If the state we are transitioning to doesn't exist, we can't validate
// the transitions for this entity further.
$this->context->addViolation($constraint->invalidStateMessage, [
......@@ -100,12 +100,12 @@ public function validate($value, Constraint $constraint) {
// we cannot do any further validation of transitions, because none will
// be setup for a state that doesn't exist. Instead allow any state to
// take its place.
if (!$workflow->hasState($original_entity->moderation_state->value)) {
if (!$workflow->getTypePlugin()->hasState($original_entity->moderation_state->value)) {
return;
}
$new_state = $workflow->getState($entity->moderation_state->value);
$original_state = $workflow->getState($original_entity->moderation_state->value);
$new_state = $workflow->getTypePlugin()->getState($entity->moderation_state->value);
$original_state = $workflow->getTypePlugin()->getState($original_entity->moderation_state->value);
if (!$original_state->canTransitionTo($new_state->id())) {
$this->context->addViolation($constraint->message, [
......
......@@ -369,7 +369,7 @@ public function getConfiguration() {
*/
public function getInitialState(WorkflowInterface $workflow, $entity = NULL) {
if ($entity instanceof EntityPublishedInterface) {
return $workflow->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft');
return $workflow->getTypePlugin()->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft');
}
return parent::getInitialState($workflow);
}
......
......@@ -40,7 +40,7 @@ public function __construct(ModerationInformationInterface $moderation_info) {
*/
public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) {
$workflow = $this->moderationInfo->getWorkflowForEntity($entity);
$current_state = $entity->moderation_state->value ? $workflow->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
$current_state = $entity->moderation_state->value ? $workflow->getTypePlugin()->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity);
return array_filter($current_state->getTransitions(), function(Transition $transition) use ($workflow, $user) {
return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());
......
......@@ -137,7 +137,7 @@ public function testInvalidStateWithoutExisting() {
// Enable moderation to test validation on existing content, with no
// explicit state.
$workflow = Workflow::load('editorial');
$workflow->addState('deleted_state', 'Deleted state');
$workflow->getTypePlugin()->addState('deleted_state', 'Deleted state');
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
$workflow->save();
......@@ -152,7 +152,7 @@ public function testInvalidStateWithoutExisting() {
// Delete the state so $node->original contains an invalid state when
// validating.
$workflow->deleteState('deleted_state');
$workflow->getTypePlugin()->deleteState('deleted_state');
$workflow->save();
$node->moderation_state->value = 'draft';
$violations = $node->validate();
......
......@@ -69,6 +69,7 @@ protected function setUpModerationInformation(ContentEntityInterface $entity) {
$workflow = new Workflow(['id' => 'process', 'type' => 'content_moderation'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'draft')
->addState('needs_review', 'needs_review')
->addState('published', 'published')
......
......@@ -104,7 +104,7 @@ class Workflow extends ConfigEntityBase implements WorkflowInterface, EntityWith
*/
public function preSave(EntityStorageInterface $storage) {
$workflow_type = $this->getTypePlugin();
$missing_states = array_diff($workflow_type->getRequiredStates(), array_keys($this->getStates()));
$missing_states = array_diff($workflow_type->getRequiredStates(), array_keys($this->getTypePlugin()->getStates()));
if (!empty($missing_states)) {
throw new RequiredStateMissingException(sprintf("Workflow type '{$workflow_type->label()}' requires states with the ID '%s' in workflow '{$this->id()}'", implode("', '", $missing_states)));
}
......@@ -172,146 +172,4 @@ public function onDependencyRemoval(array $dependencies) {
return parent::onDependencyRemoval($dependencies) || $changed;
}
/**
* {@inheritdoc}
*/
public function addState($state_id, $label) {
$this->getTypePlugin()->addState($state_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function hasState($state_id) {
return $this->getTypePlugin()->hasState($state_id);
}
/**
* {@inheritdoc}
*/
public function getStates($state_ids = NULL) {
return $this->getTypePlugin()->getStates($state_ids);
}
/**
* {@inheritdoc}
*/
public function getState($state_id) {
return $this->getTypePlugin()->getState($state_id);
}
/**
* {@inheritdoc}
*/
public function setStateLabel($state_id, $label) {
$this->getTypePlugin()->setStateLabel($state_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function setStateWeight($state_id, $weight) {
$this->getTypePlugin()->setStateWeight($state_id, $weight);
return $this;
}
/**
* {@inheritdoc}
*/
public function deleteState($state_id) {
$this->getTypePlugin()->deleteState($state_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getInitialState() {
return $this->getTypePlugin()->getInitialState($this);
}
/**
* {@inheritdoc}
*/
public function addTransition($id, $label, array $from_state_ids, $to_state_id) {
$this->getTypePlugin()->addTransition($id, $label, $from_state_ids, $to_state_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getTransition($transition_id) {
return $this->getTypePlugin()->getTransition($transition_id);
}
/**
* {@inheritdoc}
*/
public function hasTransition($transition_id) {
return $this->getTypePlugin()->hasTransition($transition_id);
}
/**
* {@inheritdoc}
*/
public function getTransitions(array $transition_ids = NULL) {
return $this->getTypePlugin()->getTransitions($transition_ids);
}
/**
* {@inheritdoc}
*/
public function getTransitionsForState($state_id, $direction = 'from') {
return $this->getTypePlugin()->getTransitionsForState($state_id, $direction);
}
/**
* {@inheritdoc}
*/
public function getTransitionFromStateToState($from_state_id, $to_state_id) {
return $this->getTypePlugin()->getTransitionFromStateToState($from_state_id, $to_state_id);
}
/**
* {@inheritdoc}
*/
public function hasTransitionFromStateToState($from_state_id, $to_state_id) {
return $this->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $to_state_id);
}
/**
* {@inheritdoc}
*/
public function setTransitionLabel($transition_id, $label) {
$this->getTypePlugin()->setTransitionLabel($transition_id, $label);
return $this;
}
/**
* {@inheritdoc}
*/
public function setTransitionWeight($transition_id, $weight) {
$this->getTypePlugin()->setTransitionWeight($transition_id, $weight);
return $this;
}
/**
* {@inheritdoc}
*/
public function setTransitionFromStates($transition_id, array $from_state_ids) {
$this->getTypePlugin()->setTransitionFromStates($transition_id, $from_state_ids);
return $this;
}
/**
* {@inheritdoc}
*/
public function deleteTransition($transition_id) {
$this->getTypePlugin()->deleteTransition($transition_id);
return $this;
}
}
......@@ -87,7 +87,7 @@ public function save(array $form, FormStateInterface $form_state) {
// Initialize the workflow using the selected type plugin.
$workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
$return = $workflow->save();
if (empty($workflow->getStates())) {
if (empty($workflow->getTypePlugin()->getStates())) {
drupal_set_message($this->t('Created the %label Workflow. In order for the workflow to be enabled there needs to be at least one state.', [
'%label' => $workflow->label(),
]));
......
......@@ -69,7 +69,7 @@ public function form(array $form, FormStateInterface $form_state) {
],
];
$states = $workflow->getStates();
$states = $workflow->getTypePlugin()->getStates();
// Warn the user if there are no states.
if (empty($states)) {
......@@ -146,7 +146,7 @@ public function form(array $form, FormStateInterface $form_state) {
],
],
];
foreach ($workflow->getTransitions() as $transition) {
foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
$links['edit'] = [
'title' => $this->t('Edit'),
'url' => Url::fromRoute('entity.workflow.edit_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
......@@ -233,10 +233,10 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
$entity->set('label', $values['label']);
$entity->set('id', $values['id']);
foreach ($values['states'] as $state_id => $state_values) {
$entity->setStateWeight($state_id, $state_values['weight']);
$entity->getTypePlugin()->setStateWeight($state_id, $state_values['weight']);
}
foreach ($values['transitions'] as $transition_id => $transition_values) {
$entity->setTransitionWeight($transition_id, $transition_values['weight']);
$entity->getTypePlugin()->setTransitionWeight($transition_id, $transition_values['weight']);
}
}
......
......@@ -63,7 +63,7 @@ public function form(array $form, FormStateInterface $form_state) {
public function exists($state_id) {
/** @var \Drupal\workflows\WorkflowInterface $original_workflow */
$original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
return $original_workflow->hasState($state_id);
return $original_workflow->getTypePlugin()->hasState($state_id);
}
/**
......@@ -84,8 +84,8 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
// Replicate the validation that Workflow::addState() does internally as the
// form values have not been validated at this point.
if (!$entity->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
$entity->addState($values['id'], $values['label']);
if (!$entity->getTypePlugin()->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
$entity->getTypePlugin()->addState($values['id'], $values['label']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
// @todo move to submitStateConfigurationForm. #2849827.
......@@ -103,7 +103,7 @@ public function save(array $form, FormStateInterface $form_state) {
$workflow = $this->entity;
$workflow->save();
drupal_set_message($this->t('Created %label state.', [
'%label' => $workflow->getState($form_state->getValue('id'))->label(),
'%label' => $workflow->getTypePlugin()->getState($form_state->getValue('id'))->label(),
]));
$form_state->setRedirectUrl($workflow->toUrl('edit-form'));
}
......
......@@ -37,7 +37,7 @@ public function getFormId() {
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getTypePlugin()->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
}
/**
......@@ -70,13 +70,13 @@ public function getConfirmText() {
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_state = NULL) {
if (!$workflow->hasState($workflow_state)) {
if (!$workflow->getTypePlugin()->hasState($workflow_state)) {
throw new NotFoundHttpException();
}
$this->workflow = $workflow;
$this->stateId = $workflow_state;
if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getState($this->stateId))) {
if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getTypePlugin()->getState($this->stateId))) {
$form['#title'] = $this->getQuestion();
$form['description'] = ['#markup' => $this->t('This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.')];
return $form;
......@@ -90,10 +90,11 @@ public function buildForm(array $form, FormStateInterface $form_state, WorkflowI
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$workflow_label = $this->workflow->getState($this->stateId)->label();
$workflow_label = $this->workflow->getTypePlugin()->getState($this->stateId)->label();
$this->workflow
->deleteState($this->stateId)
->save();
->getTypePlugin()
->deleteState($this->stateId);
$this->workflow->save();
drupal_set_message($this->t(
'State %label deleted.',
......
......@@ -42,7 +42,7 @@ public function form(array $form, FormStateInterface $form_state) {
/* @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $this->getEntity();
$state = $workflow->getState($this->stateId);
$state = $workflow->getTypePlugin()->getState($this->stateId);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
......@@ -124,7 +124,7 @@ public function form(array $form, FormStateInterface $form_state) {
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
/** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues();
$entity->setStateLabel($values['id'], $values['label']);
$entity->getTypePlugin()->setStateLabel($values['id'], $values['label']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
$configuration['states'][$values['id']] += $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
......@@ -140,7 +140,7 @@ public function save(array $form, FormStateInterface $form_state) {
$workflow = $this->entity;
$workflow->save();
drupal_set_message($this->t('Saved %label state.', [
'%label' => $workflow->getState($this->stateId)->label(),
'%label' => $workflow->getTypePlugin()->getState($this->stateId)->label(),
]));
$form_state->setRedirectUrl($workflow->toUrl('edit-form'));
}
......
......@@ -45,7 +45,7 @@ public function form(array $form, FormStateInterface $form_state) {
// @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
// only valid transitions are selectable.
$states = array_map([State::class, 'labelCallback'], $workflow->getStates());
$states = array_map([State::class, 'labelCallback'], $workflow->getTypePlugin()->getStates());
$form['from'] = [
'#type' => 'checkboxes',
'#title' => $this->t('From'),
......@@ -82,7 +82,7 @@ public function form(array $form, FormStateInterface $form_state) {
public function exists($transition_id) {
/** @var \Drupal\workflows\WorkflowInterface $original_workflow */
$original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
return $original_workflow->hasTransition($transition_id);
return $original_workflow->getTypePlugin()->hasTransition($transition_id);
}
/**
......@@ -104,7 +104,7 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
}
/** @var \Drupal\workflows\WorkflowInterface $entity */
$values = $form_state->getValues();
$entity->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
$entity->getTypePlugin()->addTransition($values['id'], $values['label'], array_filter($values['from']), $values['to']);
if (isset($values['type_settings'])) {
$configuration = $entity->getTypePlugin()->getConfiguration();
$configuration['transitions'][$values['id']] += $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
......@@ -121,10 +121,10 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$workflow = $this->getEntity();
$values = $form_state->getValues();
foreach (array_filter($values['from']) as $from_state_id) {
if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
if ($workflow->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [
'%from' => $workflow->getState($from_state_id)->label(),
'%to' => $workflow->getState($values['to'])->label(),
'%from' => $workflow->getTypePlugin()->getState($from_state_id)->label(),
'%to' => $workflow->getTypePlugin()->getState($values['to'])->label(),
]));
}
}
......
......@@ -78,7 +78,7 @@ public function getConfirmText() {
*/
public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_transition = NULL) {
try {
$this->transition = $workflow->getTransition($workflow_transition);
$this->transition = $workflow->getTypePlugin()->getTransition($workflow_transition);
}
catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException();
......@@ -92,8 +92,9 @@ public function buildForm(array $form, FormStateInterface $form_state, WorkflowI
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->workflow
->deleteTransition($this->transition->id())
->save();
->getTypePlugin()
->deleteTransition($this->transition->id());
$this->workflow->save();
drupal_set_message($this->t('%transition transition deleted.', ['%transition' => $this->transition->label()]));
$form_state->setRedirectUrl($this->getCancelUrl());
......
......@@ -43,7 +43,7 @@ public function form(array $form, FormStateInterface $form_state) {
/* @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $this->getEntity();
$transition = $workflow->getTransition($this->transitionId);
$transition = $workflow->getTypePlugin()->getTransition($this->transitionId);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
......@@ -60,7 +60,7 @@ public function form(array $form, FormStateInterface $form_state) {
// @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
// only valid transitions are selectable.
$states = array_map([State::class, 'labelCallback'], $workflow->getStates());
$states = array_map([State::class, 'labelCallback'], $workflow->getTypePlugin()->getStates());
$form['from'] = [
'#type' => 'checkboxes',
'#title' => $this->t('From'),
......@@ -94,12 +94,12 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$workflow = $this->getEntity();
$values = $form_state->getValues();
foreach (array_filter($values['from']) as $from_state_id) {
if ($workflow->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$transition = $workflow->getTransitionFromStateToState($from_state_id, $values['to']);
if ($workflow->getTypePlugin()->hasTransitionFromStateToState($from_state_id, $values['to'])) {
$transition = $workflow->getTypePlugin()->getTransitionFromStateToState($from_state_id, $values['to']);
if ($transition->id() !== $values['id']) {
$form_state->setErrorByName('from][' . $from_state_id, $this->t('The transition from %from to %to already exists.', [