class EntityModerationForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm
- 10 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm
The EntityModerationForm provides a simple UI for changing moderation state.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_moderation\Form\EntityModerationForm
Expanded class hierarchy of EntityModerationForm
1 file declares its use of EntityModerationForm
- EntityOperations.php in core/
modules/ content_moderation/ src/ EntityOperations.php
File
- core/
modules/ content_moderation/ src/ Form/ EntityModerationForm.php, line 20
Namespace
Drupal\content_moderation\FormView source
class EntityModerationForm extends FormBase {
/**
* The moderation information service.
*
* @var \Drupal\content_moderation\ModerationInformationInterface
*/
protected $moderationInfo;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\Time
*/
protected $time;
/**
* The moderation state transition validation service.
*
* @var \Drupal\content_moderation\StateTransitionValidationInterface
*/
protected $validation;
/**
* EntityModerationForm constructor.
*
* @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
* The moderation information service.
* @param \Drupal\content_moderation\StateTransitionValidationInterface $validation
* The moderation state transition validation service.
* @param \Drupal\Component\Datetime\Time $time
* The time service.
*/
public function __construct(ModerationInformationInterface $moderation_info, StateTransitionValidationInterface $validation, Time $time) {
$this->moderationInfo = $moderation_info;
$this->validation = $validation;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('content_moderation.moderation_information'), $container
->get('content_moderation.state_transition_validation'), $container
->get('datetime.time'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'content_moderation_entity_moderation_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL) {
$current_state = $entity->moderation_state->value;
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
/** @var \Drupal\workflows\Transition[] $transitions */
$transitions = $this->validation
->getValidTransitions($entity, $this
->currentUser());
// Exclude self-transitions.
$transitions = array_filter($transitions, function (Transition $transition) use ($current_state) {
return $transition
->to()
->id() != $current_state;
});
$target_states = [];
foreach ($transitions as $transition) {
$target_states[$transition
->to()
->id()] = $transition
->to()
->label();
}
if (!count($target_states)) {
return $form;
}
if ($current_state) {
$form['current'] = [
'#type' => 'item',
'#title' => $this
->t('Moderation state'),
'#markup' => $workflow
->getTypePlugin()
->getState($current_state)
->label(),
];
}
// Persist the entity so we can access it in the submit handler.
$form_state
->set('entity', $entity);
$form['new_state'] = [
'#type' => 'select',
'#title' => $this
->t('Change to'),
'#options' => $target_states,
];
$form['revision_log'] = [
'#type' => 'textfield',
'#title' => $this
->t('Log message'),
'#size' => 30,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply'),
];
$form['#theme'] = [
'entity_moderation_form',
];
$form['#attached']['library'][] = 'content_moderation/content_moderation';
// Moderating an entity is allowed in a workspace.
$form_state
->set('workspace_safe', TRUE);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $form_state
->get('entity');
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId());
$entity = $storage
->createRevision($entity, $entity
->isDefaultRevision());
$new_state = $form_state
->getValue('new_state');
$entity
->set('moderation_state', $new_state);
if ($entity instanceof RevisionLogInterface) {
$entity
->setRevisionCreationTime($this->time
->getRequestTime());
$entity
->setRevisionLogMessage($form_state
->getValue('revision_log'));
$entity
->setRevisionUserId($this
->currentUser()
->id());
}
$entity
->save();
$this
->messenger()
->addStatus($this
->t('The moderation state has been updated.'));
$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
// pending revision. Redirect to the canonical URL instead, since that will
// still exist.
if ($new_state
->isDefaultRevisionState()) {
$form_state
->setRedirectUrl($entity
->toUrl('canonical'));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityModerationForm:: |
protected | property | The moderation information service. | |
EntityModerationForm:: |
protected | property | The time service. | |
EntityModerationForm:: |
protected | property | The moderation state transition validation service. | |
EntityModerationForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EntityModerationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityModerationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityModerationForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EntityModerationForm:: |
public | function | EntityModerationForm constructor. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |