You are here

class EntityModerationForm in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Form/EntityModerationForm.php \Drupal\workbench_moderation\Form\EntityModerationForm

Defines a form for entity moderation.

Hierarchy

Expanded class hierarchy of EntityModerationForm

1 file declares its use of EntityModerationForm
EntityOperations.php in src/EntityOperations.php

File

src/Form/EntityModerationForm.php, line 18

Namespace

Drupal\workbench_moderation\Form
View source
class EntityModerationForm extends FormBase {

  /**
   * Moderation info service.
   *
   * @var \Drupal\workbench_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * State transition validation service.
   *
   * @var \Drupal\workbench_moderation\StateTransitionValidation
   */
  protected $validation;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * EntityModerationForm constructor.
   *
   * @param \Drupal\workbench_moderation\ModerationInformationInterface $moderation_info
   *   Moderation info service.
   * @param \Drupal\workbench_moderation\StateTransitionValidation $validation
   *   State transition validation service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   */
  public function __construct(ModerationInformationInterface $moderation_info, StateTransitionValidation $validation, EntityTypeManagerInterface $entity_type_manager) {
    $this->moderationInfo = $moderation_info;
    $this->validation = $validation;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('workbench_moderation.moderation_information'), $container
      ->get('workbench_moderation.state_transition_validation'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'workbench_moderation_entity_moderation_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL) {

    /** @var \Drupal\workbench_moderation\Entity\ModerationState $current_state */
    $current_state = $entity->moderation_state->entity;
    $transitions = $this->validation
      ->getValidTransitions($entity, $this
      ->currentUser());

    // Exclude self-transitions.
    $transitions = array_filter($transitions, function (ModerationStateTransition $transition) use ($current_state) {
      return $transition
        ->getToState() != $current_state
        ->id();
    });
    $target_states = [];

    /** @var \Drupal\workbench_moderation\Entity\ModerationStateTransition $transition */
    foreach ($transitions as $transition) {
      $target_states[$transition
        ->getToState()] = $transition
        ->label();
    }
    if (!count($target_states)) {
      return $form;
    }
    if ($current_state) {
      $form['current'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Status'),
        '#markup' => $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('Moderate'),
      '#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'][] = 'workbench_moderation/entity-moderation-form';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_state
      ->get('entity');
    $new_state = $form_state
      ->getValue('new_state');
    $entity->moderation_state->target_id = $new_state;
    if ($entity instanceof RevisionLogInterface) {
      $entity
        ->setRevisionLogMessage($form_state
        ->getValue('revision_log'));
      $entity
        ->setRevisionUserId($this
        ->currentUser()
        ->id());
    }
    $entity
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('The moderation state has been updated.'));

    /** @var \Drupal\workbench_moderation\Entity\ModerationState $state */
    $state = $this->entityTypeManager
      ->getStorage('moderation_state')
      ->load($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
    // still exist.
    if ($state
      ->isDefaultRevisionState()) {
      $form_state
        ->setRedirectUrl($entity
        ->toUrl('canonical'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
EntityModerationForm::$entityTypeManager protected property Entity type manager service.
EntityModerationForm::$moderationInfo protected property Moderation info service.
EntityModerationForm::$validation protected property State transition validation service.
EntityModerationForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EntityModerationForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EntityModerationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EntityModerationForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityModerationForm::__construct public function EntityModerationForm constructor.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
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.