/** * Verify moderation state methods based on entity properties. * * @covers ::isPublishedState * @covers ::isDefaultRevisionState * * @dataProvider moderationStateProvider */ public function testModerationStateProperties($published, $default_revision, $is_published, $is_default) { $moderation_state_id = $this->randomMachineName(); $moderation_state = ModerationState::create(['id' => $moderation_state_id, 'label' => $this->randomString(), 'published' => $published, 'default_revision' => $default_revision]); $moderation_state->save(); $moderation_state = ModerationState::load($moderation_state_id); $this->assertEquals($is_published, $moderation_state->isPublishedState()); $this->assertEquals($is_default, $moderation_state->isDefaultRevisionState()); }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */ $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle()); if (!$this->moderationInformation->isModeratableEntity($entity)) { // @todo write a test for this. return $element + ['#access' => FALSE]; } $options = $this->fieldDefinition->getFieldStorageDefinition()->getOptionsProvider($this->column, $entity)->getSettableOptions($this->currentUser); $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE); /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */ $default_state = ModerationState::load($default); if (!$default || !$default_state) { throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label())); } // @todo write a test for this. $from = $this->moderationStateTransitionEntityQuery->condition('stateFrom', $default)->execute(); // Can always keep this one as is. $to[$default] = $default; // @todo write a test for this. $allowed = $bundle_entity->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []); if ($from) { /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */ foreach ($this->moderationStateTransitionStorage->loadMultiple($from) as $id => $transition) { $to_state = $transition->getToState(); if ($this->currentUser->hasPermission('use ' . $id . ' transition') && in_array($to_state, $allowed, TRUE)) { $to[$to_state] = $to_state; } } } $options = array_intersect_key($options, $to); // @todo write a test for this. $element += ['#access' => count($options), '#type' => 'select', '#options' => $options, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE]; if ($this->currentUser->hasPermission($this->getAdminPermission($entity->getEntityType())) && count($options)) { // Use the dropbutton. $element['#process'][] = [get_called_class(), 'processActions']; // Don't show in sidebar/body. $element['#access'] = FALSE; } else { // Place the field as a details element in the advanced tab-set in e.g. // the sidebar. $element = ['#type' => 'details', '#group' => 'advanced', '#open' => TRUE, '#weight' => -10, '#title' => t('Moderation state'), 'target_id' => $element]; } return $element; }